Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/syslimits.h>
21 #include <sys/wait.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <stdint.h>
28 #include <poll.h>
29 #include <imsg.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <time.h>
34 #include "got_object.h"
35 #include "got_error.h"
36 #include "got_path.h"
37 #include "got_repository.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_inflate.h"
42 #include "got_lib_object.h"
43 #include "got_lib_object_parse.h"
44 #include "got_lib_privsep.h"
45 #include "got_lib_pack.h"
47 #ifndef MIN
48 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 #endif
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 static const struct got_error *
56 poll_fd(int fd, int events, int timeout)
57 {
58 struct pollfd pfd[1];
59 int n;
61 pfd[0].fd = fd;
62 pfd[0].events = events;
64 n = poll(pfd, 1, timeout);
65 if (n == -1)
66 return got_error_from_errno("poll");
67 if (n == 0)
68 return got_error(GOT_ERR_TIMEOUT);
69 if (pfd[0].revents & (POLLERR | POLLNVAL))
70 return got_error_from_errno("poll error");
71 if (pfd[0].revents & (events | POLLHUP))
72 return NULL;
74 return got_error(GOT_ERR_INTERRUPT);
75 }
77 static const struct got_error *
78 read_imsg(struct imsgbuf *ibuf)
79 {
80 const struct got_error *err;
81 size_t n;
83 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
84 if (err)
85 return err;
87 n = imsg_read(ibuf);
88 if (n == -1) {
89 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
90 return got_error(GOT_ERR_PRIVSEP_NO_FD);
91 return got_error(GOT_ERR_PRIVSEP_READ);
92 }
93 if (n == 0)
94 return got_error(GOT_ERR_PRIVSEP_PIPE);
96 return NULL;
97 }
99 const struct got_error *
100 got_privsep_wait_for_child(pid_t pid)
102 int child_status;
104 if (waitpid(pid, &child_status, 0) == -1)
105 return got_error_from_errno("waitpid");
107 if (!WIFEXITED(child_status))
108 return got_error(GOT_ERR_PRIVSEP_DIED);
110 if (WEXITSTATUS(child_status) != 0)
111 return got_error(GOT_ERR_PRIVSEP_EXIT);
113 return NULL;
116 static const struct got_error *
117 recv_imsg_error(struct imsg *imsg, size_t datalen)
119 struct got_imsg_error *ierr;
121 if (datalen != sizeof(*ierr))
122 return got_error(GOT_ERR_PRIVSEP_LEN);
124 ierr = imsg->data;
125 if (ierr->code == GOT_ERR_ERRNO) {
126 static struct got_error serr;
127 serr.code = GOT_ERR_ERRNO;
128 serr.msg = strerror(ierr->errno_code);
129 return &serr;
132 return got_error(ierr->code);
135 const struct got_error *
136 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
137 size_t min_datalen)
139 const struct got_error *err;
140 ssize_t n;
142 n = imsg_get(ibuf, imsg);
143 if (n == -1)
144 return got_error_from_errno("imsg_get");
146 while (n == 0) {
147 err = read_imsg(ibuf);
148 if (err)
149 return err;
150 n = imsg_get(ibuf, imsg);
151 if (n == -1)
152 return got_error_from_errno("imsg_get");
155 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
156 return got_error(GOT_ERR_PRIVSEP_LEN);
158 if (imsg->hdr.type == GOT_IMSG_ERROR) {
159 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
160 return recv_imsg_error(imsg, datalen);
163 return NULL;
166 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
167 void
168 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
170 const struct got_error *poll_err;
171 struct got_imsg_error ierr;
172 int ret;
174 ierr.code = err->code;
175 if (err->code == GOT_ERR_ERRNO)
176 ierr.errno_code = errno;
177 else
178 ierr.errno_code = 0;
179 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
180 if (ret == -1) {
181 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
182 getprogname(), err->code, err->msg, strerror(errno));
183 return;
186 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
187 if (poll_err) {
188 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
189 getprogname(), err->code, err->msg, poll_err->msg);
190 return;
193 ret = imsg_flush(ibuf);
194 if (ret == -1) {
195 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
196 getprogname(), err->code, err->msg, strerror(errno));
197 return;
201 static const struct got_error *
202 flush_imsg(struct imsgbuf *ibuf)
204 const struct got_error *err;
206 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
207 if (err)
208 return err;
210 if (imsg_flush(ibuf) == -1)
211 return got_error_from_errno("imsg_flush");
213 return NULL;
216 const struct got_error *
217 got_privsep_send_stop(int fd)
219 const struct got_error *err = NULL;
220 struct imsgbuf ibuf;
222 imsg_init(&ibuf, fd);
224 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
225 return got_error_from_errno("imsg_compose STOP");
227 err = flush_imsg(&ibuf);
228 imsg_clear(&ibuf);
229 return err;
232 const struct got_error *
233 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
235 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
236 == -1)
237 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
239 return flush_imsg(ibuf);
242 const struct got_error *
243 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
244 struct got_object_id *id, int pack_idx)
246 const struct got_error *err = NULL;
247 struct got_imsg_packed_object iobj, *iobjp;
248 size_t len;
250 if (id) { /* commit is packed */
251 iobj.idx = pack_idx;
252 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
253 iobjp = &iobj;
254 len = sizeof(iobj);
255 } else {
256 iobjp = NULL;
257 len = 0;
260 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
261 == -1) {
262 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
263 close(fd);
264 return err;
267 return flush_imsg(ibuf);
270 const struct got_error *
271 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
272 struct got_object_id *id, int pack_idx)
274 const struct got_error *err = NULL;
275 struct ibuf *wbuf;
276 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
278 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
279 if (wbuf == NULL)
280 return got_error_from_errno("imsg_create TREE_REQUEST");
282 if (id) { /* tree is packed */
283 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
284 err = got_error_from_errno("imsg_add TREE_ENTRY");
285 ibuf_free(wbuf);
286 return err;
289 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
290 err = got_error_from_errno("imsg_add TREE_ENTRY");
291 ibuf_free(wbuf);
292 return err;
296 wbuf->fd = fd;
297 imsg_close(ibuf, wbuf);
299 return flush_imsg(ibuf);
302 const struct got_error *
303 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
304 struct got_object_id *id, int pack_idx)
306 struct got_imsg_packed_object iobj, *iobjp;
307 size_t len;
309 if (id) { /* tag is packed */
310 iobj.idx = pack_idx;
311 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
312 iobjp = &iobj;
313 len = sizeof(iobj);
314 } else {
315 iobjp = NULL;
316 len = 0;
319 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
320 == -1)
321 return got_error_from_errno("imsg_compose TAG_REQUEST");
323 return flush_imsg(ibuf);
326 const struct got_error *
327 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
328 struct got_object_id *id, int pack_idx)
330 const struct got_error *err = NULL;
331 struct got_imsg_packed_object iobj, *iobjp;
332 size_t len;
334 if (id) { /* blob is packed */
335 iobj.idx = pack_idx;
336 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
337 iobjp = &iobj;
338 len = sizeof(iobj);
339 } else {
340 iobjp = NULL;
341 len = 0;
344 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
345 == -1) {
346 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
347 close(infd);
348 return err;
351 return flush_imsg(ibuf);
354 const struct got_error *
355 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
357 const struct got_error *err = NULL;
359 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
360 == -1) {
361 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
362 close(outfd);
363 return err;
366 return flush_imsg(ibuf);
369 const struct got_error *
370 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
372 const struct got_error *err = NULL;
374 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
375 == -1) {
376 err = got_error_from_errno("imsg_compose TMPFD");
377 close(fd);
378 return err;
381 return flush_imsg(ibuf);
384 const struct got_error *
385 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
387 struct got_imsg_object iobj;
389 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
390 iobj.type = obj->type;
391 iobj.flags = obj->flags;
392 iobj.hdrlen = obj->hdrlen;
393 iobj.size = obj->size;
394 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
395 iobj.pack_offset = obj->pack_offset;
396 iobj.pack_idx = obj->pack_idx;
399 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
400 == -1)
401 return got_error_from_errno("imsg_compose OBJECT");
403 return flush_imsg(ibuf);
406 const struct got_error *
407 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
408 struct imsgbuf *ibuf)
410 const struct got_error *err = NULL;
411 struct got_imsg_object *iobj;
412 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
414 if (datalen != sizeof(*iobj))
415 return got_error(GOT_ERR_PRIVSEP_LEN);
416 iobj = imsg->data;
418 *obj = calloc(1, sizeof(**obj));
419 if (*obj == NULL)
420 return got_error_from_errno("calloc");
422 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
423 (*obj)->type = iobj->type;
424 (*obj)->flags = iobj->flags;
425 (*obj)->hdrlen = iobj->hdrlen;
426 (*obj)->size = iobj->size;
427 /* path_packfile is handled by caller */
428 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
429 (*obj)->pack_offset = iobj->pack_offset;
430 (*obj)->pack_idx = iobj->pack_idx;
433 return err;
436 const struct got_error *
437 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
439 const struct got_error *err = NULL;
440 struct imsg imsg;
441 const size_t min_datalen =
442 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
444 *obj = NULL;
446 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
447 if (err)
448 return err;
450 switch (imsg.hdr.type) {
451 case GOT_IMSG_OBJECT:
452 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
453 break;
454 default:
455 err = got_error(GOT_ERR_PRIVSEP_MSG);
456 break;
459 imsg_free(&imsg);
461 return err;
464 static const struct got_error *
465 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
466 size_t logmsg_len)
468 const struct got_error *err = NULL;
469 size_t offset, remain;
471 offset = 0;
472 remain = logmsg_len;
473 while (remain > 0) {
474 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
476 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
477 commit->logmsg + offset, n) == -1) {
478 err = got_error_from_errno("imsg_compose "
479 "COMMIT_LOGMSG");
480 break;
483 err = flush_imsg(ibuf);
484 if (err)
485 break;
487 offset += n;
488 remain -= n;
491 return err;
494 const struct got_error *
495 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
497 const struct got_error *err = NULL;
498 struct got_imsg_commit_object *icommit;
499 uint8_t *buf;
500 size_t len, total;
501 struct got_object_qid *qid;
502 size_t author_len = strlen(commit->author);
503 size_t committer_len = strlen(commit->committer);
504 size_t logmsg_len = strlen(commit->logmsg);
506 total = sizeof(*icommit) + author_len + committer_len +
507 commit->nparents * SHA1_DIGEST_LENGTH;
509 buf = malloc(total);
510 if (buf == NULL)
511 return got_error_from_errno("malloc");
513 icommit = (struct got_imsg_commit_object *)buf;
514 memcpy(icommit->tree_id, commit->tree_id->sha1,
515 sizeof(icommit->tree_id));
516 icommit->author_len = author_len;
517 icommit->author_time = commit->author_time;
518 icommit->author_gmtoff = commit->author_gmtoff;
519 icommit->committer_len = committer_len;
520 icommit->committer_time = commit->committer_time;
521 icommit->committer_gmtoff = commit->committer_gmtoff;
522 icommit->logmsg_len = logmsg_len;
523 icommit->nparents = commit->nparents;
525 len = sizeof(*icommit);
526 memcpy(buf + len, commit->author, author_len);
527 len += author_len;
528 memcpy(buf + len, commit->committer, committer_len);
529 len += committer_len;
530 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
531 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
532 len += SHA1_DIGEST_LENGTH;
535 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
536 err = got_error_from_errno("imsg_compose COMMIT");
537 goto done;
540 if (logmsg_len == 0 ||
541 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
542 err = flush_imsg(ibuf);
543 if (err)
544 goto done;
546 err = send_commit_logmsg(ibuf, commit, logmsg_len);
547 done:
548 free(buf);
549 return err;
552 static const struct got_error *
553 get_commit_from_imsg(struct got_commit_object **commit,
554 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
556 const struct got_error *err = NULL;
557 struct got_imsg_commit_object *icommit;
558 size_t len = 0;
559 int i;
561 if (datalen < sizeof(*icommit))
562 return got_error(GOT_ERR_PRIVSEP_LEN);
564 icommit = imsg->data;
565 if (datalen != sizeof(*icommit) + icommit->author_len +
566 icommit->committer_len +
567 icommit->nparents * SHA1_DIGEST_LENGTH)
568 return got_error(GOT_ERR_PRIVSEP_LEN);
570 if (icommit->nparents < 0)
571 return got_error(GOT_ERR_PRIVSEP_LEN);
573 len += sizeof(*icommit);
575 *commit = got_object_commit_alloc_partial();
576 if (*commit == NULL)
577 return got_error_from_errno(
578 "got_object_commit_alloc_partial");
580 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
581 SHA1_DIGEST_LENGTH);
582 (*commit)->author_time = icommit->author_time;
583 (*commit)->author_gmtoff = icommit->author_gmtoff;
584 (*commit)->committer_time = icommit->committer_time;
585 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
587 if (icommit->author_len == 0) {
588 (*commit)->author = strdup("");
589 if ((*commit)->author == NULL) {
590 err = got_error_from_errno("strdup");
591 goto done;
593 } else {
594 (*commit)->author = malloc(icommit->author_len + 1);
595 if ((*commit)->author == NULL) {
596 err = got_error_from_errno("malloc");
597 goto done;
599 memcpy((*commit)->author, imsg->data + len,
600 icommit->author_len);
601 (*commit)->author[icommit->author_len] = '\0';
603 len += icommit->author_len;
605 if (icommit->committer_len == 0) {
606 (*commit)->committer = strdup("");
607 if ((*commit)->committer == NULL) {
608 err = got_error_from_errno("strdup");
609 goto done;
611 } else {
612 (*commit)->committer =
613 malloc(icommit->committer_len + 1);
614 if ((*commit)->committer == NULL) {
615 err = got_error_from_errno("malloc");
616 goto done;
618 memcpy((*commit)->committer, imsg->data + len,
619 icommit->committer_len);
620 (*commit)->committer[icommit->committer_len] = '\0';
622 len += icommit->committer_len;
624 if (icommit->logmsg_len == 0) {
625 (*commit)->logmsg = strdup("");
626 if ((*commit)->logmsg == NULL) {
627 err = got_error_from_errno("strdup");
628 goto done;
630 } else {
631 size_t offset = 0, remain = icommit->logmsg_len;
633 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
634 if ((*commit)->logmsg == NULL) {
635 err = got_error_from_errno("malloc");
636 goto done;
638 while (remain > 0) {
639 struct imsg imsg_log;
640 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
641 remain);
643 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
644 if (err)
645 goto done;
647 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
648 err = got_error(GOT_ERR_PRIVSEP_MSG);
649 goto done;
652 memcpy((*commit)->logmsg + offset,
653 imsg_log.data, n);
654 imsg_free(&imsg_log);
655 offset += n;
656 remain -= n;
658 (*commit)->logmsg[icommit->logmsg_len] = '\0';
661 for (i = 0; i < icommit->nparents; i++) {
662 struct got_object_qid *qid;
664 err = got_object_qid_alloc_partial(&qid);
665 if (err)
666 break;
667 memcpy(qid->id, imsg->data + len +
668 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
669 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
670 (*commit)->nparents++;
672 done:
673 if (err) {
674 got_object_commit_close(*commit);
675 *commit = NULL;
677 return err;
680 const struct got_error *
681 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
683 const struct got_error *err = NULL;
684 struct imsg imsg;
685 size_t datalen;
686 const size_t min_datalen =
687 MIN(sizeof(struct got_imsg_error),
688 sizeof(struct got_imsg_commit_object));
690 *commit = NULL;
692 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
693 if (err)
694 return err;
696 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
698 switch (imsg.hdr.type) {
699 case GOT_IMSG_COMMIT:
700 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
701 break;
702 default:
703 err = got_error(GOT_ERR_PRIVSEP_MSG);
704 break;
707 imsg_free(&imsg);
709 return err;
712 const struct got_error *
713 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
714 int nentries)
716 const struct got_error *err = NULL;
717 struct got_imsg_tree_object itree;
718 struct got_pathlist_entry *pe;
719 size_t totlen;
720 int nimsg; /* number of imsg queued in ibuf */
722 itree.nentries = nentries;
723 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
724 == -1)
725 return got_error_from_errno("imsg_compose TREE");
727 totlen = sizeof(itree);
728 nimsg = 1;
729 TAILQ_FOREACH(pe, entries, entry) {
730 const char *name = pe->path;
731 struct got_parsed_tree_entry *pte = pe->data;
732 struct ibuf *wbuf;
733 size_t namelen = strlen(name);
734 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
736 if (len > MAX_IMSGSIZE)
737 return got_error(GOT_ERR_NO_SPACE);
739 nimsg++;
740 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
741 err = flush_imsg(ibuf);
742 if (err)
743 return err;
744 nimsg = 0;
747 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
748 if (wbuf == NULL)
749 return got_error_from_errno("imsg_create TREE_ENTRY");
751 /* Keep in sync with struct got_imsg_tree_object definition! */
752 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
753 err = got_error_from_errno("imsg_add TREE_ENTRY");
754 ibuf_free(wbuf);
755 return err;
757 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
758 err = got_error_from_errno("imsg_add TREE_ENTRY");
759 ibuf_free(wbuf);
760 return err;
763 if (imsg_add(wbuf, name, namelen) == -1) {
764 err = got_error_from_errno("imsg_add TREE_ENTRY");
765 ibuf_free(wbuf);
766 return err;
769 wbuf->fd = -1;
770 imsg_close(ibuf, wbuf);
772 totlen += len;
775 return flush_imsg(ibuf);
778 const struct got_error *
779 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
781 const struct got_error *err = NULL;
782 const size_t min_datalen =
783 MIN(sizeof(struct got_imsg_error),
784 sizeof(struct got_imsg_tree_object));
785 struct got_imsg_tree_object *itree;
786 int nentries = 0;
788 *tree = NULL;
789 get_more:
790 err = read_imsg(ibuf);
791 if (err)
792 goto done;
794 for (;;) {
795 struct imsg imsg;
796 size_t n;
797 size_t datalen;
798 struct got_imsg_tree_entry *ite;
799 struct got_tree_entry *te = NULL;
801 n = imsg_get(ibuf, &imsg);
802 if (n == 0) {
803 if (*tree && (*tree)->nentries != nentries)
804 goto get_more;
805 break;
808 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
809 return got_error(GOT_ERR_PRIVSEP_LEN);
811 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
813 switch (imsg.hdr.type) {
814 case GOT_IMSG_ERROR:
815 err = recv_imsg_error(&imsg, datalen);
816 break;
817 case GOT_IMSG_TREE:
818 /* This message should only appear once. */
819 if (*tree != NULL) {
820 err = got_error(GOT_ERR_PRIVSEP_MSG);
821 break;
823 if (datalen != sizeof(*itree)) {
824 err = got_error(GOT_ERR_PRIVSEP_LEN);
825 break;
827 itree = imsg.data;
828 *tree = malloc(sizeof(**tree));
829 if (*tree == NULL) {
830 err = got_error_from_errno("malloc");
831 break;
833 (*tree)->entries = calloc(itree->nentries,
834 sizeof(struct got_tree_entry));
835 if ((*tree)->entries == NULL) {
836 err = got_error_from_errno("malloc");
837 break;
839 (*tree)->nentries = itree->nentries;
840 (*tree)->refcnt = 0;
841 break;
842 case GOT_IMSG_TREE_ENTRY:
843 /* This message should be preceeded by GOT_IMSG_TREE. */
844 if (*tree == NULL) {
845 err = got_error(GOT_ERR_PRIVSEP_MSG);
846 break;
848 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
849 err = got_error(GOT_ERR_PRIVSEP_LEN);
850 break;
853 /* Remaining data contains the entry's name. */
854 datalen -= sizeof(*ite);
855 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
856 err = got_error(GOT_ERR_PRIVSEP_LEN);
857 break;
859 ite = imsg.data;
861 if (datalen + 1 > sizeof(te->name)) {
862 err = got_error(GOT_ERR_NO_SPACE);
863 break;
865 te = &(*tree)->entries[nentries];
866 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
867 te->name[datalen] = '\0';
869 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
870 te->mode = ite->mode;
871 te->idx = nentries;
872 nentries++;
873 break;
874 default:
875 err = got_error(GOT_ERR_PRIVSEP_MSG);
876 break;
879 imsg_free(&imsg);
881 done:
882 if (*tree && (*tree)->nentries != nentries) {
883 if (err == NULL)
884 err = got_error(GOT_ERR_PRIVSEP_LEN);
885 got_object_tree_close(*tree);
886 *tree = NULL;
889 return err;
892 const struct got_error *
893 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
894 const uint8_t *data)
896 struct got_imsg_blob iblob;
898 iblob.size = size;
899 iblob.hdrlen = hdrlen;
901 if (data) {
902 uint8_t *buf;
904 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
905 return got_error(GOT_ERR_NO_SPACE);
907 buf = malloc(sizeof(iblob) + size);
908 if (buf == NULL)
909 return got_error_from_errno("malloc");
911 memcpy(buf, &iblob, sizeof(iblob));
912 memcpy(buf + sizeof(iblob), data, size);
913 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
914 sizeof(iblob) + size) == -1) {
915 free(buf);
916 return got_error_from_errno("imsg_compose BLOB");
918 free(buf);
919 } else {
920 /* Data has already been written to file descriptor. */
921 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
922 sizeof(iblob)) == -1)
923 return got_error_from_errno("imsg_compose BLOB");
927 return flush_imsg(ibuf);
930 const struct got_error *
931 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
932 struct imsgbuf *ibuf)
934 const struct got_error *err = NULL;
935 struct imsg imsg;
936 struct got_imsg_blob *iblob;
937 size_t datalen;
939 *outbuf = NULL;
941 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
942 if (err)
943 return err;
945 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
947 switch (imsg.hdr.type) {
948 case GOT_IMSG_BLOB:
949 if (datalen < sizeof(*iblob)) {
950 err = got_error(GOT_ERR_PRIVSEP_LEN);
951 break;
953 iblob = imsg.data;
954 *size = iblob->size;
955 *hdrlen = iblob->hdrlen;
957 if (datalen == sizeof(*iblob)) {
958 /* Data has been written to file descriptor. */
959 break;
962 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
963 err = got_error(GOT_ERR_PRIVSEP_LEN);
964 break;
967 *outbuf = malloc(*size);
968 if (*outbuf == NULL) {
969 err = got_error_from_errno("malloc");
970 break;
972 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
973 break;
974 default:
975 err = got_error(GOT_ERR_PRIVSEP_MSG);
976 break;
979 imsg_free(&imsg);
981 return err;
984 static const struct got_error *
985 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
987 const struct got_error *err = NULL;
988 size_t offset, remain;
990 offset = 0;
991 remain = tagmsg_len;
992 while (remain > 0) {
993 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
995 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
996 tag->tagmsg + offset, n) == -1) {
997 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
998 break;
1001 err = flush_imsg(ibuf);
1002 if (err)
1003 break;
1005 offset += n;
1006 remain -= n;
1009 return err;
1012 const struct got_error *
1013 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1015 const struct got_error *err = NULL;
1016 struct got_imsg_tag_object *itag;
1017 uint8_t *buf;
1018 size_t len, total;
1019 size_t tag_len = strlen(tag->tag);
1020 size_t tagger_len = strlen(tag->tagger);
1021 size_t tagmsg_len = strlen(tag->tagmsg);
1023 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1025 buf = malloc(total);
1026 if (buf == NULL)
1027 return got_error_from_errno("malloc");
1029 itag = (struct got_imsg_tag_object *)buf;
1030 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1031 itag->obj_type = tag->obj_type;
1032 itag->tag_len = tag_len;
1033 itag->tagger_len = tagger_len;
1034 itag->tagger_time = tag->tagger_time;
1035 itag->tagger_gmtoff = tag->tagger_gmtoff;
1036 itag->tagmsg_len = tagmsg_len;
1038 len = sizeof(*itag);
1039 memcpy(buf + len, tag->tag, tag_len);
1040 len += tag_len;
1041 memcpy(buf + len, tag->tagger, tagger_len);
1042 len += tagger_len;
1044 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1045 err = got_error_from_errno("imsg_compose TAG");
1046 goto done;
1049 if (tagmsg_len == 0 ||
1050 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1051 err = flush_imsg(ibuf);
1052 if (err)
1053 goto done;
1055 err = send_tagmsg(ibuf, tag, tagmsg_len);
1056 done:
1057 free(buf);
1058 return err;
1061 const struct got_error *
1062 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1064 const struct got_error *err = NULL;
1065 struct imsg imsg;
1066 struct got_imsg_tag_object *itag;
1067 size_t len, datalen;
1068 const size_t min_datalen =
1069 MIN(sizeof(struct got_imsg_error),
1070 sizeof(struct got_imsg_tag_object));
1072 *tag = NULL;
1074 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1075 if (err)
1076 return err;
1078 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1079 len = 0;
1081 switch (imsg.hdr.type) {
1082 case GOT_IMSG_TAG:
1083 if (datalen < sizeof(*itag)) {
1084 err = got_error(GOT_ERR_PRIVSEP_LEN);
1085 break;
1087 itag = imsg.data;
1088 if (datalen != sizeof(*itag) + itag->tag_len +
1089 itag->tagger_len) {
1090 err = got_error(GOT_ERR_PRIVSEP_LEN);
1091 break;
1093 len += sizeof(*itag);
1095 *tag = calloc(1, sizeof(**tag));
1096 if (*tag == NULL) {
1097 err = got_error_from_errno("calloc");
1098 break;
1101 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1103 if (itag->tag_len == 0) {
1104 (*tag)->tag = strdup("");
1105 if ((*tag)->tag == NULL) {
1106 err = got_error_from_errno("strdup");
1107 break;
1109 } else {
1110 (*tag)->tag = malloc(itag->tag_len + 1);
1111 if ((*tag)->tag == NULL) {
1112 err = got_error_from_errno("malloc");
1113 break;
1115 memcpy((*tag)->tag, imsg.data + len,
1116 itag->tag_len);
1117 (*tag)->tag[itag->tag_len] = '\0';
1119 len += itag->tag_len;
1121 (*tag)->obj_type = itag->obj_type;
1122 (*tag)->tagger_time = itag->tagger_time;
1123 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1125 if (itag->tagger_len == 0) {
1126 (*tag)->tagger = strdup("");
1127 if ((*tag)->tagger == NULL) {
1128 err = got_error_from_errno("strdup");
1129 break;
1131 } else {
1132 (*tag)->tagger = malloc(itag->tagger_len + 1);
1133 if ((*tag)->tagger == NULL) {
1134 err = got_error_from_errno("malloc");
1135 break;
1137 memcpy((*tag)->tagger, imsg.data + len,
1138 itag->tagger_len);
1139 (*tag)->tagger[itag->tagger_len] = '\0';
1141 len += itag->tagger_len;
1143 if (itag->tagmsg_len == 0) {
1144 (*tag)->tagmsg = strdup("");
1145 if ((*tag)->tagmsg == NULL) {
1146 err = got_error_from_errno("strdup");
1147 break;
1149 } else {
1150 size_t offset = 0, remain = itag->tagmsg_len;
1152 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1153 if ((*tag)->tagmsg == NULL) {
1154 err = got_error_from_errno("malloc");
1155 break;
1157 while (remain > 0) {
1158 struct imsg imsg_log;
1159 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1160 remain);
1162 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1163 if (err)
1164 return err;
1166 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1167 return got_error(GOT_ERR_PRIVSEP_MSG);
1169 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1170 n);
1171 imsg_free(&imsg_log);
1172 offset += n;
1173 remain -= n;
1175 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1178 break;
1179 default:
1180 err = got_error(GOT_ERR_PRIVSEP_MSG);
1181 break;
1184 imsg_free(&imsg);
1186 return err;
1189 const struct got_error *
1190 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1191 struct got_packidx *packidx)
1193 const struct got_error *err = NULL;
1194 struct got_imsg_packidx ipackidx;
1195 struct got_imsg_pack ipack;
1196 int fd;
1198 ipackidx.len = packidx->len;
1199 fd = dup(packidx->fd);
1200 if (fd == -1)
1201 return got_error_from_errno("dup");
1203 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1204 sizeof(ipackidx)) == -1) {
1205 err = got_error_from_errno("imsg_compose PACKIDX");
1206 close(fd);
1207 return err;
1210 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1211 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1212 return got_error(GOT_ERR_NO_SPACE);
1213 ipack.filesize = pack->filesize;
1215 fd = dup(pack->fd);
1216 if (fd == -1)
1217 return got_error_from_errno("dup");
1219 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1220 == -1) {
1221 err = got_error_from_errno("imsg_compose PACK");
1222 close(fd);
1223 return err;
1226 return flush_imsg(ibuf);
1229 const struct got_error *
1230 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1231 struct got_object_id *id)
1233 struct got_imsg_packed_object iobj;
1235 iobj.idx = idx;
1236 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1238 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1239 &iobj, sizeof(iobj)) == -1)
1240 return got_error_from_errno("imsg_compose "
1241 "PACKED_OBJECT_REQUEST");
1243 return flush_imsg(ibuf);
1246 const struct got_error *
1247 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1249 const struct got_error *err = NULL;
1251 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1252 NULL, 0) == -1) {
1253 err = got_error_from_errno("imsg_compose "
1254 "GITCONFIG_PARSE_REQUEST");
1255 close(fd);
1256 return err;
1259 return flush_imsg(ibuf);
1262 const struct got_error *
1263 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1265 if (imsg_compose(ibuf,
1266 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1267 NULL, 0) == -1)
1268 return got_error_from_errno("imsg_compose "
1269 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1271 return flush_imsg(ibuf);
1274 const struct got_error *
1275 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1277 if (imsg_compose(ibuf,
1278 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1279 return got_error_from_errno("imsg_compose "
1280 "GITCONFIG_AUTHOR_NAME_REQUEST");
1282 return flush_imsg(ibuf);
1285 const struct got_error *
1286 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1288 if (imsg_compose(ibuf,
1289 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1290 return got_error_from_errno("imsg_compose "
1291 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1293 return flush_imsg(ibuf);
1296 const struct got_error *
1297 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1299 if (imsg_compose(ibuf,
1300 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1301 return got_error_from_errno("imsg_compose "
1302 "GITCONFIG_REMOTE_REQUEST");
1304 return flush_imsg(ibuf);
1307 const struct got_error *
1308 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1310 size_t len = value ? strlen(value) + 1 : 0;
1312 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1313 value, len) == -1)
1314 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1316 return flush_imsg(ibuf);
1319 const struct got_error *
1320 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1322 const struct got_error *err = NULL;
1323 struct imsg imsg;
1324 size_t datalen;
1325 const size_t min_datalen = 0;
1327 *str = NULL;
1329 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1330 if (err)
1331 return err;
1332 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1334 switch (imsg.hdr.type) {
1335 case GOT_IMSG_GITCONFIG_STR_VAL:
1336 if (datalen == 0)
1337 break;
1338 *str = malloc(datalen);
1339 if (*str == NULL) {
1340 err = got_error_from_errno("malloc");
1341 break;
1343 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1344 err = got_error(GOT_ERR_NO_SPACE);
1345 break;
1346 default:
1347 err = got_error(GOT_ERR_PRIVSEP_MSG);
1348 break;
1351 imsg_free(&imsg);
1352 return err;
1355 const struct got_error *
1356 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1358 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1359 &value, sizeof(value)) == -1)
1360 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1362 return flush_imsg(ibuf);
1365 const struct got_error *
1366 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1368 const struct got_error *err = NULL;
1369 struct imsg imsg;
1370 size_t datalen;
1371 const size_t min_datalen =
1372 MIN(sizeof(struct got_imsg_error), sizeof(int));
1374 *val = 0;
1376 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1377 if (err)
1378 return err;
1379 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1381 switch (imsg.hdr.type) {
1382 case GOT_IMSG_GITCONFIG_INT_VAL:
1383 if (datalen != sizeof(*val)) {
1384 err = got_error(GOT_ERR_PRIVSEP_LEN);
1385 break;
1387 memcpy(val, imsg.data, sizeof(*val));
1388 break;
1389 default:
1390 err = got_error(GOT_ERR_PRIVSEP_MSG);
1391 break;
1394 imsg_free(&imsg);
1395 return err;
1398 const struct got_error *
1399 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1400 struct got_remote_repo *remotes, int nremotes)
1402 const struct got_error *err = NULL;
1403 struct got_imsg_remotes iremotes;
1404 int i;
1406 iremotes.nremotes = nremotes;
1407 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1408 &iremotes, sizeof(iremotes)) == -1)
1409 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1411 err = flush_imsg(ibuf);
1412 imsg_clear(ibuf);
1413 if (err)
1414 return err;
1416 for (i = 0; i < nremotes; i++) {
1417 struct got_imsg_remote iremote;
1418 size_t len = sizeof(iremote);
1419 struct ibuf *wbuf;
1421 iremote.name_len = strlen(remotes[i].name);
1422 len += iremote.name_len;
1423 iremote.url_len = strlen(remotes[i].url);
1424 len += iremote.url_len;
1426 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1427 if (wbuf == NULL)
1428 return got_error_from_errno(
1429 "imsg_create GITCONFIG_REMOTE");
1431 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1432 err = got_error_from_errno(
1433 "imsg_add GIITCONFIG_REMOTE");
1434 ibuf_free(wbuf);
1435 return err;
1438 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1439 err = got_error_from_errno(
1440 "imsg_add GIITCONFIG_REMOTE");
1441 ibuf_free(wbuf);
1442 return err;
1444 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1445 err = got_error_from_errno(
1446 "imsg_add GIITCONFIG_REMOTE");
1447 ibuf_free(wbuf);
1448 return err;
1451 wbuf->fd = -1;
1452 imsg_close(ibuf, wbuf);
1453 err = flush_imsg(ibuf);
1454 if (err)
1455 return err;
1458 return NULL;
1461 const struct got_error *
1462 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1463 int *nremotes, struct imsgbuf *ibuf)
1465 const struct got_error *err = NULL;
1466 struct imsg imsg;
1467 size_t datalen;
1468 struct got_imsg_remotes iremotes;
1469 struct got_imsg_remote iremote;
1471 *remotes = NULL;
1472 *nremotes = 0;
1474 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1475 if (err)
1476 return err;
1477 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1479 switch (imsg.hdr.type) {
1480 case GOT_IMSG_GITCONFIG_REMOTES:
1481 if (datalen != sizeof(iremotes)) {
1482 err = got_error(GOT_ERR_PRIVSEP_LEN);
1483 break;
1485 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1486 if (iremotes.nremotes == 0) {
1487 imsg_free(&imsg);
1488 return NULL;
1490 break;
1491 default:
1492 err = got_error(GOT_ERR_PRIVSEP_MSG);
1493 break;
1496 imsg_free(&imsg);
1498 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1499 if (*remotes == NULL)
1500 return got_error_from_errno("recallocarray");
1502 while (*nremotes < iremotes.nremotes) {
1503 struct got_remote_repo *remote;
1505 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1506 if (err)
1507 break;
1508 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1510 switch (imsg.hdr.type) {
1511 case GOT_IMSG_GITCONFIG_REMOTE:
1512 remote = &(*remotes)[*nremotes];
1513 if (datalen < sizeof(iremote)) {
1514 err = got_error(GOT_ERR_PRIVSEP_LEN);
1515 break;
1517 memcpy(&iremote, imsg.data, sizeof(iremote));
1518 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1519 (sizeof(iremote) + iremote.name_len +
1520 iremote.url_len) > datalen) {
1521 err = got_error(GOT_ERR_PRIVSEP_LEN);
1522 break;
1524 remote->name = strndup(imsg.data + sizeof(iremote),
1525 iremote.name_len);
1526 if (remote->name == NULL) {
1527 err = got_error_from_errno("strndup");
1528 break;
1530 remote->url = strndup(imsg.data + sizeof(iremote) +
1531 iremote.name_len, iremote.url_len);
1532 if (remote->url == NULL) {
1533 err = got_error_from_errno("strndup");
1534 free(remote->name);
1535 break;
1537 (*nremotes)++;
1538 break;
1539 default:
1540 err = got_error(GOT_ERR_PRIVSEP_MSG);
1541 break;
1544 imsg_free(&imsg);
1545 if (err)
1546 break;
1549 if (err) {
1550 int i;
1551 for (i = 0; i < *nremotes; i++) {
1552 free((*remotes)[i].name);
1553 free((*remotes)[i].url);
1555 free(*remotes);
1556 *remotes = NULL;
1557 *nremotes = 0;
1559 return err;
1562 const struct got_error *
1563 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1564 struct got_object_id *id, int idx, const char *path)
1566 const struct got_error *err = NULL;
1567 struct ibuf *wbuf;
1568 size_t path_len = strlen(path) + 1;
1570 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1571 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1572 if (wbuf == NULL)
1573 return got_error_from_errno(
1574 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1575 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1576 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1577 ibuf_free(wbuf);
1578 return err;
1580 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1581 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1582 ibuf_free(wbuf);
1583 return err;
1585 if (imsg_add(wbuf, path, path_len) == -1) {
1586 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1587 ibuf_free(wbuf);
1588 return err;
1591 wbuf->fd = -1;
1592 imsg_close(ibuf, wbuf);
1594 return flush_imsg(ibuf);
1597 const struct got_error *
1598 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1599 size_t ncommits, struct imsgbuf *ibuf)
1601 const struct got_error *err;
1602 struct ibuf *wbuf;
1603 int i;
1605 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
1606 sizeof(struct got_imsg_traversed_commits) +
1607 ncommits * SHA1_DIGEST_LENGTH);
1608 if (wbuf == NULL)
1609 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
1611 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
1612 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
1613 ibuf_free(wbuf);
1614 return err;
1616 for (i = 0; i < ncommits; i++) {
1617 struct got_object_id *id = &commit_ids[i];
1618 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1619 err = got_error_from_errno(
1620 "imsg_add TRAVERSED_COMMITS");
1621 ibuf_free(wbuf);
1622 return err;
1626 wbuf->fd = -1;
1627 imsg_close(ibuf, wbuf);
1629 return flush_imsg(ibuf);
1632 const struct got_error *
1633 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1634 struct got_object_id **changed_commit_id,
1635 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1637 const struct got_error *err = NULL;
1638 struct imsg imsg;
1639 struct got_imsg_traversed_commits *icommits;
1640 size_t datalen;
1641 int i, done = 0;
1643 *changed_commit = NULL;
1644 *changed_commit_id = NULL;
1646 while (!done) {
1647 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1648 if (err)
1649 return err;
1651 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1652 switch (imsg.hdr.type) {
1653 case GOT_IMSG_TRAVERSED_COMMITS:
1654 icommits = imsg.data;
1655 if (datalen != sizeof(*icommits) +
1656 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1657 err = got_error(GOT_ERR_PRIVSEP_LEN);
1658 break;
1660 for (i = 0; i < icommits->ncommits; i++) {
1661 struct got_object_qid *qid;
1662 uint8_t *sha1 = (uint8_t *)imsg.data +
1663 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1664 err = got_object_qid_alloc_partial(&qid);
1665 if (err)
1666 break;
1667 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1668 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1670 /* The last commit may contain a change. */
1671 if (i == icommits->ncommits - 1) {
1672 *changed_commit_id =
1673 got_object_id_dup(qid->id);
1674 if (*changed_commit_id == NULL) {
1675 err = got_error_from_errno(
1676 "got_object_id_dup");
1677 break;
1681 break;
1682 case GOT_IMSG_COMMIT:
1683 if (*changed_commit_id == NULL) {
1684 err = got_error(GOT_ERR_PRIVSEP_MSG);
1685 break;
1687 err = get_commit_from_imsg(changed_commit, &imsg,
1688 datalen, ibuf);
1689 break;
1690 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1691 done = 1;
1692 break;
1693 default:
1694 err = got_error(GOT_ERR_PRIVSEP_MSG);
1695 break;
1698 imsg_free(&imsg);
1699 if (err)
1700 break;
1703 if (err)
1704 got_object_id_queue_free(commit_ids);
1705 return err;
1708 const struct got_error *
1709 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
1711 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
1712 NULL, 0) == -1)
1713 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
1715 return flush_imsg(ibuf);
1718 const struct got_error *
1719 got_privsep_unveil_exec_helpers(void)
1721 const char *helpers[] = {
1722 GOT_PATH_PROG_READ_PACK,
1723 GOT_PATH_PROG_READ_OBJECT,
1724 GOT_PATH_PROG_READ_COMMIT,
1725 GOT_PATH_PROG_READ_TREE,
1726 GOT_PATH_PROG_READ_BLOB,
1727 GOT_PATH_PROG_READ_TAG,
1728 GOT_PATH_PROG_READ_GITCONFIG,
1730 int i;
1732 for (i = 0; i < nitems(helpers); i++) {
1733 if (unveil(helpers[i], "x") == 0)
1734 continue;
1735 return got_error_from_errno2("unveil", helpers[i]);
1738 return NULL;
1741 void
1742 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1744 if (close(imsg_fds[0]) != 0) {
1745 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1746 _exit(1);
1749 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1750 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1751 _exit(1);
1753 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1754 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1755 _exit(1);
1758 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1759 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1760 strerror(errno));
1761 _exit(1);