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_obj_req(struct imsgbuf *ibuf, int fd)
236 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
237 == -1)
238 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
240 return flush_imsg(ibuf);
243 const struct got_error *
244 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
245 struct got_object_id *id, int pack_idx)
247 const struct got_error *err = NULL;
248 struct got_imsg_packed_object iobj, *iobjp;
249 size_t len;
251 if (id) { /* commit is packed */
252 iobj.idx = pack_idx;
253 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
254 iobjp = &iobj;
255 len = sizeof(iobj);
256 } else {
257 iobjp = NULL;
258 len = 0;
261 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
262 == -1) {
263 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
264 close(fd);
265 return err;
268 return flush_imsg(ibuf);
271 const struct got_error *
272 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
273 struct got_object_id *id, int pack_idx)
275 const struct got_error *err = NULL;
276 struct ibuf *wbuf;
277 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
279 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
280 if (wbuf == NULL)
281 return got_error_from_errno("imsg_create TREE_REQUEST");
283 if (id) { /* tree is packed */
284 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
285 err = got_error_from_errno("imsg_add TREE_ENTRY");
286 ibuf_free(wbuf);
287 return err;
290 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
291 err = got_error_from_errno("imsg_add TREE_ENTRY");
292 ibuf_free(wbuf);
293 return err;
297 wbuf->fd = fd;
298 imsg_close(ibuf, wbuf);
300 return flush_imsg(ibuf);
303 const struct got_error *
304 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
305 struct got_object_id *id, int pack_idx)
307 struct got_imsg_packed_object iobj, *iobjp;
308 size_t len;
310 if (id) { /* tag is packed */
311 iobj.idx = pack_idx;
312 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
313 iobjp = &iobj;
314 len = sizeof(iobj);
315 } else {
316 iobjp = NULL;
317 len = 0;
320 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
321 == -1)
322 return got_error_from_errno("imsg_compose TAG_REQUEST");
324 return flush_imsg(ibuf);
327 const struct got_error *
328 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
329 struct got_object_id *id, int pack_idx)
331 const struct got_error *err = NULL;
332 struct got_imsg_packed_object iobj, *iobjp;
333 size_t len;
335 if (id) { /* blob 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_BLOB_REQUEST, 0, 0, infd, iobjp, len)
346 == -1) {
347 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
348 close(infd);
349 return err;
352 return flush_imsg(ibuf);
355 const struct got_error *
356 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
358 const struct got_error *err = NULL;
360 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
361 == -1) {
362 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
363 close(outfd);
364 return err;
367 return flush_imsg(ibuf);
370 const struct got_error *
371 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
373 const struct got_error *err = NULL;
375 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
376 == -1) {
377 err = got_error_from_errno("imsg_compose TMPFD");
378 close(fd);
379 return err;
382 return flush_imsg(ibuf);
385 const struct got_error *
386 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
388 struct got_imsg_object iobj;
390 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
391 iobj.type = obj->type;
392 iobj.flags = obj->flags;
393 iobj.hdrlen = obj->hdrlen;
394 iobj.size = obj->size;
395 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
396 iobj.pack_offset = obj->pack_offset;
397 iobj.pack_idx = obj->pack_idx;
400 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
401 == -1)
402 return got_error_from_errno("imsg_compose OBJECT");
404 return flush_imsg(ibuf);
407 const struct got_error *
408 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd)
410 const struct got_error *err = NULL;
412 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
413 NULL, 0) == -1) {
414 err = got_error_from_errno("imsg_compose FETCH_REQUEST");
415 close(fd);
416 return err;
418 return flush_imsg(ibuf);
421 const struct got_error *
422 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
424 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
425 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
426 return got_error_from_errno("imsg_compose FETCH");
427 return flush_imsg(ibuf);
430 const struct got_error *
431 got_privsep_wait_fetch_done(struct imsgbuf *ibuf, struct got_object_id *hash)
433 const struct got_error *err = NULL;
434 struct imsg imsg;
436 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
437 if (err)
438 return err;
439 if (imsg.hdr.type == GOT_IMSG_FETCH_DONE &&
440 imsg.hdr.len - sizeof(imsg.hdr) == SHA1_DIGEST_LENGTH)
441 return NULL;
442 else
443 return got_error(GOT_ERR_PRIVSEP_MSG);
444 imsg_free(&imsg);
448 const struct got_error *
449 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id hash)
451 const struct got_error *err = NULL;
453 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
454 hash.sha1, SHA1_DIGEST_LENGTH) == -1) {
455 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
456 close(fd);
457 return err;
459 return flush_imsg(ibuf);
462 const struct got_error *
463 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
465 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
466 return got_error_from_errno("imsg_compose FETCH");
467 return flush_imsg(ibuf);
470 const struct got_error *
471 got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
473 const struct got_error *err = NULL;
474 struct imsg imsg;
476 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
477 if (err)
478 return err;
479 if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
480 return NULL;
481 else
482 return got_error(GOT_ERR_PRIVSEP_MSG);
483 imsg_free(&imsg);
486 const struct got_error *
487 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
488 struct imsgbuf *ibuf)
490 const struct got_error *err = NULL;
491 struct got_imsg_object *iobj;
492 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
494 if (datalen != sizeof(*iobj))
495 return got_error(GOT_ERR_PRIVSEP_LEN);
496 iobj = imsg->data;
498 *obj = calloc(1, sizeof(**obj));
499 if (*obj == NULL)
500 return got_error_from_errno("calloc");
502 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
503 (*obj)->type = iobj->type;
504 (*obj)->flags = iobj->flags;
505 (*obj)->hdrlen = iobj->hdrlen;
506 (*obj)->size = iobj->size;
507 /* path_packfile is handled by caller */
508 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
509 (*obj)->pack_offset = iobj->pack_offset;
510 (*obj)->pack_idx = iobj->pack_idx;
513 return err;
516 const struct got_error *
517 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
519 const struct got_error *err = NULL;
520 struct imsg imsg;
521 const size_t min_datalen =
522 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
524 *obj = NULL;
526 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
527 if (err)
528 return err;
530 switch (imsg.hdr.type) {
531 case GOT_IMSG_OBJECT:
532 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
533 break;
534 default:
535 err = got_error(GOT_ERR_PRIVSEP_MSG);
536 break;
539 imsg_free(&imsg);
541 return err;
544 static const struct got_error *
545 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
546 size_t logmsg_len)
548 const struct got_error *err = NULL;
549 size_t offset, remain;
551 offset = 0;
552 remain = logmsg_len;
553 while (remain > 0) {
554 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
556 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
557 commit->logmsg + offset, n) == -1) {
558 err = got_error_from_errno("imsg_compose "
559 "COMMIT_LOGMSG");
560 break;
563 err = flush_imsg(ibuf);
564 if (err)
565 break;
567 offset += n;
568 remain -= n;
571 return err;
574 const struct got_error *
575 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
577 const struct got_error *err = NULL;
578 struct got_imsg_commit_object *icommit;
579 uint8_t *buf;
580 size_t len, total;
581 struct got_object_qid *qid;
582 size_t author_len = strlen(commit->author);
583 size_t committer_len = strlen(commit->committer);
584 size_t logmsg_len = strlen(commit->logmsg);
586 total = sizeof(*icommit) + author_len + committer_len +
587 commit->nparents * SHA1_DIGEST_LENGTH;
589 buf = malloc(total);
590 if (buf == NULL)
591 return got_error_from_errno("malloc");
593 icommit = (struct got_imsg_commit_object *)buf;
594 memcpy(icommit->tree_id, commit->tree_id->sha1,
595 sizeof(icommit->tree_id));
596 icommit->author_len = author_len;
597 icommit->author_time = commit->author_time;
598 icommit->author_gmtoff = commit->author_gmtoff;
599 icommit->committer_len = committer_len;
600 icommit->committer_time = commit->committer_time;
601 icommit->committer_gmtoff = commit->committer_gmtoff;
602 icommit->logmsg_len = logmsg_len;
603 icommit->nparents = commit->nparents;
605 len = sizeof(*icommit);
606 memcpy(buf + len, commit->author, author_len);
607 len += author_len;
608 memcpy(buf + len, commit->committer, committer_len);
609 len += committer_len;
610 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
611 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
612 len += SHA1_DIGEST_LENGTH;
615 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
616 err = got_error_from_errno("imsg_compose COMMIT");
617 goto done;
620 if (logmsg_len == 0 ||
621 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
622 err = flush_imsg(ibuf);
623 if (err)
624 goto done;
626 err = send_commit_logmsg(ibuf, commit, logmsg_len);
627 done:
628 free(buf);
629 return err;
632 static const struct got_error *
633 get_commit_from_imsg(struct got_commit_object **commit,
634 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
636 const struct got_error *err = NULL;
637 struct got_imsg_commit_object *icommit;
638 size_t len = 0;
639 int i;
641 if (datalen < sizeof(*icommit))
642 return got_error(GOT_ERR_PRIVSEP_LEN);
644 icommit = imsg->data;
645 if (datalen != sizeof(*icommit) + icommit->author_len +
646 icommit->committer_len +
647 icommit->nparents * SHA1_DIGEST_LENGTH)
648 return got_error(GOT_ERR_PRIVSEP_LEN);
650 if (icommit->nparents < 0)
651 return got_error(GOT_ERR_PRIVSEP_LEN);
653 len += sizeof(*icommit);
655 *commit = got_object_commit_alloc_partial();
656 if (*commit == NULL)
657 return got_error_from_errno(
658 "got_object_commit_alloc_partial");
660 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
661 SHA1_DIGEST_LENGTH);
662 (*commit)->author_time = icommit->author_time;
663 (*commit)->author_gmtoff = icommit->author_gmtoff;
664 (*commit)->committer_time = icommit->committer_time;
665 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
667 if (icommit->author_len == 0) {
668 (*commit)->author = strdup("");
669 if ((*commit)->author == NULL) {
670 err = got_error_from_errno("strdup");
671 goto done;
673 } else {
674 (*commit)->author = malloc(icommit->author_len + 1);
675 if ((*commit)->author == NULL) {
676 err = got_error_from_errno("malloc");
677 goto done;
679 memcpy((*commit)->author, imsg->data + len,
680 icommit->author_len);
681 (*commit)->author[icommit->author_len] = '\0';
683 len += icommit->author_len;
685 if (icommit->committer_len == 0) {
686 (*commit)->committer = strdup("");
687 if ((*commit)->committer == NULL) {
688 err = got_error_from_errno("strdup");
689 goto done;
691 } else {
692 (*commit)->committer =
693 malloc(icommit->committer_len + 1);
694 if ((*commit)->committer == NULL) {
695 err = got_error_from_errno("malloc");
696 goto done;
698 memcpy((*commit)->committer, imsg->data + len,
699 icommit->committer_len);
700 (*commit)->committer[icommit->committer_len] = '\0';
702 len += icommit->committer_len;
704 if (icommit->logmsg_len == 0) {
705 (*commit)->logmsg = strdup("");
706 if ((*commit)->logmsg == NULL) {
707 err = got_error_from_errno("strdup");
708 goto done;
710 } else {
711 size_t offset = 0, remain = icommit->logmsg_len;
713 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
714 if ((*commit)->logmsg == NULL) {
715 err = got_error_from_errno("malloc");
716 goto done;
718 while (remain > 0) {
719 struct imsg imsg_log;
720 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
721 remain);
723 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
724 if (err)
725 goto done;
727 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
728 err = got_error(GOT_ERR_PRIVSEP_MSG);
729 goto done;
732 memcpy((*commit)->logmsg + offset,
733 imsg_log.data, n);
734 imsg_free(&imsg_log);
735 offset += n;
736 remain -= n;
738 (*commit)->logmsg[icommit->logmsg_len] = '\0';
741 for (i = 0; i < icommit->nparents; i++) {
742 struct got_object_qid *qid;
744 err = got_object_qid_alloc_partial(&qid);
745 if (err)
746 break;
747 memcpy(qid->id, imsg->data + len +
748 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
749 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
750 (*commit)->nparents++;
752 done:
753 if (err) {
754 got_object_commit_close(*commit);
755 *commit = NULL;
757 return err;
760 const struct got_error *
761 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
763 const struct got_error *err = NULL;
764 struct imsg imsg;
765 size_t datalen;
766 const size_t min_datalen =
767 MIN(sizeof(struct got_imsg_error),
768 sizeof(struct got_imsg_commit_object));
770 *commit = NULL;
772 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
773 if (err)
774 return err;
776 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
778 switch (imsg.hdr.type) {
779 case GOT_IMSG_COMMIT:
780 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
781 break;
782 default:
783 err = got_error(GOT_ERR_PRIVSEP_MSG);
784 break;
787 imsg_free(&imsg);
789 return err;
792 const struct got_error *
793 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
794 int nentries)
796 const struct got_error *err = NULL;
797 struct got_imsg_tree_object itree;
798 struct got_pathlist_entry *pe;
799 size_t totlen;
800 int nimsg; /* number of imsg queued in ibuf */
802 itree.nentries = nentries;
803 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
804 == -1)
805 return got_error_from_errno("imsg_compose TREE");
807 totlen = sizeof(itree);
808 nimsg = 1;
809 TAILQ_FOREACH(pe, entries, entry) {
810 const char *name = pe->path;
811 struct got_parsed_tree_entry *pte = pe->data;
812 struct ibuf *wbuf;
813 size_t namelen = strlen(name);
814 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
816 if (len > MAX_IMSGSIZE)
817 return got_error(GOT_ERR_NO_SPACE);
819 nimsg++;
820 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
821 err = flush_imsg(ibuf);
822 if (err)
823 return err;
824 nimsg = 0;
827 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
828 if (wbuf == NULL)
829 return got_error_from_errno("imsg_create TREE_ENTRY");
831 /* Keep in sync with struct got_imsg_tree_object definition! */
832 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
833 err = got_error_from_errno("imsg_add TREE_ENTRY");
834 ibuf_free(wbuf);
835 return err;
837 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
838 err = got_error_from_errno("imsg_add TREE_ENTRY");
839 ibuf_free(wbuf);
840 return err;
843 if (imsg_add(wbuf, name, namelen) == -1) {
844 err = got_error_from_errno("imsg_add TREE_ENTRY");
845 ibuf_free(wbuf);
846 return err;
849 wbuf->fd = -1;
850 imsg_close(ibuf, wbuf);
852 totlen += len;
855 return flush_imsg(ibuf);
858 const struct got_error *
859 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
861 const struct got_error *err = NULL;
862 const size_t min_datalen =
863 MIN(sizeof(struct got_imsg_error),
864 sizeof(struct got_imsg_tree_object));
865 struct got_imsg_tree_object *itree;
866 int nentries = 0;
868 *tree = NULL;
869 get_more:
870 err = read_imsg(ibuf);
871 if (err)
872 goto done;
874 for (;;) {
875 struct imsg imsg;
876 size_t n;
877 size_t datalen;
878 struct got_imsg_tree_entry *ite;
879 struct got_tree_entry *te = NULL;
881 n = imsg_get(ibuf, &imsg);
882 if (n == 0) {
883 if (*tree && (*tree)->nentries != nentries)
884 goto get_more;
885 break;
888 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
889 return got_error(GOT_ERR_PRIVSEP_LEN);
891 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
893 switch (imsg.hdr.type) {
894 case GOT_IMSG_ERROR:
895 err = recv_imsg_error(&imsg, datalen);
896 break;
897 case GOT_IMSG_TREE:
898 /* This message should only appear once. */
899 if (*tree != NULL) {
900 err = got_error(GOT_ERR_PRIVSEP_MSG);
901 break;
903 if (datalen != sizeof(*itree)) {
904 err = got_error(GOT_ERR_PRIVSEP_LEN);
905 break;
907 itree = imsg.data;
908 *tree = malloc(sizeof(**tree));
909 if (*tree == NULL) {
910 err = got_error_from_errno("malloc");
911 break;
913 (*tree)->entries = calloc(itree->nentries,
914 sizeof(struct got_tree_entry));
915 if ((*tree)->entries == NULL) {
916 err = got_error_from_errno("malloc");
917 break;
919 (*tree)->nentries = itree->nentries;
920 (*tree)->refcnt = 0;
921 break;
922 case GOT_IMSG_TREE_ENTRY:
923 /* This message should be preceeded by GOT_IMSG_TREE. */
924 if (*tree == NULL) {
925 err = got_error(GOT_ERR_PRIVSEP_MSG);
926 break;
928 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
929 err = got_error(GOT_ERR_PRIVSEP_LEN);
930 break;
933 /* Remaining data contains the entry's name. */
934 datalen -= sizeof(*ite);
935 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
936 err = got_error(GOT_ERR_PRIVSEP_LEN);
937 break;
939 ite = imsg.data;
941 if (datalen + 1 > sizeof(te->name)) {
942 err = got_error(GOT_ERR_NO_SPACE);
943 break;
945 te = &(*tree)->entries[nentries];
946 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
947 te->name[datalen] = '\0';
949 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
950 te->mode = ite->mode;
951 te->idx = nentries;
952 nentries++;
953 break;
954 default:
955 err = got_error(GOT_ERR_PRIVSEP_MSG);
956 break;
959 imsg_free(&imsg);
961 done:
962 if (*tree && (*tree)->nentries != nentries) {
963 if (err == NULL)
964 err = got_error(GOT_ERR_PRIVSEP_LEN);
965 got_object_tree_close(*tree);
966 *tree = NULL;
969 return err;
972 const struct got_error *
973 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
974 const uint8_t *data)
976 struct got_imsg_blob iblob;
978 iblob.size = size;
979 iblob.hdrlen = hdrlen;
981 if (data) {
982 uint8_t *buf;
984 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
985 return got_error(GOT_ERR_NO_SPACE);
987 buf = malloc(sizeof(iblob) + size);
988 if (buf == NULL)
989 return got_error_from_errno("malloc");
991 memcpy(buf, &iblob, sizeof(iblob));
992 memcpy(buf + sizeof(iblob), data, size);
993 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
994 sizeof(iblob) + size) == -1) {
995 free(buf);
996 return got_error_from_errno("imsg_compose BLOB");
998 free(buf);
999 } else {
1000 /* Data has already been written to file descriptor. */
1001 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1002 sizeof(iblob)) == -1)
1003 return got_error_from_errno("imsg_compose BLOB");
1007 return flush_imsg(ibuf);
1010 const struct got_error *
1011 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1012 struct imsgbuf *ibuf)
1014 const struct got_error *err = NULL;
1015 struct imsg imsg;
1016 struct got_imsg_blob *iblob;
1017 size_t datalen;
1019 *outbuf = NULL;
1021 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1022 if (err)
1023 return err;
1025 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1027 switch (imsg.hdr.type) {
1028 case GOT_IMSG_BLOB:
1029 if (datalen < sizeof(*iblob)) {
1030 err = got_error(GOT_ERR_PRIVSEP_LEN);
1031 break;
1033 iblob = imsg.data;
1034 *size = iblob->size;
1035 *hdrlen = iblob->hdrlen;
1037 if (datalen == sizeof(*iblob)) {
1038 /* Data has been written to file descriptor. */
1039 break;
1042 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1043 err = got_error(GOT_ERR_PRIVSEP_LEN);
1044 break;
1047 *outbuf = malloc(*size);
1048 if (*outbuf == NULL) {
1049 err = got_error_from_errno("malloc");
1050 break;
1052 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1053 break;
1054 default:
1055 err = got_error(GOT_ERR_PRIVSEP_MSG);
1056 break;
1059 imsg_free(&imsg);
1061 return err;
1064 static const struct got_error *
1065 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1067 const struct got_error *err = NULL;
1068 size_t offset, remain;
1070 offset = 0;
1071 remain = tagmsg_len;
1072 while (remain > 0) {
1073 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1075 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1076 tag->tagmsg + offset, n) == -1) {
1077 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1078 break;
1081 err = flush_imsg(ibuf);
1082 if (err)
1083 break;
1085 offset += n;
1086 remain -= n;
1089 return err;
1092 const struct got_error *
1093 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1095 const struct got_error *err = NULL;
1096 struct got_imsg_tag_object *itag;
1097 uint8_t *buf;
1098 size_t len, total;
1099 size_t tag_len = strlen(tag->tag);
1100 size_t tagger_len = strlen(tag->tagger);
1101 size_t tagmsg_len = strlen(tag->tagmsg);
1103 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1105 buf = malloc(total);
1106 if (buf == NULL)
1107 return got_error_from_errno("malloc");
1109 itag = (struct got_imsg_tag_object *)buf;
1110 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1111 itag->obj_type = tag->obj_type;
1112 itag->tag_len = tag_len;
1113 itag->tagger_len = tagger_len;
1114 itag->tagger_time = tag->tagger_time;
1115 itag->tagger_gmtoff = tag->tagger_gmtoff;
1116 itag->tagmsg_len = tagmsg_len;
1118 len = sizeof(*itag);
1119 memcpy(buf + len, tag->tag, tag_len);
1120 len += tag_len;
1121 memcpy(buf + len, tag->tagger, tagger_len);
1122 len += tagger_len;
1124 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1125 err = got_error_from_errno("imsg_compose TAG");
1126 goto done;
1129 if (tagmsg_len == 0 ||
1130 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1131 err = flush_imsg(ibuf);
1132 if (err)
1133 goto done;
1135 err = send_tagmsg(ibuf, tag, tagmsg_len);
1136 done:
1137 free(buf);
1138 return err;
1141 const struct got_error *
1142 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1144 const struct got_error *err = NULL;
1145 struct imsg imsg;
1146 struct got_imsg_tag_object *itag;
1147 size_t len, datalen;
1148 const size_t min_datalen =
1149 MIN(sizeof(struct got_imsg_error),
1150 sizeof(struct got_imsg_tag_object));
1152 *tag = NULL;
1154 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1155 if (err)
1156 return err;
1158 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1159 len = 0;
1161 switch (imsg.hdr.type) {
1162 case GOT_IMSG_TAG:
1163 if (datalen < sizeof(*itag)) {
1164 err = got_error(GOT_ERR_PRIVSEP_LEN);
1165 break;
1167 itag = imsg.data;
1168 if (datalen != sizeof(*itag) + itag->tag_len +
1169 itag->tagger_len) {
1170 err = got_error(GOT_ERR_PRIVSEP_LEN);
1171 break;
1173 len += sizeof(*itag);
1175 *tag = calloc(1, sizeof(**tag));
1176 if (*tag == NULL) {
1177 err = got_error_from_errno("calloc");
1178 break;
1181 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1183 if (itag->tag_len == 0) {
1184 (*tag)->tag = strdup("");
1185 if ((*tag)->tag == NULL) {
1186 err = got_error_from_errno("strdup");
1187 break;
1189 } else {
1190 (*tag)->tag = malloc(itag->tag_len + 1);
1191 if ((*tag)->tag == NULL) {
1192 err = got_error_from_errno("malloc");
1193 break;
1195 memcpy((*tag)->tag, imsg.data + len,
1196 itag->tag_len);
1197 (*tag)->tag[itag->tag_len] = '\0';
1199 len += itag->tag_len;
1201 (*tag)->obj_type = itag->obj_type;
1202 (*tag)->tagger_time = itag->tagger_time;
1203 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1205 if (itag->tagger_len == 0) {
1206 (*tag)->tagger = strdup("");
1207 if ((*tag)->tagger == NULL) {
1208 err = got_error_from_errno("strdup");
1209 break;
1211 } else {
1212 (*tag)->tagger = malloc(itag->tagger_len + 1);
1213 if ((*tag)->tagger == NULL) {
1214 err = got_error_from_errno("malloc");
1215 break;
1217 memcpy((*tag)->tagger, imsg.data + len,
1218 itag->tagger_len);
1219 (*tag)->tagger[itag->tagger_len] = '\0';
1221 len += itag->tagger_len;
1223 if (itag->tagmsg_len == 0) {
1224 (*tag)->tagmsg = strdup("");
1225 if ((*tag)->tagmsg == NULL) {
1226 err = got_error_from_errno("strdup");
1227 break;
1229 } else {
1230 size_t offset = 0, remain = itag->tagmsg_len;
1232 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1233 if ((*tag)->tagmsg == NULL) {
1234 err = got_error_from_errno("malloc");
1235 break;
1237 while (remain > 0) {
1238 struct imsg imsg_log;
1239 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1240 remain);
1242 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1243 if (err)
1244 return err;
1246 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1247 return got_error(GOT_ERR_PRIVSEP_MSG);
1249 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1250 n);
1251 imsg_free(&imsg_log);
1252 offset += n;
1253 remain -= n;
1255 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1258 break;
1259 default:
1260 err = got_error(GOT_ERR_PRIVSEP_MSG);
1261 break;
1264 imsg_free(&imsg);
1266 return err;
1269 const struct got_error *
1270 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1271 struct got_packidx *packidx)
1273 const struct got_error *err = NULL;
1274 struct got_imsg_packidx ipackidx;
1275 struct got_imsg_pack ipack;
1276 int fd;
1278 ipackidx.len = packidx->len;
1279 fd = dup(packidx->fd);
1280 if (fd == -1)
1281 return got_error_from_errno("dup");
1283 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1284 sizeof(ipackidx)) == -1) {
1285 err = got_error_from_errno("imsg_compose PACKIDX");
1286 close(fd);
1287 return err;
1290 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1291 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1292 return got_error(GOT_ERR_NO_SPACE);
1293 ipack.filesize = pack->filesize;
1295 fd = dup(pack->fd);
1296 if (fd == -1)
1297 return got_error_from_errno("dup");
1299 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1300 == -1) {
1301 err = got_error_from_errno("imsg_compose PACK");
1302 close(fd);
1303 return err;
1306 return flush_imsg(ibuf);
1309 const struct got_error *
1310 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1311 struct got_object_id *id)
1313 struct got_imsg_packed_object iobj;
1315 iobj.idx = idx;
1316 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1318 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1319 &iobj, sizeof(iobj)) == -1)
1320 return got_error_from_errno("imsg_compose "
1321 "PACKED_OBJECT_REQUEST");
1323 return flush_imsg(ibuf);
1326 const struct got_error *
1327 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1329 const struct got_error *err = NULL;
1331 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1332 NULL, 0) == -1) {
1333 err = got_error_from_errno("imsg_compose "
1334 "GITCONFIG_PARSE_REQUEST");
1335 close(fd);
1336 return err;
1339 return flush_imsg(ibuf);
1342 const struct got_error *
1343 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1345 if (imsg_compose(ibuf,
1346 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1347 NULL, 0) == -1)
1348 return got_error_from_errno("imsg_compose "
1349 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1351 return flush_imsg(ibuf);
1354 const struct got_error *
1355 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1357 if (imsg_compose(ibuf,
1358 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1359 return got_error_from_errno("imsg_compose "
1360 "GITCONFIG_AUTHOR_NAME_REQUEST");
1362 return flush_imsg(ibuf);
1365 const struct got_error *
1366 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1368 if (imsg_compose(ibuf,
1369 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1370 return got_error_from_errno("imsg_compose "
1371 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1373 return flush_imsg(ibuf);
1376 const struct got_error *
1377 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1379 if (imsg_compose(ibuf,
1380 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1381 return got_error_from_errno("imsg_compose "
1382 "GITCONFIG_REMOTE_REQUEST");
1384 return flush_imsg(ibuf);
1387 const struct got_error *
1388 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1390 if (imsg_compose(ibuf,
1391 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1392 return got_error_from_errno("imsg_compose "
1393 "GITCONFIG_OWNER_REQUEST");
1395 return flush_imsg(ibuf);
1398 const struct got_error *
1399 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1401 size_t len = value ? strlen(value) + 1 : 0;
1403 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1404 value, len) == -1)
1405 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1407 return flush_imsg(ibuf);
1410 const struct got_error *
1411 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1413 const struct got_error *err = NULL;
1414 struct imsg imsg;
1415 size_t datalen;
1416 const size_t min_datalen = 0;
1418 *str = NULL;
1420 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1421 if (err)
1422 return err;
1423 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1425 switch (imsg.hdr.type) {
1426 case GOT_IMSG_GITCONFIG_STR_VAL:
1427 if (datalen == 0)
1428 break;
1429 *str = malloc(datalen);
1430 if (*str == NULL) {
1431 err = got_error_from_errno("malloc");
1432 break;
1434 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1435 err = got_error(GOT_ERR_NO_SPACE);
1436 break;
1437 default:
1438 err = got_error(GOT_ERR_PRIVSEP_MSG);
1439 break;
1442 imsg_free(&imsg);
1443 return err;
1446 const struct got_error *
1447 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1449 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1450 &value, sizeof(value)) == -1)
1451 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1453 return flush_imsg(ibuf);
1456 const struct got_error *
1457 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1459 const struct got_error *err = NULL;
1460 struct imsg imsg;
1461 size_t datalen;
1462 const size_t min_datalen =
1463 MIN(sizeof(struct got_imsg_error), sizeof(int));
1465 *val = 0;
1467 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1468 if (err)
1469 return err;
1470 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1472 switch (imsg.hdr.type) {
1473 case GOT_IMSG_GITCONFIG_INT_VAL:
1474 if (datalen != sizeof(*val)) {
1475 err = got_error(GOT_ERR_PRIVSEP_LEN);
1476 break;
1478 memcpy(val, imsg.data, sizeof(*val));
1479 break;
1480 default:
1481 err = got_error(GOT_ERR_PRIVSEP_MSG);
1482 break;
1485 imsg_free(&imsg);
1486 return err;
1489 const struct got_error *
1490 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1491 struct got_remote_repo *remotes, int nremotes)
1493 const struct got_error *err = NULL;
1494 struct got_imsg_remotes iremotes;
1495 int i;
1497 iremotes.nremotes = nremotes;
1498 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1499 &iremotes, sizeof(iremotes)) == -1)
1500 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1502 err = flush_imsg(ibuf);
1503 imsg_clear(ibuf);
1504 if (err)
1505 return err;
1507 for (i = 0; i < nremotes; i++) {
1508 struct got_imsg_remote iremote;
1509 size_t len = sizeof(iremote);
1510 struct ibuf *wbuf;
1512 iremote.name_len = strlen(remotes[i].name);
1513 len += iremote.name_len;
1514 iremote.url_len = strlen(remotes[i].url);
1515 len += iremote.url_len;
1517 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1518 if (wbuf == NULL)
1519 return got_error_from_errno(
1520 "imsg_create GITCONFIG_REMOTE");
1522 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1523 err = got_error_from_errno(
1524 "imsg_add GIITCONFIG_REMOTE");
1525 ibuf_free(wbuf);
1526 return err;
1529 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1530 err = got_error_from_errno(
1531 "imsg_add GIITCONFIG_REMOTE");
1532 ibuf_free(wbuf);
1533 return err;
1535 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1536 err = got_error_from_errno(
1537 "imsg_add GIITCONFIG_REMOTE");
1538 ibuf_free(wbuf);
1539 return err;
1542 wbuf->fd = -1;
1543 imsg_close(ibuf, wbuf);
1544 err = flush_imsg(ibuf);
1545 if (err)
1546 return err;
1549 return NULL;
1552 const struct got_error *
1553 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1554 int *nremotes, struct imsgbuf *ibuf)
1556 const struct got_error *err = NULL;
1557 struct imsg imsg;
1558 size_t datalen;
1559 struct got_imsg_remotes iremotes;
1560 struct got_imsg_remote iremote;
1562 *remotes = NULL;
1563 *nremotes = 0;
1564 iremotes.nremotes = 0;
1566 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1567 if (err)
1568 return err;
1569 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1571 switch (imsg.hdr.type) {
1572 case GOT_IMSG_GITCONFIG_REMOTES:
1573 if (datalen != sizeof(iremotes)) {
1574 err = got_error(GOT_ERR_PRIVSEP_LEN);
1575 break;
1577 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1578 if (iremotes.nremotes == 0) {
1579 imsg_free(&imsg);
1580 return NULL;
1582 break;
1583 default:
1584 imsg_free(&imsg);
1585 return got_error(GOT_ERR_PRIVSEP_MSG);
1588 imsg_free(&imsg);
1590 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1591 if (*remotes == NULL)
1592 return got_error_from_errno("recallocarray");
1594 while (*nremotes < iremotes.nremotes) {
1595 struct got_remote_repo *remote;
1597 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1598 if (err)
1599 break;
1600 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1602 switch (imsg.hdr.type) {
1603 case GOT_IMSG_GITCONFIG_REMOTE:
1604 remote = &(*remotes)[*nremotes];
1605 if (datalen < sizeof(iremote)) {
1606 err = got_error(GOT_ERR_PRIVSEP_LEN);
1607 break;
1609 memcpy(&iremote, imsg.data, sizeof(iremote));
1610 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1611 (sizeof(iremote) + iremote.name_len +
1612 iremote.url_len) > datalen) {
1613 err = got_error(GOT_ERR_PRIVSEP_LEN);
1614 break;
1616 remote->name = strndup(imsg.data + sizeof(iremote),
1617 iremote.name_len);
1618 if (remote->name == NULL) {
1619 err = got_error_from_errno("strndup");
1620 break;
1622 remote->url = strndup(imsg.data + sizeof(iremote) +
1623 iremote.name_len, iremote.url_len);
1624 if (remote->url == NULL) {
1625 err = got_error_from_errno("strndup");
1626 free(remote->name);
1627 break;
1629 (*nremotes)++;
1630 break;
1631 default:
1632 err = got_error(GOT_ERR_PRIVSEP_MSG);
1633 break;
1636 imsg_free(&imsg);
1637 if (err)
1638 break;
1641 if (err) {
1642 int i;
1643 for (i = 0; i < *nremotes; i++) {
1644 free((*remotes)[i].name);
1645 free((*remotes)[i].url);
1647 free(*remotes);
1648 *remotes = NULL;
1649 *nremotes = 0;
1651 return err;
1654 const struct got_error *
1655 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1656 struct got_object_id *id, int idx, const char *path)
1658 const struct got_error *err = NULL;
1659 struct ibuf *wbuf;
1660 size_t path_len = strlen(path) + 1;
1662 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1663 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1664 if (wbuf == NULL)
1665 return got_error_from_errno(
1666 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1667 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1668 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1669 ibuf_free(wbuf);
1670 return err;
1672 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1673 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1674 ibuf_free(wbuf);
1675 return err;
1677 if (imsg_add(wbuf, path, path_len) == -1) {
1678 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1679 ibuf_free(wbuf);
1680 return err;
1683 wbuf->fd = -1;
1684 imsg_close(ibuf, wbuf);
1686 return flush_imsg(ibuf);
1689 const struct got_error *
1690 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1691 size_t ncommits, struct imsgbuf *ibuf)
1693 const struct got_error *err;
1694 struct ibuf *wbuf;
1695 int i;
1697 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1698 sizeof(struct got_imsg_traversed_commits) +
1699 ncommits * SHA1_DIGEST_LENGTH);
1700 if (wbuf == NULL)
1701 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1703 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1704 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1705 ibuf_free(wbuf);
1706 return err;
1708 for (i = 0; i < ncommits; i++) {
1709 struct got_object_id *id = &commit_ids[i];
1710 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1711 err = got_error_from_errno(
1712 "imsg_add TRAVERSED_COMMITS");
1713 ibuf_free(wbuf);
1714 return err;
1718 wbuf->fd = -1;
1719 imsg_close(ibuf, wbuf);
1721 return flush_imsg(ibuf);
1724 const struct got_error *
1725 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1726 struct got_object_id **changed_commit_id,
1727 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1729 const struct got_error *err = NULL;
1730 struct imsg imsg;
1731 struct got_imsg_traversed_commits *icommits;
1732 size_t datalen;
1733 int i, done = 0;
1735 *changed_commit = NULL;
1736 *changed_commit_id = NULL;
1738 while (!done) {
1739 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1740 if (err)
1741 return err;
1743 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1744 switch (imsg.hdr.type) {
1745 case GOT_IMSG_TRAVERSED_COMMITS:
1746 icommits = imsg.data;
1747 if (datalen != sizeof(*icommits) +
1748 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1749 err = got_error(GOT_ERR_PRIVSEP_LEN);
1750 break;
1752 for (i = 0; i < icommits->ncommits; i++) {
1753 struct got_object_qid *qid;
1754 uint8_t *sha1 = (uint8_t *)imsg.data +
1755 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1756 err = got_object_qid_alloc_partial(&qid);
1757 if (err)
1758 break;
1759 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1760 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1762 /* The last commit may contain a change. */
1763 if (i == icommits->ncommits - 1) {
1764 *changed_commit_id =
1765 got_object_id_dup(qid->id);
1766 if (*changed_commit_id == NULL) {
1767 err = got_error_from_errno(
1768 "got_object_id_dup");
1769 break;
1773 break;
1774 case GOT_IMSG_COMMIT:
1775 if (*changed_commit_id == NULL) {
1776 err = got_error(GOT_ERR_PRIVSEP_MSG);
1777 break;
1779 err = get_commit_from_imsg(changed_commit, &imsg,
1780 datalen, ibuf);
1781 break;
1782 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1783 done = 1;
1784 break;
1785 default:
1786 err = got_error(GOT_ERR_PRIVSEP_MSG);
1787 break;
1790 imsg_free(&imsg);
1791 if (err)
1792 break;
1795 if (err)
1796 got_object_id_queue_free(commit_ids);
1797 return err;
1800 const struct got_error *
1801 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1803 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1804 NULL, 0) == -1)
1805 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1807 return flush_imsg(ibuf);
1810 const struct got_error *
1811 got_privsep_unveil_exec_helpers(void)
1813 const char *helpers[] = {
1814 GOT_PATH_PROG_READ_PACK,
1815 GOT_PATH_PROG_READ_OBJECT,
1816 GOT_PATH_PROG_READ_COMMIT,
1817 GOT_PATH_PROG_READ_TREE,
1818 GOT_PATH_PROG_READ_BLOB,
1819 GOT_PATH_PROG_READ_TAG,
1820 GOT_PATH_PROG_READ_GITCONFIG,
1822 int i;
1824 for (i = 0; i < nitems(helpers); i++) {
1825 if (unveil(helpers[i], "x") == 0)
1826 continue;
1827 return got_error_from_errno2("unveil", helpers[i]);
1830 return NULL;
1833 void
1834 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1836 if (close(imsg_fds[0]) != 0) {
1837 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1838 _exit(1);
1841 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1842 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1843 _exit(1);
1845 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1846 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1847 _exit(1);
1850 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1851 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1852 strerror(errno));
1853 _exit(1);