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 if (err)
740 return err;
741 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
742 err = got_error_from_errno("imsg_add TREE_ENTRY");
743 if (err)
744 return err;
746 if (imsg_add(wbuf, name, namelen) == -1)
747 err = got_error_from_errno("imsg_add TREE_ENTRY");
748 if (err)
749 return err;
751 wbuf->fd = -1;
752 imsg_close(ibuf, wbuf);
754 totlen += len;
757 return flush_imsg(ibuf);
760 const struct got_error *
761 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
763 const struct got_error *err = NULL;
764 const size_t min_datalen =
765 MIN(sizeof(struct got_imsg_error),
766 sizeof(struct got_imsg_tree_object));
767 struct got_imsg_tree_object *itree;
768 int nentries = 0;
770 *tree = NULL;
771 get_more:
772 err = read_imsg(ibuf);
773 if (err)
774 goto done;
776 for (;;) {
777 struct imsg imsg;
778 size_t n;
779 size_t datalen;
780 struct got_imsg_tree_entry *ite;
781 struct got_tree_entry *te = NULL;
783 n = imsg_get(ibuf, &imsg);
784 if (n == 0) {
785 if (*tree && (*tree)->entries.nentries != nentries)
786 goto get_more;
787 break;
790 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
791 return got_error(GOT_ERR_PRIVSEP_LEN);
793 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
795 switch (imsg.hdr.type) {
796 case GOT_IMSG_ERROR:
797 err = recv_imsg_error(&imsg, datalen);
798 break;
799 case GOT_IMSG_TREE:
800 /* This message should only appear once. */
801 if (*tree != NULL) {
802 err = got_error(GOT_ERR_PRIVSEP_MSG);
803 break;
805 if (datalen != sizeof(*itree)) {
806 err = got_error(GOT_ERR_PRIVSEP_LEN);
807 break;
809 itree = imsg.data;
810 *tree = malloc(sizeof(**tree));
811 if (*tree == NULL) {
812 err = got_error_from_errno("malloc");
813 break;
815 (*tree)->entries.nentries = itree->nentries;
816 SIMPLEQ_INIT(&(*tree)->entries.head);
817 (*tree)->refcnt = 0;
818 break;
819 case GOT_IMSG_TREE_ENTRY:
820 /* This message should be preceeded by GOT_IMSG_TREE. */
821 if (*tree == NULL) {
822 err = got_error(GOT_ERR_PRIVSEP_MSG);
823 break;
825 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
826 err = got_error(GOT_ERR_PRIVSEP_LEN);
827 break;
830 /* Remaining data contains the entry's name. */
831 datalen -= sizeof(*ite);
832 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
833 err = got_error(GOT_ERR_PRIVSEP_LEN);
834 break;
836 ite = imsg.data;
838 te = got_alloc_tree_entry_partial();
839 if (te == NULL) {
840 err = got_error_from_errno(
841 "got_alloc_tree_entry_partial");
842 break;
844 te->name = malloc(datalen + 1);
845 if (te->name == NULL) {
846 free(te);
847 err = got_error_from_errno("malloc");
848 break;
850 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
851 te->name[datalen] = '\0';
853 memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
854 te->mode = ite->mode;
855 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
856 nentries++;
857 break;
858 default:
859 err = got_error(GOT_ERR_PRIVSEP_MSG);
860 break;
863 imsg_free(&imsg);
865 done:
866 if (*tree && (*tree)->entries.nentries != nentries) {
867 if (err == NULL)
868 err = got_error(GOT_ERR_PRIVSEP_LEN);
869 got_object_tree_close(*tree);
870 *tree = NULL;
873 return err;
876 const struct got_error *
877 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
878 const uint8_t *data)
880 struct got_imsg_blob iblob;
882 iblob.size = size;
883 iblob.hdrlen = hdrlen;
885 if (data) {
886 uint8_t *buf;
888 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
889 return got_error(GOT_ERR_NO_SPACE);
891 buf = malloc(sizeof(iblob) + size);
892 if (buf == NULL)
893 return got_error_from_errno("malloc");
895 memcpy(buf, &iblob, sizeof(iblob));
896 memcpy(buf + sizeof(iblob), data, size);
897 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
898 sizeof(iblob) + size) == -1) {
899 free(buf);
900 return got_error_from_errno("imsg_compose BLOB");
902 free(buf);
903 } else {
904 /* Data has already been written to file descriptor. */
905 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
906 sizeof(iblob)) == -1)
907 return got_error_from_errno("imsg_compose BLOB");
911 return flush_imsg(ibuf);
914 const struct got_error *
915 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
916 struct imsgbuf *ibuf)
918 const struct got_error *err = NULL;
919 struct imsg imsg;
920 struct got_imsg_blob *iblob;
921 size_t datalen;
923 *outbuf = NULL;
925 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
926 if (err)
927 return err;
929 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
931 switch (imsg.hdr.type) {
932 case GOT_IMSG_BLOB:
933 if (datalen < sizeof(*iblob)) {
934 err = got_error(GOT_ERR_PRIVSEP_LEN);
935 break;
937 iblob = imsg.data;
938 *size = iblob->size;
939 *hdrlen = iblob->hdrlen;
941 if (datalen == sizeof(*iblob)) {
942 /* Data has been written to file descriptor. */
943 break;
946 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
947 err = got_error(GOT_ERR_PRIVSEP_LEN);
948 break;
951 *outbuf = malloc(*size);
952 if (*outbuf == NULL) {
953 err = got_error_from_errno("malloc");
954 break;
956 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
957 break;
958 default:
959 err = got_error(GOT_ERR_PRIVSEP_MSG);
960 break;
963 imsg_free(&imsg);
965 return err;
968 static const struct got_error *
969 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
971 const struct got_error *err = NULL;
972 size_t offset, remain;
974 offset = 0;
975 remain = tagmsg_len;
976 while (remain > 0) {
977 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
979 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
980 tag->tagmsg + offset, n) == -1) {
981 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
982 break;
985 err = flush_imsg(ibuf);
986 if (err)
987 break;
989 offset += n;
990 remain -= n;
993 return err;
996 const struct got_error *
997 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
999 const struct got_error *err = NULL;
1000 struct got_imsg_tag_object *itag;
1001 uint8_t *buf;
1002 size_t len, total;
1003 size_t tag_len = strlen(tag->tag);
1004 size_t tagger_len = strlen(tag->tagger);
1005 size_t tagmsg_len = strlen(tag->tagmsg);
1007 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1009 buf = malloc(total);
1010 if (buf == NULL)
1011 return got_error_from_errno("malloc");
1013 itag = (struct got_imsg_tag_object *)buf;
1014 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1015 itag->obj_type = tag->obj_type;
1016 itag->tag_len = tag_len;
1017 itag->tagger_len = tagger_len;
1018 itag->tagger_time = tag->tagger_time;
1019 itag->tagger_gmtoff = tag->tagger_gmtoff;
1020 itag->tagmsg_len = tagmsg_len;
1022 len = sizeof(*itag);
1023 memcpy(buf + len, tag->tag, tag_len);
1024 len += tag_len;
1025 memcpy(buf + len, tag->tagger, tagger_len);
1026 len += tagger_len;
1028 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1029 err = got_error_from_errno("imsg_compose TAG");
1030 goto done;
1033 if (tagmsg_len == 0 ||
1034 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1035 err = flush_imsg(ibuf);
1036 if (err)
1037 goto done;
1039 err = send_tagmsg(ibuf, tag, tagmsg_len);
1040 done:
1041 free(buf);
1042 return err;
1045 const struct got_error *
1046 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1048 const struct got_error *err = NULL;
1049 struct imsg imsg;
1050 struct got_imsg_tag_object *itag;
1051 size_t len, datalen;
1052 const size_t min_datalen =
1053 MIN(sizeof(struct got_imsg_error),
1054 sizeof(struct got_imsg_tag_object));
1056 *tag = NULL;
1058 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1059 if (err)
1060 return err;
1062 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1063 len = 0;
1065 switch (imsg.hdr.type) {
1066 case GOT_IMSG_TAG:
1067 if (datalen < sizeof(*itag)) {
1068 err = got_error(GOT_ERR_PRIVSEP_LEN);
1069 break;
1071 itag = imsg.data;
1072 if (datalen != sizeof(*itag) + itag->tag_len +
1073 itag->tagger_len) {
1074 err = got_error(GOT_ERR_PRIVSEP_LEN);
1075 break;
1077 len += sizeof(*itag);
1079 *tag = calloc(1, sizeof(**tag));
1080 if (*tag == NULL) {
1081 err = got_error_from_errno("calloc");
1082 break;
1085 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1087 if (itag->tag_len == 0) {
1088 (*tag)->tag = strdup("");
1089 if ((*tag)->tag == NULL) {
1090 err = got_error_from_errno("strdup");
1091 break;
1093 } else {
1094 (*tag)->tag = malloc(itag->tag_len + 1);
1095 if ((*tag)->tag == NULL) {
1096 err = got_error_from_errno("malloc");
1097 break;
1099 memcpy((*tag)->tag, imsg.data + len,
1100 itag->tag_len);
1101 (*tag)->tag[itag->tag_len] = '\0';
1103 len += itag->tag_len;
1105 (*tag)->obj_type = itag->obj_type;
1106 (*tag)->tagger_time = itag->tagger_time;
1107 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1109 if (itag->tagger_len == 0) {
1110 (*tag)->tagger = strdup("");
1111 if ((*tag)->tagger == NULL) {
1112 err = got_error_from_errno("strdup");
1113 break;
1115 } else {
1116 (*tag)->tagger = malloc(itag->tagger_len + 1);
1117 if ((*tag)->tagger == NULL) {
1118 err = got_error_from_errno("malloc");
1119 break;
1121 memcpy((*tag)->tagger, imsg.data + len,
1122 itag->tagger_len);
1123 (*tag)->tagger[itag->tagger_len] = '\0';
1125 len += itag->tagger_len;
1127 if (itag->tagmsg_len == 0) {
1128 (*tag)->tagmsg = strdup("");
1129 if ((*tag)->tagmsg == NULL) {
1130 err = got_error_from_errno("strdup");
1131 break;
1133 } else {
1134 size_t offset = 0, remain = itag->tagmsg_len;
1136 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1137 if ((*tag)->tagmsg == NULL) {
1138 err = got_error_from_errno("malloc");
1139 break;
1141 while (remain > 0) {
1142 struct imsg imsg_log;
1143 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1144 remain);
1146 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1147 if (err)
1148 return err;
1150 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1151 return got_error(GOT_ERR_PRIVSEP_MSG);
1153 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1154 n);
1155 imsg_free(&imsg_log);
1156 offset += n;
1157 remain -= n;
1159 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1162 break;
1163 default:
1164 err = got_error(GOT_ERR_PRIVSEP_MSG);
1165 break;
1168 imsg_free(&imsg);
1170 return err;
1173 const struct got_error *
1174 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1175 struct got_packidx *packidx)
1177 const struct got_error *err = NULL;
1178 struct got_imsg_packidx ipackidx;
1179 struct got_imsg_pack ipack;
1180 int fd;
1182 ipackidx.len = packidx->len;
1183 fd = dup(packidx->fd);
1184 if (fd == -1)
1185 return got_error_from_errno("dup");
1187 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1188 sizeof(ipackidx)) == -1) {
1189 err = got_error_from_errno("imsg_compose PACKIDX");
1190 close(fd);
1191 return err;
1194 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1195 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1196 return got_error(GOT_ERR_NO_SPACE);
1197 ipack.filesize = pack->filesize;
1199 fd = dup(pack->fd);
1200 if (fd == -1)
1201 return got_error_from_errno("dup");
1203 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1204 == -1) {
1205 err = got_error_from_errno("imsg_compose PACK");
1206 close(fd);
1207 return err;
1210 return flush_imsg(ibuf);
1213 const struct got_error *
1214 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1215 struct got_object_id *id)
1217 struct got_imsg_packed_object iobj;
1219 iobj.idx = idx;
1220 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1222 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1223 &iobj, sizeof(iobj)) == -1)
1224 return got_error_from_errno("imsg_compose "
1225 "PACKED_OBJECT_REQUEST");
1227 return flush_imsg(ibuf);
1230 const struct got_error *
1231 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1233 const struct got_error *err = NULL;
1235 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1236 NULL, 0) == -1) {
1237 err = got_error_from_errno("imsg_compose "
1238 "GITCONFIG_PARSE_REQUEST");
1239 close(fd);
1240 return err;
1243 return flush_imsg(ibuf);
1246 const struct got_error *
1247 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1249 if (imsg_compose(ibuf,
1250 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1251 NULL, 0) == -1)
1252 return got_error_from_errno("imsg_compose "
1253 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1255 return flush_imsg(ibuf);
1258 const struct got_error *
1259 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1261 if (imsg_compose(ibuf,
1262 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1263 return got_error_from_errno("imsg_compose "
1264 "GITCONFIG_AUTHOR_NAME_REQUEST");
1266 return flush_imsg(ibuf);
1269 const struct got_error *
1270 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1272 if (imsg_compose(ibuf,
1273 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1274 return got_error_from_errno("imsg_compose "
1275 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1277 return flush_imsg(ibuf);
1280 const struct got_error *
1281 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1283 size_t len = value ? strlen(value) + 1 : 0;
1285 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1286 value, len) == -1)
1287 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1289 return flush_imsg(ibuf);
1292 const struct got_error *
1293 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1295 const struct got_error *err = NULL;
1296 struct imsg imsg;
1297 size_t datalen;
1298 const size_t min_datalen = 0;
1300 *str = NULL;
1302 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1303 if (err)
1304 return err;
1305 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1307 switch (imsg.hdr.type) {
1308 case GOT_IMSG_GITCONFIG_STR_VAL:
1309 if (datalen == 0)
1310 break;
1311 *str = malloc(datalen);
1312 if (*str == NULL) {
1313 err = got_error_from_errno("malloc");
1314 break;
1316 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1317 err = got_error(GOT_ERR_NO_SPACE);
1318 break;
1319 default:
1320 err = got_error(GOT_ERR_PRIVSEP_MSG);
1321 break;
1324 imsg_free(&imsg);
1325 return err;
1328 const struct got_error *
1329 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1331 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1332 &value, sizeof(value)) == -1)
1333 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1335 return flush_imsg(ibuf);
1338 const struct got_error *
1339 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1341 const struct got_error *err = NULL;
1342 struct imsg imsg;
1343 size_t datalen;
1344 const size_t min_datalen =
1345 MIN(sizeof(struct got_imsg_error), sizeof(int));
1347 *val = 0;
1349 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1350 if (err)
1351 return err;
1352 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1354 switch (imsg.hdr.type) {
1355 case GOT_IMSG_GITCONFIG_INT_VAL:
1356 if (datalen != sizeof(*val)) {
1357 err = got_error(GOT_ERR_PRIVSEP_LEN);
1358 break;
1360 memcpy(val, imsg.data, sizeof(*val));
1361 break;
1362 default:
1363 err = got_error(GOT_ERR_PRIVSEP_MSG);
1364 break;
1367 imsg_free(&imsg);
1368 return err;
1371 const struct got_error *
1372 got_privsep_unveil_exec_helpers(void)
1374 const char *helpers[] = {
1375 GOT_PATH_PROG_READ_PACK,
1376 GOT_PATH_PROG_READ_OBJECT,
1377 GOT_PATH_PROG_READ_COMMIT,
1378 GOT_PATH_PROG_READ_TREE,
1379 GOT_PATH_PROG_READ_BLOB,
1380 GOT_PATH_PROG_READ_TAG,
1381 GOT_PATH_PROG_READ_GITCONFIG,
1383 int i;
1385 for (i = 0; i < nitems(helpers); i++) {
1386 if (unveil(helpers[i], "x") == 0)
1387 continue;
1388 return got_error_from_errno2("unveil", helpers[i]);
1391 return NULL;
1394 void
1395 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1397 if (close(imsg_fds[0]) != 0) {
1398 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1399 _exit(1);
1402 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1403 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1404 _exit(1);
1406 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1407 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1408 _exit(1);
1411 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1412 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1413 strerror(errno));
1414 _exit(1);