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"
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 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 static const struct got_error *
54 poll_fd(int fd, int events, int timeout)
55 {
56 struct pollfd pfd[1];
57 int n;
59 pfd[0].fd = fd;
60 pfd[0].events = events;
62 n = poll(pfd, 1, timeout);
63 if (n == -1)
64 return got_error_from_errno("poll");
65 if (n == 0)
66 return got_error(GOT_ERR_TIMEOUT);
67 if (pfd[0].revents & (POLLERR | POLLNVAL))
68 return got_error_from_errno("poll error");
69 if (pfd[0].revents & (events | POLLHUP))
70 return NULL;
72 return got_error(GOT_ERR_INTERRUPT);
73 }
75 static const struct got_error *
76 read_imsg(struct imsgbuf *ibuf)
77 {
78 const struct got_error *err;
79 size_t n;
81 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
82 if (err)
83 return err;
85 n = imsg_read(ibuf);
86 if (n == -1) {
87 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
88 return got_error(GOT_ERR_PRIVSEP_NO_FD);
89 return got_error(GOT_ERR_PRIVSEP_READ);
90 }
91 if (n == 0)
92 return got_error(GOT_ERR_PRIVSEP_PIPE);
94 return NULL;
95 }
97 const struct got_error *
98 got_privsep_wait_for_child(pid_t pid)
99 {
100 int child_status;
102 if (waitpid(pid, &child_status, 0) == -1)
103 return got_error_from_errno("waitpid");
105 if (!WIFEXITED(child_status))
106 return got_error(GOT_ERR_PRIVSEP_DIED);
108 if (WEXITSTATUS(child_status) != 0)
109 return got_error(GOT_ERR_PRIVSEP_EXIT);
111 return NULL;
114 static const struct got_error *
115 recv_imsg_error(struct imsg *imsg, size_t datalen)
117 struct got_imsg_error *ierr;
119 if (datalen != sizeof(*ierr))
120 return got_error(GOT_ERR_PRIVSEP_LEN);
122 ierr = imsg->data;
123 if (ierr->code == GOT_ERR_ERRNO) {
124 static struct got_error serr;
125 serr.code = GOT_ERR_ERRNO;
126 serr.msg = strerror(ierr->errno_code);
127 return &serr;
130 return got_error(ierr->code);
133 const struct got_error *
134 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
135 size_t min_datalen)
137 const struct got_error *err;
138 ssize_t n;
140 n = imsg_get(ibuf, imsg);
141 if (n == -1)
142 return got_error_from_errno("imsg_get");
144 while (n == 0) {
145 err = read_imsg(ibuf);
146 if (err)
147 return err;
148 n = imsg_get(ibuf, imsg);
151 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
152 return got_error(GOT_ERR_PRIVSEP_LEN);
154 if (imsg->hdr.type == GOT_IMSG_ERROR) {
155 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
156 return recv_imsg_error(imsg, datalen);
159 return NULL;
162 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
163 void
164 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
166 const struct got_error *poll_err;
167 struct got_imsg_error ierr;
168 int ret;
170 ierr.code = err->code;
171 if (err->code == GOT_ERR_ERRNO)
172 ierr.errno_code = errno;
173 else
174 ierr.errno_code = 0;
175 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
176 if (ret == -1) {
177 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
178 getprogname(), err->code, err->msg, strerror(errno));
179 return;
182 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
183 if (poll_err) {
184 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
185 getprogname(), err->code, err->msg, poll_err->msg);
186 return;
189 ret = imsg_flush(ibuf);
190 if (ret == -1) {
191 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
192 getprogname(), err->code, err->msg, strerror(errno));
193 return;
197 static const struct got_error *
198 flush_imsg(struct imsgbuf *ibuf)
200 const struct got_error *err;
202 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
203 if (err)
204 return err;
206 if (imsg_flush(ibuf) == -1)
207 return got_error_from_errno("imsg_flush");
209 return NULL;
212 const struct got_error *
213 got_privsep_send_stop(int fd)
215 const struct got_error *err = NULL;
216 struct imsgbuf ibuf;
218 imsg_init(&ibuf, fd);
220 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
221 return got_error_from_errno("imsg_compose STOP");
223 err = flush_imsg(&ibuf);
224 imsg_clear(&ibuf);
225 return err;
228 const struct got_error *
229 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
231 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
232 == -1)
233 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
235 return flush_imsg(ibuf);
238 const struct got_error *
239 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
240 struct got_object_id *id, int pack_idx)
242 const struct got_error *err = NULL;
243 struct got_imsg_packed_object iobj, *iobjp;
244 size_t len;
246 if (id) { /* commit is packed */
247 iobj.idx = pack_idx;
248 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
249 iobjp = &iobj;
250 len = sizeof(iobj);
251 } else {
252 iobjp = NULL;
253 len = 0;
256 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
257 == -1) {
258 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
259 close(fd);
260 return err;
263 return flush_imsg(ibuf);
266 const struct got_error *
267 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
268 struct got_object_id *id, int pack_idx)
270 const struct got_error *err = NULL;
271 struct got_imsg_packed_object iobj, *iobjp;
272 size_t len;
274 if (id) { /* tree is packed */
275 iobj.idx = pack_idx;
276 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
277 iobjp = &iobj;
278 len = sizeof(iobj);
279 } else {
280 iobjp = NULL;
281 len = 0;
284 if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
285 == -1) {
286 err = got_error_from_errno("imsg_compose TREE_REQUEST");
287 close(fd);
288 return err;
291 return flush_imsg(ibuf);
294 const struct got_error *
295 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
296 struct got_object_id *id, int pack_idx)
298 struct got_imsg_packed_object iobj, *iobjp;
299 size_t len;
301 if (id) { /* tag is packed */
302 iobj.idx = pack_idx;
303 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
304 iobjp = &iobj;
305 len = sizeof(iobj);
306 } else {
307 iobjp = NULL;
308 len = 0;
311 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
312 == -1)
313 return got_error_from_errno("imsg_compose TAG_REQUEST");
315 return flush_imsg(ibuf);
318 const struct got_error *
319 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
320 struct got_object_id *id, int pack_idx)
322 const struct got_error *err = NULL;
323 struct got_imsg_packed_object iobj, *iobjp;
324 size_t len;
326 if (id) { /* blob is packed */
327 iobj.idx = pack_idx;
328 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
329 iobjp = &iobj;
330 len = sizeof(iobj);
331 } else {
332 iobjp = NULL;
333 len = 0;
336 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
337 == -1) {
338 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
339 close(infd);
340 return err;
343 return flush_imsg(ibuf);
346 const struct got_error *
347 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
349 const struct got_error *err = NULL;
351 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
352 == -1) {
353 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
354 close(outfd);
355 return err;
358 return flush_imsg(ibuf);
361 const struct got_error *
362 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
364 const struct got_error *err = NULL;
366 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
367 == -1) {
368 err = got_error_from_errno("imsg_compose TMPFD");
369 close(fd);
370 return err;
373 return flush_imsg(ibuf);
376 const struct got_error *
377 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
379 struct got_imsg_object iobj;
381 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
382 iobj.type = obj->type;
383 iobj.flags = obj->flags;
384 iobj.hdrlen = obj->hdrlen;
385 iobj.size = obj->size;
386 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
387 iobj.pack_offset = obj->pack_offset;
388 iobj.pack_idx = obj->pack_idx;
391 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
392 == -1)
393 return got_error_from_errno("imsg_compose OBJECT");
395 return flush_imsg(ibuf);
398 const struct got_error *
399 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
400 struct imsgbuf *ibuf)
402 const struct got_error *err = NULL;
403 struct got_imsg_object *iobj;
404 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
406 if (datalen != sizeof(*iobj))
407 return got_error(GOT_ERR_PRIVSEP_LEN);
408 iobj = imsg->data;
410 *obj = calloc(1, sizeof(**obj));
411 if (*obj == NULL)
412 return got_error_from_errno("calloc");
414 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
415 (*obj)->type = iobj->type;
416 (*obj)->flags = iobj->flags;
417 (*obj)->hdrlen = iobj->hdrlen;
418 (*obj)->size = iobj->size;
419 /* path_packfile is handled by caller */
420 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
421 (*obj)->pack_offset = iobj->pack_offset;
422 (*obj)->pack_idx = iobj->pack_idx;
425 return err;
428 const struct got_error *
429 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
431 const struct got_error *err = NULL;
432 struct imsg imsg;
433 const size_t min_datalen =
434 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
436 *obj = NULL;
438 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
439 if (err)
440 return err;
442 switch (imsg.hdr.type) {
443 case GOT_IMSG_OBJECT:
444 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
445 break;
446 default:
447 err = got_error(GOT_ERR_PRIVSEP_MSG);
448 break;
451 imsg_free(&imsg);
453 return err;
456 static const struct got_error *
457 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
458 size_t logmsg_len)
460 const struct got_error *err = NULL;
461 size_t offset, remain;
463 offset = 0;
464 remain = logmsg_len;
465 while (remain > 0) {
466 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
468 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
469 commit->logmsg + offset, n) == -1) {
470 err = got_error_from_errno("imsg_compose "
471 "COMMIT_LOGMSG");
472 break;
475 err = flush_imsg(ibuf);
476 if (err)
477 break;
479 offset += n;
480 remain -= n;
483 return err;
486 const struct got_error *
487 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
489 const struct got_error *err = NULL;
490 struct got_imsg_commit_object *icommit;
491 uint8_t *buf;
492 size_t len, total;
493 struct got_object_qid *qid;
494 size_t author_len = strlen(commit->author);
495 size_t committer_len = strlen(commit->committer);
496 size_t logmsg_len = strlen(commit->logmsg);
498 total = sizeof(*icommit) + author_len + committer_len +
499 commit->nparents * SHA1_DIGEST_LENGTH;
501 buf = malloc(total);
502 if (buf == NULL)
503 return got_error_from_errno("malloc");
505 icommit = (struct got_imsg_commit_object *)buf;
506 memcpy(icommit->tree_id, commit->tree_id->sha1,
507 sizeof(icommit->tree_id));
508 icommit->author_len = author_len;
509 icommit->author_time = commit->author_time;
510 icommit->author_gmtoff = commit->author_gmtoff;
511 icommit->committer_len = committer_len;
512 icommit->committer_time = commit->committer_time;
513 icommit->committer_gmtoff = commit->committer_gmtoff;
514 icommit->logmsg_len = logmsg_len;
515 icommit->nparents = commit->nparents;
517 len = sizeof(*icommit);
518 memcpy(buf + len, commit->author, author_len);
519 len += author_len;
520 memcpy(buf + len, commit->committer, committer_len);
521 len += committer_len;
522 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
523 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
524 len += SHA1_DIGEST_LENGTH;
527 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
528 err = got_error_from_errno("imsg_compose COMMIT");
529 goto done;
532 if (logmsg_len == 0 ||
533 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
534 err = flush_imsg(ibuf);
535 if (err)
536 goto done;
538 err = send_commit_logmsg(ibuf, commit, logmsg_len);
539 done:
540 free(buf);
541 return err;
544 const struct got_error *
545 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
547 const struct got_error *err = NULL;
548 struct imsg imsg;
549 struct got_imsg_commit_object *icommit;
550 size_t len, datalen;
551 int i;
552 const size_t min_datalen =
553 MIN(sizeof(struct got_imsg_error),
554 sizeof(struct got_imsg_commit_object));
556 *commit = NULL;
558 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
559 if (err)
560 return err;
562 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
563 len = 0;
565 switch (imsg.hdr.type) {
566 case GOT_IMSG_COMMIT:
567 if (datalen < sizeof(*icommit)) {
568 err = got_error(GOT_ERR_PRIVSEP_LEN);
569 break;
571 icommit = imsg.data;
572 if (datalen != sizeof(*icommit) + icommit->author_len +
573 icommit->committer_len +
574 icommit->nparents * SHA1_DIGEST_LENGTH) {
575 err = got_error(GOT_ERR_PRIVSEP_LEN);
576 break;
578 if (icommit->nparents < 0) {
579 err = got_error(GOT_ERR_PRIVSEP_LEN);
580 break;
582 len += sizeof(*icommit);
584 *commit = got_object_commit_alloc_partial();
585 if (*commit == NULL) {
586 err = got_error_from_errno(
587 "got_object_commit_alloc_partial");
588 break;
591 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
592 SHA1_DIGEST_LENGTH);
593 (*commit)->author_time = icommit->author_time;
594 (*commit)->author_gmtoff = icommit->author_gmtoff;
595 (*commit)->committer_time = icommit->committer_time;
596 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
598 if (icommit->author_len == 0) {
599 (*commit)->author = strdup("");
600 if ((*commit)->author == NULL) {
601 err = got_error_from_errno("strdup");
602 break;
604 } else {
605 (*commit)->author = malloc(icommit->author_len + 1);
606 if ((*commit)->author == NULL) {
607 err = got_error_from_errno("malloc");
608 break;
610 memcpy((*commit)->author, imsg.data + len,
611 icommit->author_len);
612 (*commit)->author[icommit->author_len] = '\0';
614 len += icommit->author_len;
616 if (icommit->committer_len == 0) {
617 (*commit)->committer = strdup("");
618 if ((*commit)->committer == NULL) {
619 err = got_error_from_errno("strdup");
620 break;
622 } else {
623 (*commit)->committer =
624 malloc(icommit->committer_len + 1);
625 if ((*commit)->committer == NULL) {
626 err = got_error_from_errno("malloc");
627 break;
629 memcpy((*commit)->committer, imsg.data + len,
630 icommit->committer_len);
631 (*commit)->committer[icommit->committer_len] = '\0';
633 len += icommit->committer_len;
635 if (icommit->logmsg_len == 0) {
636 (*commit)->logmsg = strdup("");
637 if ((*commit)->logmsg == NULL) {
638 err = got_error_from_errno("strdup");
639 break;
641 } else {
642 size_t offset = 0, remain = icommit->logmsg_len;
644 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
645 if ((*commit)->logmsg == NULL) {
646 err = got_error_from_errno("malloc");
647 break;
649 while (remain > 0) {
650 struct imsg imsg_log;
651 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
652 remain);
654 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
655 if (err)
656 return err;
658 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
659 return got_error(GOT_ERR_PRIVSEP_MSG);
661 memcpy((*commit)->logmsg + offset,
662 imsg_log.data, n);
663 imsg_free(&imsg_log);
664 offset += n;
665 remain -= n;
667 (*commit)->logmsg[icommit->logmsg_len] = '\0';
670 for (i = 0; i < icommit->nparents; i++) {
671 struct got_object_qid *qid;
673 err = got_object_qid_alloc_partial(&qid);
674 if (err)
675 break;
676 memcpy(qid->id, imsg.data + len +
677 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
678 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
679 (*commit)->nparents++;
681 break;
682 default:
683 err = got_error(GOT_ERR_PRIVSEP_MSG);
684 break;
687 imsg_free(&imsg);
689 return err;
692 const struct got_error *
693 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
695 const struct got_error *err = NULL;
696 struct got_imsg_tree_object itree;
697 struct got_tree_entry *te;
698 size_t totlen;
699 int nimsg; /* number of imsg queued in ibuf */
701 itree.nentries = tree->entries.nentries;
702 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
703 == -1)
704 return got_error_from_errno("imsg_compose TREE");
706 totlen = sizeof(itree);
707 nimsg = 1;
708 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
709 struct got_imsg_tree_entry *ite;
710 uint8_t *buf = NULL;
711 size_t len = sizeof(*ite) + strlen(te->name);
713 if (len > MAX_IMSGSIZE)
714 return got_error(GOT_ERR_NO_SPACE);
716 nimsg++;
717 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
718 err = flush_imsg(ibuf);
719 if (err)
720 return err;
721 nimsg = 0;
724 buf = malloc(len);
725 if (buf == NULL)
726 return got_error_from_errno("malloc");
728 ite = (struct got_imsg_tree_entry *)buf;
729 memcpy(ite->id, te->id->sha1, sizeof(ite->id));
730 ite->mode = te->mode;
731 memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
733 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
734 buf, len) == -1)
735 err = got_error_from_errno("imsg_compose TREE_ENTRY");
736 free(buf);
737 if (err)
738 return err;
739 totlen += len;
742 return flush_imsg(ibuf);
745 const struct got_error *
746 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
748 const struct got_error *err = NULL;
749 const size_t min_datalen =
750 MIN(sizeof(struct got_imsg_error),
751 sizeof(struct got_imsg_tree_object));
752 struct got_imsg_tree_object *itree;
753 int nentries = 0;
755 *tree = NULL;
756 get_more:
757 err = read_imsg(ibuf);
758 if (err)
759 goto done;
761 for (;;) {
762 struct imsg imsg;
763 size_t n;
764 size_t datalen;
765 struct got_imsg_tree_entry *ite;
766 struct got_tree_entry *te = NULL;
768 n = imsg_get(ibuf, &imsg);
769 if (n == 0) {
770 if (*tree && (*tree)->entries.nentries != nentries)
771 goto get_more;
772 break;
775 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
776 return got_error(GOT_ERR_PRIVSEP_LEN);
778 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
780 switch (imsg.hdr.type) {
781 case GOT_IMSG_ERROR:
782 err = recv_imsg_error(&imsg, datalen);
783 break;
784 case GOT_IMSG_TREE:
785 /* This message should only appear once. */
786 if (*tree != NULL) {
787 err = got_error(GOT_ERR_PRIVSEP_MSG);
788 break;
790 if (datalen != sizeof(*itree)) {
791 err = got_error(GOT_ERR_PRIVSEP_LEN);
792 break;
794 itree = imsg.data;
795 *tree = malloc(sizeof(**tree));
796 if (*tree == NULL) {
797 err = got_error_from_errno("malloc");
798 break;
800 (*tree)->entries.nentries = itree->nentries;
801 SIMPLEQ_INIT(&(*tree)->entries.head);
802 (*tree)->refcnt = 0;
803 break;
804 case GOT_IMSG_TREE_ENTRY:
805 /* This message should be preceeded by GOT_IMSG_TREE. */
806 if (*tree == NULL) {
807 err = got_error(GOT_ERR_PRIVSEP_MSG);
808 break;
810 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
811 err = got_error(GOT_ERR_PRIVSEP_LEN);
812 break;
815 /* Remaining data contains the entry's name. */
816 datalen -= sizeof(*ite);
817 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
818 err = got_error(GOT_ERR_PRIVSEP_LEN);
819 break;
821 ite = imsg.data;
823 te = got_alloc_tree_entry_partial();
824 if (te == NULL) {
825 err = got_error_from_errno(
826 "got_alloc_tree_entry_partial");
827 break;
829 te->name = malloc(datalen + 1);
830 if (te->name == NULL) {
831 free(te);
832 err = got_error_from_errno("malloc");
833 break;
835 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
836 te->name[datalen] = '\0';
838 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
839 te->mode = ite->mode;
840 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
841 nentries++;
842 break;
843 default:
844 err = got_error(GOT_ERR_PRIVSEP_MSG);
845 break;
848 imsg_free(&imsg);
850 done:
851 if (*tree && (*tree)->entries.nentries != nentries) {
852 if (err == NULL)
853 err = got_error(GOT_ERR_PRIVSEP_LEN);
854 got_object_tree_close(*tree);
855 *tree = NULL;
858 return err;
861 const struct got_error *
862 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
863 const uint8_t *data)
865 struct got_imsg_blob iblob;
867 iblob.size = size;
868 iblob.hdrlen = hdrlen;
870 if (data) {
871 uint8_t *buf;
873 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
874 return got_error(GOT_ERR_NO_SPACE);
876 buf = malloc(sizeof(iblob) + size);
877 if (buf == NULL)
878 return got_error_from_errno("malloc");
880 memcpy(buf, &iblob, sizeof(iblob));
881 memcpy(buf + sizeof(iblob), data, size);
882 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
883 sizeof(iblob) + size) == -1) {
884 free(buf);
885 return got_error_from_errno("imsg_compose BLOB");
887 free(buf);
888 } else {
889 /* Data has already been written to file descriptor. */
890 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
891 sizeof(iblob)) == -1)
892 return got_error_from_errno("imsg_compose BLOB");
896 return flush_imsg(ibuf);
899 const struct got_error *
900 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
901 struct imsgbuf *ibuf)
903 const struct got_error *err = NULL;
904 struct imsg imsg;
905 struct got_imsg_blob *iblob;
906 size_t datalen;
908 *outbuf = NULL;
910 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
911 if (err)
912 return err;
914 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
916 switch (imsg.hdr.type) {
917 case GOT_IMSG_BLOB:
918 if (datalen < sizeof(*iblob)) {
919 err = got_error(GOT_ERR_PRIVSEP_LEN);
920 break;
922 iblob = imsg.data;
923 *size = iblob->size;
924 *hdrlen = iblob->hdrlen;
926 if (datalen == sizeof(*iblob)) {
927 /* Data has been written to file descriptor. */
928 break;
931 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
932 err = got_error(GOT_ERR_PRIVSEP_LEN);
933 break;
936 *outbuf = malloc(*size);
937 if (*outbuf == NULL) {
938 err = got_error_from_errno("malloc");
939 break;
941 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
942 break;
943 default:
944 err = got_error(GOT_ERR_PRIVSEP_MSG);
945 break;
948 imsg_free(&imsg);
950 return err;
953 static const struct got_error *
954 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
956 const struct got_error *err = NULL;
957 size_t offset, remain;
959 offset = 0;
960 remain = tagmsg_len;
961 while (remain > 0) {
962 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
964 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
965 tag->tagmsg + offset, n) == -1) {
966 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
967 break;
970 err = flush_imsg(ibuf);
971 if (err)
972 break;
974 offset += n;
975 remain -= n;
978 return err;
981 const struct got_error *
982 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
984 const struct got_error *err = NULL;
985 struct got_imsg_tag_object *itag;
986 uint8_t *buf;
987 size_t len, total;
988 size_t tag_len = strlen(tag->tag);
989 size_t tagger_len = strlen(tag->tagger);
990 size_t tagmsg_len = strlen(tag->tagmsg);
992 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
994 buf = malloc(total);
995 if (buf == NULL)
996 return got_error_from_errno("malloc");
998 itag = (struct got_imsg_tag_object *)buf;
999 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1000 itag->obj_type = tag->obj_type;
1001 itag->tag_len = tag_len;
1002 itag->tagger_len = tagger_len;
1003 itag->tagger_time = tag->tagger_time;
1004 itag->tagger_gmtoff = tag->tagger_gmtoff;
1005 itag->tagmsg_len = tagmsg_len;
1007 len = sizeof(*itag);
1008 memcpy(buf + len, tag->tag, tag_len);
1009 len += tag_len;
1010 memcpy(buf + len, tag->tagger, tagger_len);
1011 len += tagger_len;
1013 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1014 err = got_error_from_errno("imsg_compose TAG");
1015 goto done;
1018 if (tagmsg_len == 0 ||
1019 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1020 err = flush_imsg(ibuf);
1021 if (err)
1022 goto done;
1024 err = send_tagmsg(ibuf, tag, tagmsg_len);
1025 done:
1026 free(buf);
1027 return err;
1030 const struct got_error *
1031 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1033 const struct got_error *err = NULL;
1034 struct imsg imsg;
1035 struct got_imsg_tag_object *itag;
1036 size_t len, datalen;
1037 const size_t min_datalen =
1038 MIN(sizeof(struct got_imsg_error),
1039 sizeof(struct got_imsg_tag_object));
1041 *tag = NULL;
1043 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1044 if (err)
1045 return err;
1047 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1048 len = 0;
1050 switch (imsg.hdr.type) {
1051 case GOT_IMSG_TAG:
1052 if (datalen < sizeof(*itag)) {
1053 err = got_error(GOT_ERR_PRIVSEP_LEN);
1054 break;
1056 itag = imsg.data;
1057 if (datalen != sizeof(*itag) + itag->tag_len +
1058 itag->tagger_len) {
1059 err = got_error(GOT_ERR_PRIVSEP_LEN);
1060 break;
1062 len += sizeof(*itag);
1064 *tag = calloc(1, sizeof(**tag));
1065 if (*tag == NULL) {
1066 err = got_error_from_errno("calloc");
1067 break;
1070 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1072 if (itag->tag_len == 0) {
1073 (*tag)->tag = strdup("");
1074 if ((*tag)->tag == NULL) {
1075 err = got_error_from_errno("strdup");
1076 break;
1078 } else {
1079 (*tag)->tag = malloc(itag->tag_len + 1);
1080 if ((*tag)->tag == NULL) {
1081 err = got_error_from_errno("malloc");
1082 break;
1084 memcpy((*tag)->tag, imsg.data + len,
1085 itag->tag_len);
1086 (*tag)->tag[itag->tag_len] = '\0';
1088 len += itag->tag_len;
1090 (*tag)->obj_type = itag->obj_type;
1091 (*tag)->tagger_time = itag->tagger_time;
1092 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1094 if (itag->tagger_len == 0) {
1095 (*tag)->tagger = strdup("");
1096 if ((*tag)->tagger == NULL) {
1097 err = got_error_from_errno("strdup");
1098 break;
1100 } else {
1101 (*tag)->tagger = malloc(itag->tagger_len + 1);
1102 if ((*tag)->tagger == NULL) {
1103 err = got_error_from_errno("malloc");
1104 break;
1106 memcpy((*tag)->tagger, imsg.data + len,
1107 itag->tagger_len);
1108 (*tag)->tagger[itag->tagger_len] = '\0';
1110 len += itag->tagger_len;
1112 if (itag->tagmsg_len == 0) {
1113 (*tag)->tagmsg = strdup("");
1114 if ((*tag)->tagmsg == NULL) {
1115 err = got_error_from_errno("strdup");
1116 break;
1118 } else {
1119 size_t offset = 0, remain = itag->tagmsg_len;
1121 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1122 if ((*tag)->tagmsg == NULL) {
1123 err = got_error_from_errno("malloc");
1124 break;
1126 while (remain > 0) {
1127 struct imsg imsg_log;
1128 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1129 remain);
1131 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1132 if (err)
1133 return err;
1135 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1136 return got_error(GOT_ERR_PRIVSEP_MSG);
1138 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1139 n);
1140 imsg_free(&imsg_log);
1141 offset += n;
1142 remain -= n;
1144 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1147 break;
1148 default:
1149 err = got_error(GOT_ERR_PRIVSEP_MSG);
1150 break;
1153 imsg_free(&imsg);
1155 return err;
1158 const struct got_error *
1159 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1160 struct got_packidx *packidx)
1162 const struct got_error *err = NULL;
1163 struct got_imsg_packidx ipackidx;
1164 struct got_imsg_pack ipack;
1165 int fd;
1167 ipackidx.len = packidx->len;
1168 fd = dup(packidx->fd);
1169 if (fd == -1)
1170 return got_error_from_errno("dup");
1172 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1173 sizeof(ipackidx)) == -1) {
1174 err = got_error_from_errno("imsg_compose PACKIDX");
1175 close(fd);
1176 return err;
1179 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1180 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1181 return got_error(GOT_ERR_NO_SPACE);
1182 ipack.filesize = pack->filesize;
1184 fd = dup(pack->fd);
1185 if (fd == -1)
1186 return got_error_from_errno("dup");
1188 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1189 == -1) {
1190 err = got_error_from_errno("imsg_compose PACK");
1191 close(fd);
1192 return err;
1195 return flush_imsg(ibuf);
1198 const struct got_error *
1199 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1200 struct got_object_id *id)
1202 struct got_imsg_packed_object iobj;
1204 iobj.idx = idx;
1205 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1207 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1208 &iobj, sizeof(iobj)) == -1)
1209 return got_error_from_errno("imsg_compose "
1210 "PACKED_OBJECT_REQUEST");
1212 return flush_imsg(ibuf);
1215 const struct got_error *
1216 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1218 const struct got_error *err = NULL;
1220 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1221 NULL, 0) == -1) {
1222 err = got_error_from_errno("imsg_compose "
1223 "GITCONFIG_PARSE_REQUEST");
1224 close(fd);
1225 return err;
1228 return flush_imsg(ibuf);
1231 const struct got_error *
1232 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1234 if (imsg_compose(ibuf,
1235 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1236 NULL, 0) == -1)
1237 return got_error_from_errno("imsg_compose "
1238 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1240 return flush_imsg(ibuf);
1243 const struct got_error *
1244 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1246 if (imsg_compose(ibuf,
1247 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1248 return got_error_from_errno("imsg_compose "
1249 "GITCONFIG_AUTHOR_NAME_REQUEST");
1251 return flush_imsg(ibuf);
1254 const struct got_error *
1255 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1257 if (imsg_compose(ibuf,
1258 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1259 return got_error_from_errno("imsg_compose "
1260 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1262 return flush_imsg(ibuf);
1265 const struct got_error *
1266 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1268 size_t len = value ? strlen(value) + 1 : 0;
1270 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1271 value, len) == -1)
1272 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1274 return flush_imsg(ibuf);
1277 const struct got_error *
1278 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1280 const struct got_error *err = NULL;
1281 struct imsg imsg;
1282 size_t datalen;
1283 const size_t min_datalen = 0;
1285 *str = NULL;
1287 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1288 if (err)
1289 return err;
1290 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1292 switch (imsg.hdr.type) {
1293 case GOT_IMSG_GITCONFIG_STR_VAL:
1294 if (datalen == 0)
1295 break;
1296 *str = malloc(datalen);
1297 if (*str == NULL) {
1298 err = got_error_from_errno("malloc");
1299 break;
1301 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1302 err = got_error(GOT_ERR_NO_SPACE);
1303 break;
1304 default:
1305 err = got_error(GOT_ERR_PRIVSEP_MSG);
1306 break;
1309 imsg_free(&imsg);
1310 return err;
1313 const struct got_error *
1314 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1316 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1317 &value, sizeof(value)) == -1)
1318 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1320 return flush_imsg(ibuf);
1323 const struct got_error *
1324 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1326 const struct got_error *err = NULL;
1327 struct imsg imsg;
1328 size_t datalen;
1329 const size_t min_datalen =
1330 MIN(sizeof(struct got_imsg_error), sizeof(int));
1332 *val = 0;
1334 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1335 if (err)
1336 return err;
1337 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1339 switch (imsg.hdr.type) {
1340 case GOT_IMSG_GITCONFIG_INT_VAL:
1341 if (datalen != sizeof(*val)) {
1342 err = got_error(GOT_ERR_PRIVSEP_LEN);
1343 break;
1345 memcpy(val, imsg.data, sizeof(*val));
1346 break;
1347 default:
1348 err = got_error(GOT_ERR_PRIVSEP_MSG);
1349 break;
1352 imsg_free(&imsg);
1353 return err;
1356 const struct got_error *
1357 got_privsep_unveil_exec_helpers(void)
1359 const char *helpers[] = {
1360 GOT_PATH_PROG_READ_PACK,
1361 GOT_PATH_PROG_READ_OBJECT,
1362 GOT_PATH_PROG_READ_COMMIT,
1363 GOT_PATH_PROG_READ_TREE,
1364 GOT_PATH_PROG_READ_BLOB,
1365 GOT_PATH_PROG_READ_TAG,
1366 GOT_PATH_PROG_READ_GITCONFIG,
1368 int i;
1370 for (i = 0; i < nitems(helpers); i++) {
1371 if (unveil(helpers[i], "x") == 0)
1372 continue;
1373 return got_error_from_errno2("unveil", helpers[i]);
1376 return NULL;
1379 void
1380 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1382 if (close(imsg_fds[0]) != 0) {
1383 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1384 _exit(1);
1387 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1388 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1389 _exit(1);
1391 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1392 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1393 _exit(1);
1396 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1397 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1398 strerror(errno));
1399 _exit(1);