Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/syslimits.h>
22 #include <sys/wait.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <poll.h>
30 #include <imsg.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <time.h>
35 #include "got_object.h"
36 #include "got_error.h"
37 #include "got_path.h"
38 #include "got_repository.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_parse.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_pack.h"
48 #ifndef MIN
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 #endif
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static const struct got_error *
57 poll_fd(int fd, int events, int timeout)
58 {
59 struct pollfd pfd[1];
60 int n;
62 pfd[0].fd = fd;
63 pfd[0].events = events;
65 n = poll(pfd, 1, timeout);
66 if (n == -1)
67 return got_error_from_errno("poll");
68 if (n == 0)
69 return got_error(GOT_ERR_TIMEOUT);
70 if (pfd[0].revents & (POLLERR | POLLNVAL))
71 return got_error_from_errno("poll error");
72 if (pfd[0].revents & (events | POLLHUP))
73 return NULL;
75 return got_error(GOT_ERR_INTERRUPT);
76 }
78 static const struct got_error *
79 read_imsg(struct imsgbuf *ibuf)
80 {
81 const struct got_error *err;
82 size_t n;
84 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
85 if (err)
86 return err;
88 n = imsg_read(ibuf);
89 if (n == -1) {
90 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
91 return got_error(GOT_ERR_PRIVSEP_NO_FD);
92 return got_error(GOT_ERR_PRIVSEP_READ);
93 }
94 if (n == 0)
95 return got_error(GOT_ERR_PRIVSEP_PIPE);
97 return NULL;
98 }
100 const struct got_error *
101 got_privsep_wait_for_child(pid_t pid)
103 int child_status;
105 if (waitpid(pid, &child_status, 0) == -1)
106 return got_error_from_errno("waitpid");
108 if (!WIFEXITED(child_status))
109 return got_error(GOT_ERR_PRIVSEP_DIED);
111 if (WEXITSTATUS(child_status) != 0)
112 return got_error(GOT_ERR_PRIVSEP_EXIT);
114 return NULL;
117 static const struct got_error *
118 recv_imsg_error(struct imsg *imsg, size_t datalen)
120 struct got_imsg_error *ierr;
122 if (datalen != sizeof(*ierr))
123 return got_error(GOT_ERR_PRIVSEP_LEN);
125 ierr = imsg->data;
126 if (ierr->code == GOT_ERR_ERRNO) {
127 static struct got_error serr;
128 serr.code = GOT_ERR_ERRNO;
129 serr.msg = strerror(ierr->errno_code);
130 return &serr;
133 return got_error(ierr->code);
136 const struct got_error *
137 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
138 size_t min_datalen)
140 const struct got_error *err;
141 ssize_t n;
143 n = imsg_get(ibuf, imsg);
144 if (n == -1)
145 return got_error_from_errno("imsg_get");
147 while (n == 0) {
148 err = read_imsg(ibuf);
149 if (err)
150 return err;
151 n = imsg_get(ibuf, imsg);
152 if (n == -1)
153 return got_error_from_errno("imsg_get");
156 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
157 return got_error(GOT_ERR_PRIVSEP_LEN);
159 if (imsg->hdr.type == GOT_IMSG_ERROR) {
160 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
161 return recv_imsg_error(imsg, datalen);
164 return NULL;
167 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
168 void
169 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
171 const struct got_error *poll_err;
172 struct got_imsg_error ierr;
173 int ret;
175 ierr.code = err->code;
176 if (err->code == GOT_ERR_ERRNO)
177 ierr.errno_code = errno;
178 else
179 ierr.errno_code = 0;
180 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
181 if (ret == -1) {
182 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
183 getprogname(), err->code, err->msg, strerror(errno));
184 return;
187 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
188 if (poll_err) {
189 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
190 getprogname(), err->code, err->msg, poll_err->msg);
191 return;
194 ret = imsg_flush(ibuf);
195 if (ret == -1) {
196 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
197 getprogname(), err->code, err->msg, strerror(errno));
198 return;
202 static const struct got_error *
203 flush_imsg(struct imsgbuf *ibuf)
205 const struct got_error *err;
207 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
208 if (err)
209 return err;
211 if (imsg_flush(ibuf) == -1)
212 return got_error_from_errno("imsg_flush");
214 return NULL;
217 const struct got_error *
218 got_privsep_send_stop(int fd)
220 const struct got_error *err = NULL;
221 struct imsgbuf ibuf;
223 imsg_init(&ibuf, fd);
225 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
226 return got_error_from_errno("imsg_compose STOP");
228 err = flush_imsg(&ibuf);
229 imsg_clear(&ibuf);
230 return err;
233 const struct got_error *
234 got_privsep_send_ack(struct imsgbuf *ibuf)
236 if (imsg_compose(ibuf, GOT_IMSG_ACK, 0, 0, -1, NULL, 0) == -1)
237 return got_error_from_errno("imsg_compose ACK");
238 return flush_imsg(ibuf);
241 const struct got_error *
242 got_privsep_wait_ack(struct imsgbuf *ibuf)
244 const struct got_error *err = NULL;
245 struct imsg imsg;
247 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
248 if (err)
249 return err;
250 if (imsg.hdr.type == GOT_IMSG_ACK && imsg.hdr.len - IMSG_HEADER_SIZE == 0)
251 return NULL;
252 else
253 return got_error(GOT_ERR_PRIVSEP_MSG);
254 imsg_free(&imsg);
258 const struct got_error *
259 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
261 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
262 == -1)
263 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
265 return flush_imsg(ibuf);
268 const struct got_error *
269 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
270 struct got_object_id *id, int pack_idx)
272 const struct got_error *err = NULL;
273 struct got_imsg_packed_object iobj, *iobjp;
274 size_t len;
276 if (id) { /* commit is packed */
277 iobj.idx = pack_idx;
278 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
279 iobjp = &iobj;
280 len = sizeof(iobj);
281 } else {
282 iobjp = NULL;
283 len = 0;
286 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
287 == -1) {
288 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
289 close(fd);
290 return err;
293 return flush_imsg(ibuf);
296 const struct got_error *
297 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
298 struct got_object_id *id, int pack_idx)
300 const struct got_error *err = NULL;
301 struct ibuf *wbuf;
302 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
304 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
305 if (wbuf == NULL)
306 return got_error_from_errno("imsg_create TREE_REQUEST");
308 if (id) { /* tree is packed */
309 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
310 err = got_error_from_errno("imsg_add TREE_ENTRY");
311 ibuf_free(wbuf);
312 return err;
315 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
316 err = got_error_from_errno("imsg_add TREE_ENTRY");
317 ibuf_free(wbuf);
318 return err;
322 wbuf->fd = fd;
323 imsg_close(ibuf, wbuf);
325 return flush_imsg(ibuf);
328 const struct got_error *
329 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
330 struct got_object_id *id, int pack_idx)
332 struct got_imsg_packed_object iobj, *iobjp;
333 size_t len;
335 if (id) { /* tag is packed */
336 iobj.idx = pack_idx;
337 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
338 iobjp = &iobj;
339 len = sizeof(iobj);
340 } else {
341 iobjp = NULL;
342 len = 0;
345 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
346 == -1)
347 return got_error_from_errno("imsg_compose TAG_REQUEST");
349 return flush_imsg(ibuf);
352 const struct got_error *
353 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
354 struct got_object_id *id, int pack_idx)
356 const struct got_error *err = NULL;
357 struct got_imsg_packed_object iobj, *iobjp;
358 size_t len;
360 if (id) { /* blob is packed */
361 iobj.idx = pack_idx;
362 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
363 iobjp = &iobj;
364 len = sizeof(iobj);
365 } else {
366 iobjp = NULL;
367 len = 0;
370 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
371 == -1) {
372 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
373 close(infd);
374 return err;
377 return flush_imsg(ibuf);
380 const struct got_error *
381 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
383 const struct got_error *err = NULL;
385 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
386 == -1) {
387 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
388 close(outfd);
389 return err;
392 return flush_imsg(ibuf);
395 const struct got_error *
396 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
398 const struct got_error *err = NULL;
400 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
401 == -1) {
402 err = got_error_from_errno("imsg_compose TMPFD");
403 close(fd);
404 return err;
407 return flush_imsg(ibuf);
410 const struct got_error *
411 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
413 struct got_imsg_object iobj;
415 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
416 iobj.type = obj->type;
417 iobj.flags = obj->flags;
418 iobj.hdrlen = obj->hdrlen;
419 iobj.size = obj->size;
420 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
421 iobj.pack_offset = obj->pack_offset;
422 iobj.pack_idx = obj->pack_idx;
425 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
426 == -1)
427 return got_error_from_errno("imsg_compose OBJECT");
429 return flush_imsg(ibuf);
432 const struct got_error *
433 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd)
435 const struct got_error *err = NULL;
437 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
438 NULL, 0) == -1) {
439 err = got_error_from_errno("imsg_compose FETCH_REQUEST");
440 close(fd);
441 return err;
443 return flush_imsg(ibuf);
446 const struct got_error *
447 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
449 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
450 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
451 return got_error_from_errno("imsg_compose FETCH");
452 return flush_imsg(ibuf);
455 const struct got_error *
456 got_privsep_wait_fetch_done(struct imsgbuf *ibuf, struct got_object_id *hash)
458 const struct got_error *err = NULL;
459 struct imsg imsg;
461 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
462 if (err)
463 return err;
464 if (imsg.hdr.type == GOT_IMSG_FETCH_DONE &&
465 imsg.hdr.len - sizeof(imsg.hdr) == SHA1_DIGEST_LENGTH)
466 return NULL;
467 else
468 return got_error(GOT_ERR_PRIVSEP_MSG);
469 imsg_free(&imsg);
473 const struct got_error *
474 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id hash)
476 const struct got_error *err = NULL;
478 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
479 hash.sha1, SHA1_DIGEST_LENGTH) == -1) {
480 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
481 close(fd);
482 return err;
484 return flush_imsg(ibuf);
487 const struct got_error *
488 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
490 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
491 return got_error_from_errno("imsg_compose FETCH");
492 return flush_imsg(ibuf);
495 const struct got_error *
496 got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
498 const struct got_error *err = NULL;
499 struct imsg imsg;
501 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
502 if (err)
503 return err;
504 if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
505 return NULL;
506 else
507 return got_error(GOT_ERR_PRIVSEP_MSG);
508 imsg_free(&imsg);
511 const struct got_error *
512 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
513 struct imsgbuf *ibuf)
515 const struct got_error *err = NULL;
516 struct got_imsg_object *iobj;
517 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
519 if (datalen != sizeof(*iobj))
520 return got_error(GOT_ERR_PRIVSEP_LEN);
521 iobj = imsg->data;
523 *obj = calloc(1, sizeof(**obj));
524 if (*obj == NULL)
525 return got_error_from_errno("calloc");
527 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
528 (*obj)->type = iobj->type;
529 (*obj)->flags = iobj->flags;
530 (*obj)->hdrlen = iobj->hdrlen;
531 (*obj)->size = iobj->size;
532 /* path_packfile is handled by caller */
533 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
534 (*obj)->pack_offset = iobj->pack_offset;
535 (*obj)->pack_idx = iobj->pack_idx;
538 return err;
541 const struct got_error *
542 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
544 const struct got_error *err = NULL;
545 struct imsg imsg;
546 const size_t min_datalen =
547 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
549 *obj = NULL;
551 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
552 if (err)
553 return err;
555 switch (imsg.hdr.type) {
556 case GOT_IMSG_OBJECT:
557 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
558 break;
559 default:
560 err = got_error(GOT_ERR_PRIVSEP_MSG);
561 break;
564 imsg_free(&imsg);
566 return err;
569 static const struct got_error *
570 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
571 size_t logmsg_len)
573 const struct got_error *err = NULL;
574 size_t offset, remain;
576 offset = 0;
577 remain = logmsg_len;
578 while (remain > 0) {
579 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
581 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
582 commit->logmsg + offset, n) == -1) {
583 err = got_error_from_errno("imsg_compose "
584 "COMMIT_LOGMSG");
585 break;
588 err = flush_imsg(ibuf);
589 if (err)
590 break;
592 offset += n;
593 remain -= n;
596 return err;
599 const struct got_error *
600 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
602 const struct got_error *err = NULL;
603 struct got_imsg_commit_object *icommit;
604 uint8_t *buf;
605 size_t len, total;
606 struct got_object_qid *qid;
607 size_t author_len = strlen(commit->author);
608 size_t committer_len = strlen(commit->committer);
609 size_t logmsg_len = strlen(commit->logmsg);
611 total = sizeof(*icommit) + author_len + committer_len +
612 commit->nparents * SHA1_DIGEST_LENGTH;
614 buf = malloc(total);
615 if (buf == NULL)
616 return got_error_from_errno("malloc");
618 icommit = (struct got_imsg_commit_object *)buf;
619 memcpy(icommit->tree_id, commit->tree_id->sha1,
620 sizeof(icommit->tree_id));
621 icommit->author_len = author_len;
622 icommit->author_time = commit->author_time;
623 icommit->author_gmtoff = commit->author_gmtoff;
624 icommit->committer_len = committer_len;
625 icommit->committer_time = commit->committer_time;
626 icommit->committer_gmtoff = commit->committer_gmtoff;
627 icommit->logmsg_len = logmsg_len;
628 icommit->nparents = commit->nparents;
630 len = sizeof(*icommit);
631 memcpy(buf + len, commit->author, author_len);
632 len += author_len;
633 memcpy(buf + len, commit->committer, committer_len);
634 len += committer_len;
635 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
636 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
637 len += SHA1_DIGEST_LENGTH;
640 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
641 err = got_error_from_errno("imsg_compose COMMIT");
642 goto done;
645 if (logmsg_len == 0 ||
646 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
647 err = flush_imsg(ibuf);
648 if (err)
649 goto done;
651 err = send_commit_logmsg(ibuf, commit, logmsg_len);
652 done:
653 free(buf);
654 return err;
657 static const struct got_error *
658 get_commit_from_imsg(struct got_commit_object **commit,
659 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
661 const struct got_error *err = NULL;
662 struct got_imsg_commit_object *icommit;
663 size_t len = 0;
664 int i;
666 if (datalen < sizeof(*icommit))
667 return got_error(GOT_ERR_PRIVSEP_LEN);
669 icommit = imsg->data;
670 if (datalen != sizeof(*icommit) + icommit->author_len +
671 icommit->committer_len +
672 icommit->nparents * SHA1_DIGEST_LENGTH)
673 return got_error(GOT_ERR_PRIVSEP_LEN);
675 if (icommit->nparents < 0)
676 return got_error(GOT_ERR_PRIVSEP_LEN);
678 len += sizeof(*icommit);
680 *commit = got_object_commit_alloc_partial();
681 if (*commit == NULL)
682 return got_error_from_errno(
683 "got_object_commit_alloc_partial");
685 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
686 SHA1_DIGEST_LENGTH);
687 (*commit)->author_time = icommit->author_time;
688 (*commit)->author_gmtoff = icommit->author_gmtoff;
689 (*commit)->committer_time = icommit->committer_time;
690 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
692 if (icommit->author_len == 0) {
693 (*commit)->author = strdup("");
694 if ((*commit)->author == NULL) {
695 err = got_error_from_errno("strdup");
696 goto done;
698 } else {
699 (*commit)->author = malloc(icommit->author_len + 1);
700 if ((*commit)->author == NULL) {
701 err = got_error_from_errno("malloc");
702 goto done;
704 memcpy((*commit)->author, imsg->data + len,
705 icommit->author_len);
706 (*commit)->author[icommit->author_len] = '\0';
708 len += icommit->author_len;
710 if (icommit->committer_len == 0) {
711 (*commit)->committer = strdup("");
712 if ((*commit)->committer == NULL) {
713 err = got_error_from_errno("strdup");
714 goto done;
716 } else {
717 (*commit)->committer =
718 malloc(icommit->committer_len + 1);
719 if ((*commit)->committer == NULL) {
720 err = got_error_from_errno("malloc");
721 goto done;
723 memcpy((*commit)->committer, imsg->data + len,
724 icommit->committer_len);
725 (*commit)->committer[icommit->committer_len] = '\0';
727 len += icommit->committer_len;
729 if (icommit->logmsg_len == 0) {
730 (*commit)->logmsg = strdup("");
731 if ((*commit)->logmsg == NULL) {
732 err = got_error_from_errno("strdup");
733 goto done;
735 } else {
736 size_t offset = 0, remain = icommit->logmsg_len;
738 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
739 if ((*commit)->logmsg == NULL) {
740 err = got_error_from_errno("malloc");
741 goto done;
743 while (remain > 0) {
744 struct imsg imsg_log;
745 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
746 remain);
748 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
749 if (err)
750 goto done;
752 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
753 err = got_error(GOT_ERR_PRIVSEP_MSG);
754 goto done;
757 memcpy((*commit)->logmsg + offset,
758 imsg_log.data, n);
759 imsg_free(&imsg_log);
760 offset += n;
761 remain -= n;
763 (*commit)->logmsg[icommit->logmsg_len] = '\0';
766 for (i = 0; i < icommit->nparents; i++) {
767 struct got_object_qid *qid;
769 err = got_object_qid_alloc_partial(&qid);
770 if (err)
771 break;
772 memcpy(qid->id, imsg->data + len +
773 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
774 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
775 (*commit)->nparents++;
777 done:
778 if (err) {
779 got_object_commit_close(*commit);
780 *commit = NULL;
782 return err;
785 const struct got_error *
786 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
788 const struct got_error *err = NULL;
789 struct imsg imsg;
790 size_t datalen;
791 const size_t min_datalen =
792 MIN(sizeof(struct got_imsg_error),
793 sizeof(struct got_imsg_commit_object));
795 *commit = NULL;
797 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
798 if (err)
799 return err;
801 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
803 switch (imsg.hdr.type) {
804 case GOT_IMSG_COMMIT:
805 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
806 break;
807 default:
808 err = got_error(GOT_ERR_PRIVSEP_MSG);
809 break;
812 imsg_free(&imsg);
814 return err;
817 const struct got_error *
818 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
819 int nentries)
821 const struct got_error *err = NULL;
822 struct got_imsg_tree_object itree;
823 struct got_pathlist_entry *pe;
824 size_t totlen;
825 int nimsg; /* number of imsg queued in ibuf */
827 itree.nentries = nentries;
828 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
829 == -1)
830 return got_error_from_errno("imsg_compose TREE");
832 totlen = sizeof(itree);
833 nimsg = 1;
834 TAILQ_FOREACH(pe, entries, entry) {
835 const char *name = pe->path;
836 struct got_parsed_tree_entry *pte = pe->data;
837 struct ibuf *wbuf;
838 size_t namelen = strlen(name);
839 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
841 if (len > MAX_IMSGSIZE)
842 return got_error(GOT_ERR_NO_SPACE);
844 nimsg++;
845 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
846 err = flush_imsg(ibuf);
847 if (err)
848 return err;
849 nimsg = 0;
852 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
853 if (wbuf == NULL)
854 return got_error_from_errno("imsg_create TREE_ENTRY");
856 /* Keep in sync with struct got_imsg_tree_object definition! */
857 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
858 err = got_error_from_errno("imsg_add TREE_ENTRY");
859 ibuf_free(wbuf);
860 return err;
862 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
863 err = got_error_from_errno("imsg_add TREE_ENTRY");
864 ibuf_free(wbuf);
865 return err;
868 if (imsg_add(wbuf, name, namelen) == -1) {
869 err = got_error_from_errno("imsg_add TREE_ENTRY");
870 ibuf_free(wbuf);
871 return err;
874 wbuf->fd = -1;
875 imsg_close(ibuf, wbuf);
877 totlen += len;
880 return flush_imsg(ibuf);
883 const struct got_error *
884 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
886 const struct got_error *err = NULL;
887 const size_t min_datalen =
888 MIN(sizeof(struct got_imsg_error),
889 sizeof(struct got_imsg_tree_object));
890 struct got_imsg_tree_object *itree;
891 int nentries = 0;
893 *tree = NULL;
894 get_more:
895 err = read_imsg(ibuf);
896 if (err)
897 goto done;
899 for (;;) {
900 struct imsg imsg;
901 size_t n;
902 size_t datalen;
903 struct got_imsg_tree_entry *ite;
904 struct got_tree_entry *te = NULL;
906 n = imsg_get(ibuf, &imsg);
907 if (n == 0) {
908 if (*tree && (*tree)->nentries != nentries)
909 goto get_more;
910 break;
913 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
914 return got_error(GOT_ERR_PRIVSEP_LEN);
916 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
918 switch (imsg.hdr.type) {
919 case GOT_IMSG_ERROR:
920 err = recv_imsg_error(&imsg, datalen);
921 break;
922 case GOT_IMSG_TREE:
923 /* This message should only appear once. */
924 if (*tree != NULL) {
925 err = got_error(GOT_ERR_PRIVSEP_MSG);
926 break;
928 if (datalen != sizeof(*itree)) {
929 err = got_error(GOT_ERR_PRIVSEP_LEN);
930 break;
932 itree = imsg.data;
933 *tree = malloc(sizeof(**tree));
934 if (*tree == NULL) {
935 err = got_error_from_errno("malloc");
936 break;
938 (*tree)->entries = calloc(itree->nentries,
939 sizeof(struct got_tree_entry));
940 if ((*tree)->entries == NULL) {
941 err = got_error_from_errno("malloc");
942 break;
944 (*tree)->nentries = itree->nentries;
945 (*tree)->refcnt = 0;
946 break;
947 case GOT_IMSG_TREE_ENTRY:
948 /* This message should be preceeded by GOT_IMSG_TREE. */
949 if (*tree == NULL) {
950 err = got_error(GOT_ERR_PRIVSEP_MSG);
951 break;
953 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
954 err = got_error(GOT_ERR_PRIVSEP_LEN);
955 break;
958 /* Remaining data contains the entry's name. */
959 datalen -= sizeof(*ite);
960 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
961 err = got_error(GOT_ERR_PRIVSEP_LEN);
962 break;
964 ite = imsg.data;
966 if (datalen + 1 > sizeof(te->name)) {
967 err = got_error(GOT_ERR_NO_SPACE);
968 break;
970 te = &(*tree)->entries[nentries];
971 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
972 te->name[datalen] = '\0';
974 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
975 te->mode = ite->mode;
976 te->idx = nentries;
977 nentries++;
978 break;
979 default:
980 err = got_error(GOT_ERR_PRIVSEP_MSG);
981 break;
984 imsg_free(&imsg);
986 done:
987 if (*tree && (*tree)->nentries != nentries) {
988 if (err == NULL)
989 err = got_error(GOT_ERR_PRIVSEP_LEN);
990 got_object_tree_close(*tree);
991 *tree = NULL;
994 return err;
997 const struct got_error *
998 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
999 const uint8_t *data)
1001 struct got_imsg_blob iblob;
1003 iblob.size = size;
1004 iblob.hdrlen = hdrlen;
1006 if (data) {
1007 uint8_t *buf;
1009 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1010 return got_error(GOT_ERR_NO_SPACE);
1012 buf = malloc(sizeof(iblob) + size);
1013 if (buf == NULL)
1014 return got_error_from_errno("malloc");
1016 memcpy(buf, &iblob, sizeof(iblob));
1017 memcpy(buf + sizeof(iblob), data, size);
1018 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1019 sizeof(iblob) + size) == -1) {
1020 free(buf);
1021 return got_error_from_errno("imsg_compose BLOB");
1023 free(buf);
1024 } else {
1025 /* Data has already been written to file descriptor. */
1026 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1027 sizeof(iblob)) == -1)
1028 return got_error_from_errno("imsg_compose BLOB");
1032 return flush_imsg(ibuf);
1035 const struct got_error *
1036 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1037 struct imsgbuf *ibuf)
1039 const struct got_error *err = NULL;
1040 struct imsg imsg;
1041 struct got_imsg_blob *iblob;
1042 size_t datalen;
1044 *outbuf = NULL;
1046 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1047 if (err)
1048 return err;
1050 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1052 switch (imsg.hdr.type) {
1053 case GOT_IMSG_BLOB:
1054 if (datalen < sizeof(*iblob)) {
1055 err = got_error(GOT_ERR_PRIVSEP_LEN);
1056 break;
1058 iblob = imsg.data;
1059 *size = iblob->size;
1060 *hdrlen = iblob->hdrlen;
1062 if (datalen == sizeof(*iblob)) {
1063 /* Data has been written to file descriptor. */
1064 break;
1067 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1068 err = got_error(GOT_ERR_PRIVSEP_LEN);
1069 break;
1072 *outbuf = malloc(*size);
1073 if (*outbuf == NULL) {
1074 err = got_error_from_errno("malloc");
1075 break;
1077 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1078 break;
1079 default:
1080 err = got_error(GOT_ERR_PRIVSEP_MSG);
1081 break;
1084 imsg_free(&imsg);
1086 return err;
1089 static const struct got_error *
1090 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1092 const struct got_error *err = NULL;
1093 size_t offset, remain;
1095 offset = 0;
1096 remain = tagmsg_len;
1097 while (remain > 0) {
1098 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1100 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1101 tag->tagmsg + offset, n) == -1) {
1102 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1103 break;
1106 err = flush_imsg(ibuf);
1107 if (err)
1108 break;
1110 offset += n;
1111 remain -= n;
1114 return err;
1117 const struct got_error *
1118 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1120 const struct got_error *err = NULL;
1121 struct got_imsg_tag_object *itag;
1122 uint8_t *buf;
1123 size_t len, total;
1124 size_t tag_len = strlen(tag->tag);
1125 size_t tagger_len = strlen(tag->tagger);
1126 size_t tagmsg_len = strlen(tag->tagmsg);
1128 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1130 buf = malloc(total);
1131 if (buf == NULL)
1132 return got_error_from_errno("malloc");
1134 itag = (struct got_imsg_tag_object *)buf;
1135 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1136 itag->obj_type = tag->obj_type;
1137 itag->tag_len = tag_len;
1138 itag->tagger_len = tagger_len;
1139 itag->tagger_time = tag->tagger_time;
1140 itag->tagger_gmtoff = tag->tagger_gmtoff;
1141 itag->tagmsg_len = tagmsg_len;
1143 len = sizeof(*itag);
1144 memcpy(buf + len, tag->tag, tag_len);
1145 len += tag_len;
1146 memcpy(buf + len, tag->tagger, tagger_len);
1147 len += tagger_len;
1149 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1150 err = got_error_from_errno("imsg_compose TAG");
1151 goto done;
1154 if (tagmsg_len == 0 ||
1155 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1156 err = flush_imsg(ibuf);
1157 if (err)
1158 goto done;
1160 err = send_tagmsg(ibuf, tag, tagmsg_len);
1161 done:
1162 free(buf);
1163 return err;
1166 const struct got_error *
1167 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1169 const struct got_error *err = NULL;
1170 struct imsg imsg;
1171 struct got_imsg_tag_object *itag;
1172 size_t len, datalen;
1173 const size_t min_datalen =
1174 MIN(sizeof(struct got_imsg_error),
1175 sizeof(struct got_imsg_tag_object));
1177 *tag = NULL;
1179 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1180 if (err)
1181 return err;
1183 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1184 len = 0;
1186 switch (imsg.hdr.type) {
1187 case GOT_IMSG_TAG:
1188 if (datalen < sizeof(*itag)) {
1189 err = got_error(GOT_ERR_PRIVSEP_LEN);
1190 break;
1192 itag = imsg.data;
1193 if (datalen != sizeof(*itag) + itag->tag_len +
1194 itag->tagger_len) {
1195 err = got_error(GOT_ERR_PRIVSEP_LEN);
1196 break;
1198 len += sizeof(*itag);
1200 *tag = calloc(1, sizeof(**tag));
1201 if (*tag == NULL) {
1202 err = got_error_from_errno("calloc");
1203 break;
1206 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1208 if (itag->tag_len == 0) {
1209 (*tag)->tag = strdup("");
1210 if ((*tag)->tag == NULL) {
1211 err = got_error_from_errno("strdup");
1212 break;
1214 } else {
1215 (*tag)->tag = malloc(itag->tag_len + 1);
1216 if ((*tag)->tag == NULL) {
1217 err = got_error_from_errno("malloc");
1218 break;
1220 memcpy((*tag)->tag, imsg.data + len,
1221 itag->tag_len);
1222 (*tag)->tag[itag->tag_len] = '\0';
1224 len += itag->tag_len;
1226 (*tag)->obj_type = itag->obj_type;
1227 (*tag)->tagger_time = itag->tagger_time;
1228 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1230 if (itag->tagger_len == 0) {
1231 (*tag)->tagger = strdup("");
1232 if ((*tag)->tagger == NULL) {
1233 err = got_error_from_errno("strdup");
1234 break;
1236 } else {
1237 (*tag)->tagger = malloc(itag->tagger_len + 1);
1238 if ((*tag)->tagger == NULL) {
1239 err = got_error_from_errno("malloc");
1240 break;
1242 memcpy((*tag)->tagger, imsg.data + len,
1243 itag->tagger_len);
1244 (*tag)->tagger[itag->tagger_len] = '\0';
1246 len += itag->tagger_len;
1248 if (itag->tagmsg_len == 0) {
1249 (*tag)->tagmsg = strdup("");
1250 if ((*tag)->tagmsg == NULL) {
1251 err = got_error_from_errno("strdup");
1252 break;
1254 } else {
1255 size_t offset = 0, remain = itag->tagmsg_len;
1257 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1258 if ((*tag)->tagmsg == NULL) {
1259 err = got_error_from_errno("malloc");
1260 break;
1262 while (remain > 0) {
1263 struct imsg imsg_log;
1264 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1265 remain);
1267 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1268 if (err)
1269 return err;
1271 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1272 return got_error(GOT_ERR_PRIVSEP_MSG);
1274 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1275 n);
1276 imsg_free(&imsg_log);
1277 offset += n;
1278 remain -= n;
1280 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1283 break;
1284 default:
1285 err = got_error(GOT_ERR_PRIVSEP_MSG);
1286 break;
1289 imsg_free(&imsg);
1291 return err;
1294 const struct got_error *
1295 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1296 struct got_packidx *packidx)
1298 const struct got_error *err = NULL;
1299 struct got_imsg_packidx ipackidx;
1300 struct got_imsg_pack ipack;
1301 int fd;
1303 ipackidx.len = packidx->len;
1304 fd = dup(packidx->fd);
1305 if (fd == -1)
1306 return got_error_from_errno("dup");
1308 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1309 sizeof(ipackidx)) == -1) {
1310 err = got_error_from_errno("imsg_compose PACKIDX");
1311 close(fd);
1312 return err;
1315 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1316 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1317 return got_error(GOT_ERR_NO_SPACE);
1318 ipack.filesize = pack->filesize;
1320 fd = dup(pack->fd);
1321 if (fd == -1)
1322 return got_error_from_errno("dup");
1324 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1325 == -1) {
1326 err = got_error_from_errno("imsg_compose PACK");
1327 close(fd);
1328 return err;
1331 return flush_imsg(ibuf);
1334 const struct got_error *
1335 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1336 struct got_object_id *id)
1338 struct got_imsg_packed_object iobj;
1340 iobj.idx = idx;
1341 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1343 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1344 &iobj, sizeof(iobj)) == -1)
1345 return got_error_from_errno("imsg_compose "
1346 "PACKED_OBJECT_REQUEST");
1348 return flush_imsg(ibuf);
1351 const struct got_error *
1352 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1354 const struct got_error *err = NULL;
1356 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1357 NULL, 0) == -1) {
1358 err = got_error_from_errno("imsg_compose "
1359 "GITCONFIG_PARSE_REQUEST");
1360 close(fd);
1361 return err;
1364 return flush_imsg(ibuf);
1367 const struct got_error *
1368 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1370 if (imsg_compose(ibuf,
1371 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1372 NULL, 0) == -1)
1373 return got_error_from_errno("imsg_compose "
1374 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1376 return flush_imsg(ibuf);
1379 const struct got_error *
1380 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1382 if (imsg_compose(ibuf,
1383 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1384 return got_error_from_errno("imsg_compose "
1385 "GITCONFIG_AUTHOR_NAME_REQUEST");
1387 return flush_imsg(ibuf);
1390 const struct got_error *
1391 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1393 if (imsg_compose(ibuf,
1394 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1395 return got_error_from_errno("imsg_compose "
1396 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1398 return flush_imsg(ibuf);
1401 const struct got_error *
1402 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1404 if (imsg_compose(ibuf,
1405 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1406 return got_error_from_errno("imsg_compose "
1407 "GITCONFIG_REMOTE_REQUEST");
1409 return flush_imsg(ibuf);
1412 const struct got_error *
1413 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1415 if (imsg_compose(ibuf,
1416 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1417 return got_error_from_errno("imsg_compose "
1418 "GITCONFIG_OWNER_REQUEST");
1420 return flush_imsg(ibuf);
1423 const struct got_error *
1424 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1426 size_t len = value ? strlen(value) + 1 : 0;
1428 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1429 value, len) == -1)
1430 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1432 return flush_imsg(ibuf);
1435 const struct got_error *
1436 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1438 const struct got_error *err = NULL;
1439 struct imsg imsg;
1440 size_t datalen;
1441 const size_t min_datalen = 0;
1443 *str = NULL;
1445 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1446 if (err)
1447 return err;
1448 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1450 switch (imsg.hdr.type) {
1451 case GOT_IMSG_GITCONFIG_STR_VAL:
1452 if (datalen == 0)
1453 break;
1454 *str = malloc(datalen);
1455 if (*str == NULL) {
1456 err = got_error_from_errno("malloc");
1457 break;
1459 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1460 err = got_error(GOT_ERR_NO_SPACE);
1461 break;
1462 default:
1463 err = got_error(GOT_ERR_PRIVSEP_MSG);
1464 break;
1467 imsg_free(&imsg);
1468 return err;
1471 const struct got_error *
1472 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1474 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1475 &value, sizeof(value)) == -1)
1476 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1478 return flush_imsg(ibuf);
1481 const struct got_error *
1482 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1484 const struct got_error *err = NULL;
1485 struct imsg imsg;
1486 size_t datalen;
1487 const size_t min_datalen =
1488 MIN(sizeof(struct got_imsg_error), sizeof(int));
1490 *val = 0;
1492 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1493 if (err)
1494 return err;
1495 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1497 switch (imsg.hdr.type) {
1498 case GOT_IMSG_GITCONFIG_INT_VAL:
1499 if (datalen != sizeof(*val)) {
1500 err = got_error(GOT_ERR_PRIVSEP_LEN);
1501 break;
1503 memcpy(val, imsg.data, sizeof(*val));
1504 break;
1505 default:
1506 err = got_error(GOT_ERR_PRIVSEP_MSG);
1507 break;
1510 imsg_free(&imsg);
1511 return err;
1514 const struct got_error *
1515 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1516 struct got_remote_repo *remotes, int nremotes)
1518 const struct got_error *err = NULL;
1519 struct got_imsg_remotes iremotes;
1520 int i;
1522 iremotes.nremotes = nremotes;
1523 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1524 &iremotes, sizeof(iremotes)) == -1)
1525 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1527 err = flush_imsg(ibuf);
1528 imsg_clear(ibuf);
1529 if (err)
1530 return err;
1532 for (i = 0; i < nremotes; i++) {
1533 struct got_imsg_remote iremote;
1534 size_t len = sizeof(iremote);
1535 struct ibuf *wbuf;
1537 iremote.name_len = strlen(remotes[i].name);
1538 len += iremote.name_len;
1539 iremote.url_len = strlen(remotes[i].url);
1540 len += iremote.url_len;
1542 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1543 if (wbuf == NULL)
1544 return got_error_from_errno(
1545 "imsg_create GITCONFIG_REMOTE");
1547 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1548 err = got_error_from_errno(
1549 "imsg_add GIITCONFIG_REMOTE");
1550 ibuf_free(wbuf);
1551 return err;
1554 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1555 err = got_error_from_errno(
1556 "imsg_add GIITCONFIG_REMOTE");
1557 ibuf_free(wbuf);
1558 return err;
1560 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1561 err = got_error_from_errno(
1562 "imsg_add GIITCONFIG_REMOTE");
1563 ibuf_free(wbuf);
1564 return err;
1567 wbuf->fd = -1;
1568 imsg_close(ibuf, wbuf);
1569 err = flush_imsg(ibuf);
1570 if (err)
1571 return err;
1574 return NULL;
1577 const struct got_error *
1578 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1579 int *nremotes, struct imsgbuf *ibuf)
1581 const struct got_error *err = NULL;
1582 struct imsg imsg;
1583 size_t datalen;
1584 struct got_imsg_remotes iremotes;
1585 struct got_imsg_remote iremote;
1587 *remotes = NULL;
1588 *nremotes = 0;
1589 iremotes.nremotes = 0;
1591 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1592 if (err)
1593 return err;
1594 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1596 switch (imsg.hdr.type) {
1597 case GOT_IMSG_GITCONFIG_REMOTES:
1598 if (datalen != sizeof(iremotes)) {
1599 err = got_error(GOT_ERR_PRIVSEP_LEN);
1600 break;
1602 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1603 if (iremotes.nremotes == 0) {
1604 imsg_free(&imsg);
1605 return NULL;
1607 break;
1608 default:
1609 imsg_free(&imsg);
1610 return got_error(GOT_ERR_PRIVSEP_MSG);
1613 imsg_free(&imsg);
1615 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1616 if (*remotes == NULL)
1617 return got_error_from_errno("recallocarray");
1619 while (*nremotes < iremotes.nremotes) {
1620 struct got_remote_repo *remote;
1622 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1623 if (err)
1624 break;
1625 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1627 switch (imsg.hdr.type) {
1628 case GOT_IMSG_GITCONFIG_REMOTE:
1629 remote = &(*remotes)[*nremotes];
1630 if (datalen < sizeof(iremote)) {
1631 err = got_error(GOT_ERR_PRIVSEP_LEN);
1632 break;
1634 memcpy(&iremote, imsg.data, sizeof(iremote));
1635 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1636 (sizeof(iremote) + iremote.name_len +
1637 iremote.url_len) > datalen) {
1638 err = got_error(GOT_ERR_PRIVSEP_LEN);
1639 break;
1641 remote->name = strndup(imsg.data + sizeof(iremote),
1642 iremote.name_len);
1643 if (remote->name == NULL) {
1644 err = got_error_from_errno("strndup");
1645 break;
1647 remote->url = strndup(imsg.data + sizeof(iremote) +
1648 iremote.name_len, iremote.url_len);
1649 if (remote->url == NULL) {
1650 err = got_error_from_errno("strndup");
1651 free(remote->name);
1652 break;
1654 (*nremotes)++;
1655 break;
1656 default:
1657 err = got_error(GOT_ERR_PRIVSEP_MSG);
1658 break;
1661 imsg_free(&imsg);
1662 if (err)
1663 break;
1666 if (err) {
1667 int i;
1668 for (i = 0; i < *nremotes; i++) {
1669 free((*remotes)[i].name);
1670 free((*remotes)[i].url);
1672 free(*remotes);
1673 *remotes = NULL;
1674 *nremotes = 0;
1676 return err;
1679 const struct got_error *
1680 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1681 struct got_object_id *id, int idx, const char *path)
1683 const struct got_error *err = NULL;
1684 struct ibuf *wbuf;
1685 size_t path_len = strlen(path) + 1;
1687 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1688 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1689 if (wbuf == NULL)
1690 return got_error_from_errno(
1691 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1692 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1693 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1694 ibuf_free(wbuf);
1695 return err;
1697 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1698 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1699 ibuf_free(wbuf);
1700 return err;
1702 if (imsg_add(wbuf, path, path_len) == -1) {
1703 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1704 ibuf_free(wbuf);
1705 return err;
1708 wbuf->fd = -1;
1709 imsg_close(ibuf, wbuf);
1711 return flush_imsg(ibuf);
1714 const struct got_error *
1715 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1716 size_t ncommits, struct imsgbuf *ibuf)
1718 const struct got_error *err;
1719 struct ibuf *wbuf;
1720 int i;
1722 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1723 sizeof(struct got_imsg_traversed_commits) +
1724 ncommits * SHA1_DIGEST_LENGTH);
1725 if (wbuf == NULL)
1726 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1728 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1729 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1730 ibuf_free(wbuf);
1731 return err;
1733 for (i = 0; i < ncommits; i++) {
1734 struct got_object_id *id = &commit_ids[i];
1735 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1736 err = got_error_from_errno(
1737 "imsg_add TRAVERSED_COMMITS");
1738 ibuf_free(wbuf);
1739 return err;
1743 wbuf->fd = -1;
1744 imsg_close(ibuf, wbuf);
1746 return flush_imsg(ibuf);
1749 const struct got_error *
1750 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1751 struct got_object_id **changed_commit_id,
1752 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1754 const struct got_error *err = NULL;
1755 struct imsg imsg;
1756 struct got_imsg_traversed_commits *icommits;
1757 size_t datalen;
1758 int i, done = 0;
1760 *changed_commit = NULL;
1761 *changed_commit_id = NULL;
1763 while (!done) {
1764 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1765 if (err)
1766 return err;
1768 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1769 switch (imsg.hdr.type) {
1770 case GOT_IMSG_TRAVERSED_COMMITS:
1771 icommits = imsg.data;
1772 if (datalen != sizeof(*icommits) +
1773 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1774 err = got_error(GOT_ERR_PRIVSEP_LEN);
1775 break;
1777 for (i = 0; i < icommits->ncommits; i++) {
1778 struct got_object_qid *qid;
1779 uint8_t *sha1 = (uint8_t *)imsg.data +
1780 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1781 err = got_object_qid_alloc_partial(&qid);
1782 if (err)
1783 break;
1784 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1785 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1787 /* The last commit may contain a change. */
1788 if (i == icommits->ncommits - 1) {
1789 *changed_commit_id =
1790 got_object_id_dup(qid->id);
1791 if (*changed_commit_id == NULL) {
1792 err = got_error_from_errno(
1793 "got_object_id_dup");
1794 break;
1798 break;
1799 case GOT_IMSG_COMMIT:
1800 if (*changed_commit_id == NULL) {
1801 err = got_error(GOT_ERR_PRIVSEP_MSG);
1802 break;
1804 err = get_commit_from_imsg(changed_commit, &imsg,
1805 datalen, ibuf);
1806 break;
1807 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1808 done = 1;
1809 break;
1810 default:
1811 err = got_error(GOT_ERR_PRIVSEP_MSG);
1812 break;
1815 imsg_free(&imsg);
1816 if (err)
1817 break;
1820 if (err)
1821 got_object_id_queue_free(commit_ids);
1822 return err;
1825 const struct got_error *
1826 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1828 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1829 NULL, 0) == -1)
1830 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1832 return flush_imsg(ibuf);
1835 const struct got_error *
1836 got_privsep_unveil_exec_helpers(void)
1838 const char *helpers[] = {
1839 GOT_PATH_PROG_READ_PACK,
1840 GOT_PATH_PROG_READ_OBJECT,
1841 GOT_PATH_PROG_READ_COMMIT,
1842 GOT_PATH_PROG_READ_TREE,
1843 GOT_PATH_PROG_READ_BLOB,
1844 GOT_PATH_PROG_READ_TAG,
1845 GOT_PATH_PROG_READ_GITCONFIG,
1847 int i;
1849 for (i = 0; i < nitems(helpers); i++) {
1850 if (unveil(helpers[i], "x") == 0)
1851 continue;
1852 return got_error_from_errno2("unveil", helpers[i]);
1855 return NULL;
1858 void
1859 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1861 if (close(imsg_fds[0]) != 0) {
1862 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1863 _exit(1);
1866 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1867 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1868 _exit(1);
1870 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1871 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1872 _exit(1);
1875 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1876 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1877 strerror(errno));
1878 _exit(1);