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 memcpy(hash->sha1, imsg.data, SHA1_DIGEST_LENGTH);
442 imsg_free(&imsg);
443 return NULL;
446 imsg_free(&imsg);
447 return got_error(GOT_ERR_PRIVSEP_MSG);
451 const struct got_error *
452 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
454 const struct got_error *err = NULL;
456 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
457 hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
458 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
459 close(fd);
460 return err;
462 return flush_imsg(ibuf);
465 const struct got_error *
466 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
468 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
469 return got_error_from_errno("imsg_compose FETCH");
470 return flush_imsg(ibuf);
473 const struct got_error *
474 got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
476 const struct got_error *err = NULL;
477 struct imsg imsg;
479 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
480 if (err)
481 return err;
482 if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
483 return NULL;
484 else
485 return got_error(GOT_ERR_PRIVSEP_MSG);
486 imsg_free(&imsg);
489 const struct got_error *
490 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
491 struct imsgbuf *ibuf)
493 const struct got_error *err = NULL;
494 struct got_imsg_object *iobj;
495 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
497 if (datalen != sizeof(*iobj))
498 return got_error(GOT_ERR_PRIVSEP_LEN);
499 iobj = imsg->data;
501 *obj = calloc(1, sizeof(**obj));
502 if (*obj == NULL)
503 return got_error_from_errno("calloc");
505 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
506 (*obj)->type = iobj->type;
507 (*obj)->flags = iobj->flags;
508 (*obj)->hdrlen = iobj->hdrlen;
509 (*obj)->size = iobj->size;
510 /* path_packfile is handled by caller */
511 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
512 (*obj)->pack_offset = iobj->pack_offset;
513 (*obj)->pack_idx = iobj->pack_idx;
516 return err;
519 const struct got_error *
520 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
522 const struct got_error *err = NULL;
523 struct imsg imsg;
524 const size_t min_datalen =
525 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
527 *obj = NULL;
529 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
530 if (err)
531 return err;
533 switch (imsg.hdr.type) {
534 case GOT_IMSG_OBJECT:
535 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
536 break;
537 default:
538 err = got_error(GOT_ERR_PRIVSEP_MSG);
539 break;
542 imsg_free(&imsg);
544 return err;
547 static const struct got_error *
548 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
549 size_t logmsg_len)
551 const struct got_error *err = NULL;
552 size_t offset, remain;
554 offset = 0;
555 remain = logmsg_len;
556 while (remain > 0) {
557 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
559 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
560 commit->logmsg + offset, n) == -1) {
561 err = got_error_from_errno("imsg_compose "
562 "COMMIT_LOGMSG");
563 break;
566 err = flush_imsg(ibuf);
567 if (err)
568 break;
570 offset += n;
571 remain -= n;
574 return err;
577 const struct got_error *
578 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
580 const struct got_error *err = NULL;
581 struct got_imsg_commit_object *icommit;
582 uint8_t *buf;
583 size_t len, total;
584 struct got_object_qid *qid;
585 size_t author_len = strlen(commit->author);
586 size_t committer_len = strlen(commit->committer);
587 size_t logmsg_len = strlen(commit->logmsg);
589 total = sizeof(*icommit) + author_len + committer_len +
590 commit->nparents * SHA1_DIGEST_LENGTH;
592 buf = malloc(total);
593 if (buf == NULL)
594 return got_error_from_errno("malloc");
596 icommit = (struct got_imsg_commit_object *)buf;
597 memcpy(icommit->tree_id, commit->tree_id->sha1,
598 sizeof(icommit->tree_id));
599 icommit->author_len = author_len;
600 icommit->author_time = commit->author_time;
601 icommit->author_gmtoff = commit->author_gmtoff;
602 icommit->committer_len = committer_len;
603 icommit->committer_time = commit->committer_time;
604 icommit->committer_gmtoff = commit->committer_gmtoff;
605 icommit->logmsg_len = logmsg_len;
606 icommit->nparents = commit->nparents;
608 len = sizeof(*icommit);
609 memcpy(buf + len, commit->author, author_len);
610 len += author_len;
611 memcpy(buf + len, commit->committer, committer_len);
612 len += committer_len;
613 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
614 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
615 len += SHA1_DIGEST_LENGTH;
618 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
619 err = got_error_from_errno("imsg_compose COMMIT");
620 goto done;
623 if (logmsg_len == 0 ||
624 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
625 err = flush_imsg(ibuf);
626 if (err)
627 goto done;
629 err = send_commit_logmsg(ibuf, commit, logmsg_len);
630 done:
631 free(buf);
632 return err;
635 static const struct got_error *
636 get_commit_from_imsg(struct got_commit_object **commit,
637 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
639 const struct got_error *err = NULL;
640 struct got_imsg_commit_object *icommit;
641 size_t len = 0;
642 int i;
644 if (datalen < sizeof(*icommit))
645 return got_error(GOT_ERR_PRIVSEP_LEN);
647 icommit = imsg->data;
648 if (datalen != sizeof(*icommit) + icommit->author_len +
649 icommit->committer_len +
650 icommit->nparents * SHA1_DIGEST_LENGTH)
651 return got_error(GOT_ERR_PRIVSEP_LEN);
653 if (icommit->nparents < 0)
654 return got_error(GOT_ERR_PRIVSEP_LEN);
656 len += sizeof(*icommit);
658 *commit = got_object_commit_alloc_partial();
659 if (*commit == NULL)
660 return got_error_from_errno(
661 "got_object_commit_alloc_partial");
663 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
664 SHA1_DIGEST_LENGTH);
665 (*commit)->author_time = icommit->author_time;
666 (*commit)->author_gmtoff = icommit->author_gmtoff;
667 (*commit)->committer_time = icommit->committer_time;
668 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
670 if (icommit->author_len == 0) {
671 (*commit)->author = strdup("");
672 if ((*commit)->author == NULL) {
673 err = got_error_from_errno("strdup");
674 goto done;
676 } else {
677 (*commit)->author = malloc(icommit->author_len + 1);
678 if ((*commit)->author == NULL) {
679 err = got_error_from_errno("malloc");
680 goto done;
682 memcpy((*commit)->author, imsg->data + len,
683 icommit->author_len);
684 (*commit)->author[icommit->author_len] = '\0';
686 len += icommit->author_len;
688 if (icommit->committer_len == 0) {
689 (*commit)->committer = strdup("");
690 if ((*commit)->committer == NULL) {
691 err = got_error_from_errno("strdup");
692 goto done;
694 } else {
695 (*commit)->committer =
696 malloc(icommit->committer_len + 1);
697 if ((*commit)->committer == NULL) {
698 err = got_error_from_errno("malloc");
699 goto done;
701 memcpy((*commit)->committer, imsg->data + len,
702 icommit->committer_len);
703 (*commit)->committer[icommit->committer_len] = '\0';
705 len += icommit->committer_len;
707 if (icommit->logmsg_len == 0) {
708 (*commit)->logmsg = strdup("");
709 if ((*commit)->logmsg == NULL) {
710 err = got_error_from_errno("strdup");
711 goto done;
713 } else {
714 size_t offset = 0, remain = icommit->logmsg_len;
716 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
717 if ((*commit)->logmsg == NULL) {
718 err = got_error_from_errno("malloc");
719 goto done;
721 while (remain > 0) {
722 struct imsg imsg_log;
723 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
724 remain);
726 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
727 if (err)
728 goto done;
730 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
731 err = got_error(GOT_ERR_PRIVSEP_MSG);
732 goto done;
735 memcpy((*commit)->logmsg + offset,
736 imsg_log.data, n);
737 imsg_free(&imsg_log);
738 offset += n;
739 remain -= n;
741 (*commit)->logmsg[icommit->logmsg_len] = '\0';
744 for (i = 0; i < icommit->nparents; i++) {
745 struct got_object_qid *qid;
747 err = got_object_qid_alloc_partial(&qid);
748 if (err)
749 break;
750 memcpy(qid->id, imsg->data + len +
751 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
752 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
753 (*commit)->nparents++;
755 done:
756 if (err) {
757 got_object_commit_close(*commit);
758 *commit = NULL;
760 return err;
763 const struct got_error *
764 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
766 const struct got_error *err = NULL;
767 struct imsg imsg;
768 size_t datalen;
769 const size_t min_datalen =
770 MIN(sizeof(struct got_imsg_error),
771 sizeof(struct got_imsg_commit_object));
773 *commit = NULL;
775 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
776 if (err)
777 return err;
779 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
781 switch (imsg.hdr.type) {
782 case GOT_IMSG_COMMIT:
783 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
784 break;
785 default:
786 err = got_error(GOT_ERR_PRIVSEP_MSG);
787 break;
790 imsg_free(&imsg);
792 return err;
795 const struct got_error *
796 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
797 int nentries)
799 const struct got_error *err = NULL;
800 struct got_imsg_tree_object itree;
801 struct got_pathlist_entry *pe;
802 size_t totlen;
803 int nimsg; /* number of imsg queued in ibuf */
805 itree.nentries = nentries;
806 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
807 == -1)
808 return got_error_from_errno("imsg_compose TREE");
810 totlen = sizeof(itree);
811 nimsg = 1;
812 TAILQ_FOREACH(pe, entries, entry) {
813 const char *name = pe->path;
814 struct got_parsed_tree_entry *pte = pe->data;
815 struct ibuf *wbuf;
816 size_t namelen = strlen(name);
817 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
819 if (len > MAX_IMSGSIZE)
820 return got_error(GOT_ERR_NO_SPACE);
822 nimsg++;
823 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
824 err = flush_imsg(ibuf);
825 if (err)
826 return err;
827 nimsg = 0;
830 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
831 if (wbuf == NULL)
832 return got_error_from_errno("imsg_create TREE_ENTRY");
834 /* Keep in sync with struct got_imsg_tree_object definition! */
835 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
836 err = got_error_from_errno("imsg_add TREE_ENTRY");
837 ibuf_free(wbuf);
838 return err;
840 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
841 err = got_error_from_errno("imsg_add TREE_ENTRY");
842 ibuf_free(wbuf);
843 return err;
846 if (imsg_add(wbuf, name, namelen) == -1) {
847 err = got_error_from_errno("imsg_add TREE_ENTRY");
848 ibuf_free(wbuf);
849 return err;
852 wbuf->fd = -1;
853 imsg_close(ibuf, wbuf);
855 totlen += len;
858 return flush_imsg(ibuf);
861 const struct got_error *
862 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
864 const struct got_error *err = NULL;
865 const size_t min_datalen =
866 MIN(sizeof(struct got_imsg_error),
867 sizeof(struct got_imsg_tree_object));
868 struct got_imsg_tree_object *itree;
869 int nentries = 0;
871 *tree = NULL;
872 get_more:
873 err = read_imsg(ibuf);
874 if (err)
875 goto done;
877 for (;;) {
878 struct imsg imsg;
879 size_t n;
880 size_t datalen;
881 struct got_imsg_tree_entry *ite;
882 struct got_tree_entry *te = NULL;
884 n = imsg_get(ibuf, &imsg);
885 if (n == 0) {
886 if (*tree && (*tree)->nentries != nentries)
887 goto get_more;
888 break;
891 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
892 return got_error(GOT_ERR_PRIVSEP_LEN);
894 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
896 switch (imsg.hdr.type) {
897 case GOT_IMSG_ERROR:
898 err = recv_imsg_error(&imsg, datalen);
899 break;
900 case GOT_IMSG_TREE:
901 /* This message should only appear once. */
902 if (*tree != NULL) {
903 err = got_error(GOT_ERR_PRIVSEP_MSG);
904 break;
906 if (datalen != sizeof(*itree)) {
907 err = got_error(GOT_ERR_PRIVSEP_LEN);
908 break;
910 itree = imsg.data;
911 *tree = malloc(sizeof(**tree));
912 if (*tree == NULL) {
913 err = got_error_from_errno("malloc");
914 break;
916 (*tree)->entries = calloc(itree->nentries,
917 sizeof(struct got_tree_entry));
918 if ((*tree)->entries == NULL) {
919 err = got_error_from_errno("malloc");
920 break;
922 (*tree)->nentries = itree->nentries;
923 (*tree)->refcnt = 0;
924 break;
925 case GOT_IMSG_TREE_ENTRY:
926 /* This message should be preceeded by GOT_IMSG_TREE. */
927 if (*tree == NULL) {
928 err = got_error(GOT_ERR_PRIVSEP_MSG);
929 break;
931 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
932 err = got_error(GOT_ERR_PRIVSEP_LEN);
933 break;
936 /* Remaining data contains the entry's name. */
937 datalen -= sizeof(*ite);
938 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
939 err = got_error(GOT_ERR_PRIVSEP_LEN);
940 break;
942 ite = imsg.data;
944 if (datalen + 1 > sizeof(te->name)) {
945 err = got_error(GOT_ERR_NO_SPACE);
946 break;
948 te = &(*tree)->entries[nentries];
949 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
950 te->name[datalen] = '\0';
952 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
953 te->mode = ite->mode;
954 te->idx = nentries;
955 nentries++;
956 break;
957 default:
958 err = got_error(GOT_ERR_PRIVSEP_MSG);
959 break;
962 imsg_free(&imsg);
964 done:
965 if (*tree && (*tree)->nentries != nentries) {
966 if (err == NULL)
967 err = got_error(GOT_ERR_PRIVSEP_LEN);
968 got_object_tree_close(*tree);
969 *tree = NULL;
972 return err;
975 const struct got_error *
976 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
977 const uint8_t *data)
979 struct got_imsg_blob iblob;
981 iblob.size = size;
982 iblob.hdrlen = hdrlen;
984 if (data) {
985 uint8_t *buf;
987 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
988 return got_error(GOT_ERR_NO_SPACE);
990 buf = malloc(sizeof(iblob) + size);
991 if (buf == NULL)
992 return got_error_from_errno("malloc");
994 memcpy(buf, &iblob, sizeof(iblob));
995 memcpy(buf + sizeof(iblob), data, size);
996 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
997 sizeof(iblob) + size) == -1) {
998 free(buf);
999 return got_error_from_errno("imsg_compose BLOB");
1001 free(buf);
1002 } else {
1003 /* Data has already been written to file descriptor. */
1004 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1005 sizeof(iblob)) == -1)
1006 return got_error_from_errno("imsg_compose BLOB");
1010 return flush_imsg(ibuf);
1013 const struct got_error *
1014 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1015 struct imsgbuf *ibuf)
1017 const struct got_error *err = NULL;
1018 struct imsg imsg;
1019 struct got_imsg_blob *iblob;
1020 size_t datalen;
1022 *outbuf = NULL;
1024 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1025 if (err)
1026 return err;
1028 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1030 switch (imsg.hdr.type) {
1031 case GOT_IMSG_BLOB:
1032 if (datalen < sizeof(*iblob)) {
1033 err = got_error(GOT_ERR_PRIVSEP_LEN);
1034 break;
1036 iblob = imsg.data;
1037 *size = iblob->size;
1038 *hdrlen = iblob->hdrlen;
1040 if (datalen == sizeof(*iblob)) {
1041 /* Data has been written to file descriptor. */
1042 break;
1045 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1046 err = got_error(GOT_ERR_PRIVSEP_LEN);
1047 break;
1050 *outbuf = malloc(*size);
1051 if (*outbuf == NULL) {
1052 err = got_error_from_errno("malloc");
1053 break;
1055 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1056 break;
1057 default:
1058 err = got_error(GOT_ERR_PRIVSEP_MSG);
1059 break;
1062 imsg_free(&imsg);
1064 return err;
1067 static const struct got_error *
1068 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1070 const struct got_error *err = NULL;
1071 size_t offset, remain;
1073 offset = 0;
1074 remain = tagmsg_len;
1075 while (remain > 0) {
1076 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1078 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1079 tag->tagmsg + offset, n) == -1) {
1080 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1081 break;
1084 err = flush_imsg(ibuf);
1085 if (err)
1086 break;
1088 offset += n;
1089 remain -= n;
1092 return err;
1095 const struct got_error *
1096 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1098 const struct got_error *err = NULL;
1099 struct got_imsg_tag_object *itag;
1100 uint8_t *buf;
1101 size_t len, total;
1102 size_t tag_len = strlen(tag->tag);
1103 size_t tagger_len = strlen(tag->tagger);
1104 size_t tagmsg_len = strlen(tag->tagmsg);
1106 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1108 buf = malloc(total);
1109 if (buf == NULL)
1110 return got_error_from_errno("malloc");
1112 itag = (struct got_imsg_tag_object *)buf;
1113 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1114 itag->obj_type = tag->obj_type;
1115 itag->tag_len = tag_len;
1116 itag->tagger_len = tagger_len;
1117 itag->tagger_time = tag->tagger_time;
1118 itag->tagger_gmtoff = tag->tagger_gmtoff;
1119 itag->tagmsg_len = tagmsg_len;
1121 len = sizeof(*itag);
1122 memcpy(buf + len, tag->tag, tag_len);
1123 len += tag_len;
1124 memcpy(buf + len, tag->tagger, tagger_len);
1125 len += tagger_len;
1127 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1128 err = got_error_from_errno("imsg_compose TAG");
1129 goto done;
1132 if (tagmsg_len == 0 ||
1133 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1134 err = flush_imsg(ibuf);
1135 if (err)
1136 goto done;
1138 err = send_tagmsg(ibuf, tag, tagmsg_len);
1139 done:
1140 free(buf);
1141 return err;
1144 const struct got_error *
1145 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1147 const struct got_error *err = NULL;
1148 struct imsg imsg;
1149 struct got_imsg_tag_object *itag;
1150 size_t len, datalen;
1151 const size_t min_datalen =
1152 MIN(sizeof(struct got_imsg_error),
1153 sizeof(struct got_imsg_tag_object));
1155 *tag = NULL;
1157 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1158 if (err)
1159 return err;
1161 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1162 len = 0;
1164 switch (imsg.hdr.type) {
1165 case GOT_IMSG_TAG:
1166 if (datalen < sizeof(*itag)) {
1167 err = got_error(GOT_ERR_PRIVSEP_LEN);
1168 break;
1170 itag = imsg.data;
1171 if (datalen != sizeof(*itag) + itag->tag_len +
1172 itag->tagger_len) {
1173 err = got_error(GOT_ERR_PRIVSEP_LEN);
1174 break;
1176 len += sizeof(*itag);
1178 *tag = calloc(1, sizeof(**tag));
1179 if (*tag == NULL) {
1180 err = got_error_from_errno("calloc");
1181 break;
1184 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1186 if (itag->tag_len == 0) {
1187 (*tag)->tag = strdup("");
1188 if ((*tag)->tag == NULL) {
1189 err = got_error_from_errno("strdup");
1190 break;
1192 } else {
1193 (*tag)->tag = malloc(itag->tag_len + 1);
1194 if ((*tag)->tag == NULL) {
1195 err = got_error_from_errno("malloc");
1196 break;
1198 memcpy((*tag)->tag, imsg.data + len,
1199 itag->tag_len);
1200 (*tag)->tag[itag->tag_len] = '\0';
1202 len += itag->tag_len;
1204 (*tag)->obj_type = itag->obj_type;
1205 (*tag)->tagger_time = itag->tagger_time;
1206 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1208 if (itag->tagger_len == 0) {
1209 (*tag)->tagger = strdup("");
1210 if ((*tag)->tagger == NULL) {
1211 err = got_error_from_errno("strdup");
1212 break;
1214 } else {
1215 (*tag)->tagger = malloc(itag->tagger_len + 1);
1216 if ((*tag)->tagger == NULL) {
1217 err = got_error_from_errno("malloc");
1218 break;
1220 memcpy((*tag)->tagger, imsg.data + len,
1221 itag->tagger_len);
1222 (*tag)->tagger[itag->tagger_len] = '\0';
1224 len += itag->tagger_len;
1226 if (itag->tagmsg_len == 0) {
1227 (*tag)->tagmsg = strdup("");
1228 if ((*tag)->tagmsg == NULL) {
1229 err = got_error_from_errno("strdup");
1230 break;
1232 } else {
1233 size_t offset = 0, remain = itag->tagmsg_len;
1235 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1236 if ((*tag)->tagmsg == NULL) {
1237 err = got_error_from_errno("malloc");
1238 break;
1240 while (remain > 0) {
1241 struct imsg imsg_log;
1242 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1243 remain);
1245 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1246 if (err)
1247 return err;
1249 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1250 return got_error(GOT_ERR_PRIVSEP_MSG);
1252 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1253 n);
1254 imsg_free(&imsg_log);
1255 offset += n;
1256 remain -= n;
1258 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1261 break;
1262 default:
1263 err = got_error(GOT_ERR_PRIVSEP_MSG);
1264 break;
1267 imsg_free(&imsg);
1269 return err;
1272 const struct got_error *
1273 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1274 struct got_packidx *packidx)
1276 const struct got_error *err = NULL;
1277 struct got_imsg_packidx ipackidx;
1278 struct got_imsg_pack ipack;
1279 int fd;
1281 ipackidx.len = packidx->len;
1282 fd = dup(packidx->fd);
1283 if (fd == -1)
1284 return got_error_from_errno("dup");
1286 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1287 sizeof(ipackidx)) == -1) {
1288 err = got_error_from_errno("imsg_compose PACKIDX");
1289 close(fd);
1290 return err;
1293 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1294 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1295 return got_error(GOT_ERR_NO_SPACE);
1296 ipack.filesize = pack->filesize;
1298 fd = dup(pack->fd);
1299 if (fd == -1)
1300 return got_error_from_errno("dup");
1302 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1303 == -1) {
1304 err = got_error_from_errno("imsg_compose PACK");
1305 close(fd);
1306 return err;
1309 return flush_imsg(ibuf);
1312 const struct got_error *
1313 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1314 struct got_object_id *id)
1316 struct got_imsg_packed_object iobj;
1318 iobj.idx = idx;
1319 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1321 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1322 &iobj, sizeof(iobj)) == -1)
1323 return got_error_from_errno("imsg_compose "
1324 "PACKED_OBJECT_REQUEST");
1326 return flush_imsg(ibuf);
1329 const struct got_error *
1330 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1332 const struct got_error *err = NULL;
1334 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1335 NULL, 0) == -1) {
1336 err = got_error_from_errno("imsg_compose "
1337 "GITCONFIG_PARSE_REQUEST");
1338 close(fd);
1339 return err;
1342 return flush_imsg(ibuf);
1345 const struct got_error *
1346 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1348 if (imsg_compose(ibuf,
1349 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1350 NULL, 0) == -1)
1351 return got_error_from_errno("imsg_compose "
1352 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1354 return flush_imsg(ibuf);
1357 const struct got_error *
1358 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1360 if (imsg_compose(ibuf,
1361 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1362 return got_error_from_errno("imsg_compose "
1363 "GITCONFIG_AUTHOR_NAME_REQUEST");
1365 return flush_imsg(ibuf);
1368 const struct got_error *
1369 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1371 if (imsg_compose(ibuf,
1372 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1373 return got_error_from_errno("imsg_compose "
1374 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1376 return flush_imsg(ibuf);
1379 const struct got_error *
1380 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1382 if (imsg_compose(ibuf,
1383 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1384 return got_error_from_errno("imsg_compose "
1385 "GITCONFIG_REMOTE_REQUEST");
1387 return flush_imsg(ibuf);
1390 const struct got_error *
1391 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1393 if (imsg_compose(ibuf,
1394 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1395 return got_error_from_errno("imsg_compose "
1396 "GITCONFIG_OWNER_REQUEST");
1398 return flush_imsg(ibuf);
1401 const struct got_error *
1402 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1404 size_t len = value ? strlen(value) + 1 : 0;
1406 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1407 value, len) == -1)
1408 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1410 return flush_imsg(ibuf);
1413 const struct got_error *
1414 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1416 const struct got_error *err = NULL;
1417 struct imsg imsg;
1418 size_t datalen;
1419 const size_t min_datalen = 0;
1421 *str = NULL;
1423 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1424 if (err)
1425 return err;
1426 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1428 switch (imsg.hdr.type) {
1429 case GOT_IMSG_GITCONFIG_STR_VAL:
1430 if (datalen == 0)
1431 break;
1432 *str = malloc(datalen);
1433 if (*str == NULL) {
1434 err = got_error_from_errno("malloc");
1435 break;
1437 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1438 err = got_error(GOT_ERR_NO_SPACE);
1439 break;
1440 default:
1441 err = got_error(GOT_ERR_PRIVSEP_MSG);
1442 break;
1445 imsg_free(&imsg);
1446 return err;
1449 const struct got_error *
1450 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1452 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1453 &value, sizeof(value)) == -1)
1454 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1456 return flush_imsg(ibuf);
1459 const struct got_error *
1460 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1462 const struct got_error *err = NULL;
1463 struct imsg imsg;
1464 size_t datalen;
1465 const size_t min_datalen =
1466 MIN(sizeof(struct got_imsg_error), sizeof(int));
1468 *val = 0;
1470 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1471 if (err)
1472 return err;
1473 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1475 switch (imsg.hdr.type) {
1476 case GOT_IMSG_GITCONFIG_INT_VAL:
1477 if (datalen != sizeof(*val)) {
1478 err = got_error(GOT_ERR_PRIVSEP_LEN);
1479 break;
1481 memcpy(val, imsg.data, sizeof(*val));
1482 break;
1483 default:
1484 err = got_error(GOT_ERR_PRIVSEP_MSG);
1485 break;
1488 imsg_free(&imsg);
1489 return err;
1492 const struct got_error *
1493 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1494 struct got_remote_repo *remotes, int nremotes)
1496 const struct got_error *err = NULL;
1497 struct got_imsg_remotes iremotes;
1498 int i;
1500 iremotes.nremotes = nremotes;
1501 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1502 &iremotes, sizeof(iremotes)) == -1)
1503 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1505 err = flush_imsg(ibuf);
1506 imsg_clear(ibuf);
1507 if (err)
1508 return err;
1510 for (i = 0; i < nremotes; i++) {
1511 struct got_imsg_remote iremote;
1512 size_t len = sizeof(iremote);
1513 struct ibuf *wbuf;
1515 iremote.name_len = strlen(remotes[i].name);
1516 len += iremote.name_len;
1517 iremote.url_len = strlen(remotes[i].url);
1518 len += iremote.url_len;
1520 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1521 if (wbuf == NULL)
1522 return got_error_from_errno(
1523 "imsg_create GITCONFIG_REMOTE");
1525 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1526 err = got_error_from_errno(
1527 "imsg_add GIITCONFIG_REMOTE");
1528 ibuf_free(wbuf);
1529 return err;
1532 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1533 err = got_error_from_errno(
1534 "imsg_add GIITCONFIG_REMOTE");
1535 ibuf_free(wbuf);
1536 return err;
1538 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1539 err = got_error_from_errno(
1540 "imsg_add GIITCONFIG_REMOTE");
1541 ibuf_free(wbuf);
1542 return err;
1545 wbuf->fd = -1;
1546 imsg_close(ibuf, wbuf);
1547 err = flush_imsg(ibuf);
1548 if (err)
1549 return err;
1552 return NULL;
1555 const struct got_error *
1556 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1557 int *nremotes, struct imsgbuf *ibuf)
1559 const struct got_error *err = NULL;
1560 struct imsg imsg;
1561 size_t datalen;
1562 struct got_imsg_remotes iremotes;
1563 struct got_imsg_remote iremote;
1565 *remotes = NULL;
1566 *nremotes = 0;
1567 iremotes.nremotes = 0;
1569 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1570 if (err)
1571 return err;
1572 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1574 switch (imsg.hdr.type) {
1575 case GOT_IMSG_GITCONFIG_REMOTES:
1576 if (datalen != sizeof(iremotes)) {
1577 err = got_error(GOT_ERR_PRIVSEP_LEN);
1578 break;
1580 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1581 if (iremotes.nremotes == 0) {
1582 imsg_free(&imsg);
1583 return NULL;
1585 break;
1586 default:
1587 imsg_free(&imsg);
1588 return got_error(GOT_ERR_PRIVSEP_MSG);
1591 imsg_free(&imsg);
1593 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1594 if (*remotes == NULL)
1595 return got_error_from_errno("recallocarray");
1597 while (*nremotes < iremotes.nremotes) {
1598 struct got_remote_repo *remote;
1600 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1601 if (err)
1602 break;
1603 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1605 switch (imsg.hdr.type) {
1606 case GOT_IMSG_GITCONFIG_REMOTE:
1607 remote = &(*remotes)[*nremotes];
1608 if (datalen < sizeof(iremote)) {
1609 err = got_error(GOT_ERR_PRIVSEP_LEN);
1610 break;
1612 memcpy(&iremote, imsg.data, sizeof(iremote));
1613 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1614 (sizeof(iremote) + iremote.name_len +
1615 iremote.url_len) > datalen) {
1616 err = got_error(GOT_ERR_PRIVSEP_LEN);
1617 break;
1619 remote->name = strndup(imsg.data + sizeof(iremote),
1620 iremote.name_len);
1621 if (remote->name == NULL) {
1622 err = got_error_from_errno("strndup");
1623 break;
1625 remote->url = strndup(imsg.data + sizeof(iremote) +
1626 iremote.name_len, iremote.url_len);
1627 if (remote->url == NULL) {
1628 err = got_error_from_errno("strndup");
1629 free(remote->name);
1630 break;
1632 (*nremotes)++;
1633 break;
1634 default:
1635 err = got_error(GOT_ERR_PRIVSEP_MSG);
1636 break;
1639 imsg_free(&imsg);
1640 if (err)
1641 break;
1644 if (err) {
1645 int i;
1646 for (i = 0; i < *nremotes; i++) {
1647 free((*remotes)[i].name);
1648 free((*remotes)[i].url);
1650 free(*remotes);
1651 *remotes = NULL;
1652 *nremotes = 0;
1654 return err;
1657 const struct got_error *
1658 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1659 struct got_object_id *id, int idx, const char *path)
1661 const struct got_error *err = NULL;
1662 struct ibuf *wbuf;
1663 size_t path_len = strlen(path) + 1;
1665 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1666 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1667 if (wbuf == NULL)
1668 return got_error_from_errno(
1669 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1670 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1671 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1672 ibuf_free(wbuf);
1673 return err;
1675 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1676 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1677 ibuf_free(wbuf);
1678 return err;
1680 if (imsg_add(wbuf, path, path_len) == -1) {
1681 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1682 ibuf_free(wbuf);
1683 return err;
1686 wbuf->fd = -1;
1687 imsg_close(ibuf, wbuf);
1689 return flush_imsg(ibuf);
1692 const struct got_error *
1693 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1694 size_t ncommits, struct imsgbuf *ibuf)
1696 const struct got_error *err;
1697 struct ibuf *wbuf;
1698 int i;
1700 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1701 sizeof(struct got_imsg_traversed_commits) +
1702 ncommits * SHA1_DIGEST_LENGTH);
1703 if (wbuf == NULL)
1704 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1706 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1707 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1708 ibuf_free(wbuf);
1709 return err;
1711 for (i = 0; i < ncommits; i++) {
1712 struct got_object_id *id = &commit_ids[i];
1713 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1714 err = got_error_from_errno(
1715 "imsg_add TRAVERSED_COMMITS");
1716 ibuf_free(wbuf);
1717 return err;
1721 wbuf->fd = -1;
1722 imsg_close(ibuf, wbuf);
1724 return flush_imsg(ibuf);
1727 const struct got_error *
1728 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1729 struct got_object_id **changed_commit_id,
1730 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1732 const struct got_error *err = NULL;
1733 struct imsg imsg;
1734 struct got_imsg_traversed_commits *icommits;
1735 size_t datalen;
1736 int i, done = 0;
1738 *changed_commit = NULL;
1739 *changed_commit_id = NULL;
1741 while (!done) {
1742 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1743 if (err)
1744 return err;
1746 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1747 switch (imsg.hdr.type) {
1748 case GOT_IMSG_TRAVERSED_COMMITS:
1749 icommits = imsg.data;
1750 if (datalen != sizeof(*icommits) +
1751 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1752 err = got_error(GOT_ERR_PRIVSEP_LEN);
1753 break;
1755 for (i = 0; i < icommits->ncommits; i++) {
1756 struct got_object_qid *qid;
1757 uint8_t *sha1 = (uint8_t *)imsg.data +
1758 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1759 err = got_object_qid_alloc_partial(&qid);
1760 if (err)
1761 break;
1762 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1763 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1765 /* The last commit may contain a change. */
1766 if (i == icommits->ncommits - 1) {
1767 *changed_commit_id =
1768 got_object_id_dup(qid->id);
1769 if (*changed_commit_id == NULL) {
1770 err = got_error_from_errno(
1771 "got_object_id_dup");
1772 break;
1776 break;
1777 case GOT_IMSG_COMMIT:
1778 if (*changed_commit_id == NULL) {
1779 err = got_error(GOT_ERR_PRIVSEP_MSG);
1780 break;
1782 err = get_commit_from_imsg(changed_commit, &imsg,
1783 datalen, ibuf);
1784 break;
1785 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1786 done = 1;
1787 break;
1788 default:
1789 err = got_error(GOT_ERR_PRIVSEP_MSG);
1790 break;
1793 imsg_free(&imsg);
1794 if (err)
1795 break;
1798 if (err)
1799 got_object_id_queue_free(commit_ids);
1800 return err;
1803 const struct got_error *
1804 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1806 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1807 NULL, 0) == -1)
1808 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1810 return flush_imsg(ibuf);
1813 const struct got_error *
1814 got_privsep_unveil_exec_helpers(void)
1816 const char *helpers[] = {
1817 GOT_PATH_PROG_READ_PACK,
1818 GOT_PATH_PROG_READ_OBJECT,
1819 GOT_PATH_PROG_READ_COMMIT,
1820 GOT_PATH_PROG_READ_TREE,
1821 GOT_PATH_PROG_READ_BLOB,
1822 GOT_PATH_PROG_READ_TAG,
1823 GOT_PATH_PROG_READ_GITCONFIG,
1825 int i;
1827 for (i = 0; i < nitems(helpers); i++) {
1828 if (unveil(helpers[i], "x") == 0)
1829 continue;
1830 return got_error_from_errno2("unveil", helpers[i]);
1833 return NULL;
1836 void
1837 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1839 if (close(imsg_fds[0]) != 0) {
1840 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1841 _exit(1);
1844 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1845 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1846 _exit(1);
1848 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1849 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1850 _exit(1);
1853 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1854 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1855 strerror(errno));
1856 _exit(1);