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 <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <stdint.h>
30 #include <poll.h>
31 #include <imsg.h>
32 #include <sha1.h>
33 #include <zlib.h>
34 #include <time.h>
36 #include "got_object.h"
37 #include "got_error.h"
38 #include "got_path.h"
39 #include "got_repository.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_pack.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 static const struct got_error *
58 poll_fd(int fd, int events, int timeout)
59 {
60 struct pollfd pfd[1];
61 int n;
63 pfd[0].fd = fd;
64 pfd[0].events = events;
66 n = poll(pfd, 1, timeout);
67 if (n == -1)
68 return got_error_from_errno("poll");
69 if (n == 0)
70 return got_error(GOT_ERR_TIMEOUT);
71 if (pfd[0].revents & (POLLERR | POLLNVAL))
72 return got_error_from_errno("poll error");
73 if (pfd[0].revents & (events | POLLHUP))
74 return NULL;
76 return got_error(GOT_ERR_INTERRUPT);
77 }
79 static const struct got_error *
80 read_imsg(struct imsgbuf *ibuf)
81 {
82 const struct got_error *err;
83 size_t n;
85 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
86 if (err)
87 return err;
89 n = imsg_read(ibuf);
90 if (n == -1) {
91 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
92 return got_error(GOT_ERR_PRIVSEP_NO_FD);
93 return got_error(GOT_ERR_PRIVSEP_READ);
94 }
95 if (n == 0)
96 return got_error(GOT_ERR_PRIVSEP_PIPE);
98 return NULL;
99 }
101 const struct got_error *
102 got_privsep_wait_for_child(pid_t pid)
104 int child_status;
106 if (waitpid(pid, &child_status, 0) == -1)
107 return got_error_from_errno("waitpid");
109 if (!WIFEXITED(child_status))
110 return got_error(GOT_ERR_PRIVSEP_DIED);
112 if (WEXITSTATUS(child_status) != 0)
113 return got_error(GOT_ERR_PRIVSEP_EXIT);
115 return NULL;
118 static const struct got_error *
119 recv_imsg_error(struct imsg *imsg, size_t datalen)
121 struct got_imsg_error *ierr;
123 if (datalen != sizeof(*ierr))
124 return got_error(GOT_ERR_PRIVSEP_LEN);
126 ierr = imsg->data;
127 if (ierr->code == GOT_ERR_ERRNO) {
128 static struct got_error serr;
129 serr.code = GOT_ERR_ERRNO;
130 serr.msg = strerror(ierr->errno_code);
131 return &serr;
134 return got_error(ierr->code);
137 const struct got_error *
138 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
139 size_t min_datalen)
141 const struct got_error *err;
142 ssize_t n;
144 n = imsg_get(ibuf, imsg);
145 if (n == -1)
146 return got_error_from_errno("imsg_get");
148 while (n == 0) {
149 err = read_imsg(ibuf);
150 if (err)
151 return err;
152 n = imsg_get(ibuf, imsg);
153 if (n == -1)
154 return got_error_from_errno("imsg_get");
157 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
158 return got_error(GOT_ERR_PRIVSEP_LEN);
160 if (imsg->hdr.type == GOT_IMSG_ERROR) {
161 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 return recv_imsg_error(imsg, datalen);
165 return NULL;
168 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
169 void
170 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
172 const struct got_error *poll_err;
173 struct got_imsg_error ierr;
174 int ret;
176 ierr.code = err->code;
177 if (err->code == GOT_ERR_ERRNO)
178 ierr.errno_code = errno;
179 else
180 ierr.errno_code = 0;
181 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
182 if (ret == -1) {
183 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
184 getprogname(), err->code, err->msg, strerror(errno));
185 return;
188 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
189 if (poll_err) {
190 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
191 getprogname(), err->code, err->msg, poll_err->msg);
192 return;
195 ret = imsg_flush(ibuf);
196 if (ret == -1) {
197 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
198 getprogname(), err->code, err->msg, strerror(errno));
199 return;
203 static const struct got_error *
204 flush_imsg(struct imsgbuf *ibuf)
206 const struct got_error *err;
208 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
209 if (err)
210 return err;
212 if (imsg_flush(ibuf) == -1)
213 return got_error_from_errno("imsg_flush");
215 return NULL;
218 const struct got_error *
219 got_privsep_send_stop(int fd)
221 const struct got_error *err = NULL;
222 struct imsgbuf ibuf;
224 imsg_init(&ibuf, fd);
226 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
227 return got_error_from_errno("imsg_compose STOP");
229 err = flush_imsg(&ibuf);
230 imsg_clear(&ibuf);
231 return err;
234 const struct got_error *
235 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
237 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
238 == -1)
239 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
241 return flush_imsg(ibuf);
244 const struct got_error *
245 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
246 struct got_object_id *id, int pack_idx)
248 const struct got_error *err = NULL;
249 struct got_imsg_packed_object iobj, *iobjp;
250 size_t len;
252 if (id) { /* commit is packed */
253 iobj.idx = pack_idx;
254 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
255 iobjp = &iobj;
256 len = sizeof(iobj);
257 } else {
258 iobjp = NULL;
259 len = 0;
262 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
263 == -1) {
264 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
265 close(fd);
266 return err;
269 return flush_imsg(ibuf);
272 const struct got_error *
273 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
274 struct got_object_id *id, int pack_idx)
276 const struct got_error *err = NULL;
277 struct ibuf *wbuf;
278 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
280 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
281 if (wbuf == NULL)
282 return got_error_from_errno("imsg_create TREE_REQUEST");
284 if (id) { /* tree is packed */
285 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
286 err = got_error_from_errno("imsg_add TREE_ENTRY");
287 ibuf_free(wbuf);
288 return err;
291 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
292 err = got_error_from_errno("imsg_add TREE_ENTRY");
293 ibuf_free(wbuf);
294 return err;
298 wbuf->fd = fd;
299 imsg_close(ibuf, wbuf);
301 return flush_imsg(ibuf);
304 const struct got_error *
305 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
306 struct got_object_id *id, int pack_idx)
308 struct got_imsg_packed_object iobj, *iobjp;
309 size_t len;
311 if (id) { /* tag is packed */
312 iobj.idx = pack_idx;
313 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
314 iobjp = &iobj;
315 len = sizeof(iobj);
316 } else {
317 iobjp = NULL;
318 len = 0;
321 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
322 == -1)
323 return got_error_from_errno("imsg_compose TAG_REQUEST");
325 return flush_imsg(ibuf);
328 const struct got_error *
329 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
330 struct got_object_id *id, int pack_idx)
332 const struct got_error *err = NULL;
333 struct got_imsg_packed_object iobj, *iobjp;
334 size_t len;
336 if (id) { /* blob is packed */
337 iobj.idx = pack_idx;
338 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
339 iobjp = &iobj;
340 len = sizeof(iobj);
341 } else {
342 iobjp = NULL;
343 len = 0;
346 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
347 == -1) {
348 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
349 close(infd);
350 return err;
353 return flush_imsg(ibuf);
356 const struct got_error *
357 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
359 const struct got_error *err = NULL;
361 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
362 == -1) {
363 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
364 close(outfd);
365 return err;
368 return flush_imsg(ibuf);
371 static const struct got_error *
372 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
374 const struct got_error *err = NULL;
376 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -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_tmpfd(struct imsgbuf *ibuf, int fd)
388 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
391 const struct got_error *
392 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
394 struct got_imsg_object iobj;
396 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
397 iobj.type = obj->type;
398 iobj.flags = obj->flags;
399 iobj.hdrlen = obj->hdrlen;
400 iobj.size = obj->size;
401 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
402 iobj.pack_offset = obj->pack_offset;
403 iobj.pack_idx = obj->pack_idx;
406 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
407 == -1)
408 return got_error_from_errno("imsg_compose OBJECT");
410 return flush_imsg(ibuf);
413 const struct got_error *
414 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
415 struct got_pathlist_head *have_refs, int fetch_all_branches,
416 struct got_pathlist_head *wanted_branches,
417 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
419 const struct got_error *err = NULL;
420 struct ibuf *wbuf;
421 size_t len;
422 struct got_pathlist_entry *pe;
423 struct got_imsg_fetch_request fetchreq;
425 memset(&fetchreq, 0, sizeof(fetchreq));
426 fetchreq.fetch_all_branches = fetch_all_branches;
427 fetchreq.list_refs_only = list_refs_only;
428 fetchreq.verbosity = verbosity;
429 TAILQ_FOREACH(pe, have_refs, entry)
430 fetchreq.n_have_refs++;
431 TAILQ_FOREACH(pe, wanted_branches, entry)
432 fetchreq.n_wanted_branches++;
433 TAILQ_FOREACH(pe, wanted_refs, entry)
434 fetchreq.n_wanted_refs++;
435 len = sizeof(struct got_imsg_fetch_request);
436 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
437 close(fd);
438 return got_error(GOT_ERR_NO_SPACE);
441 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
442 &fetchreq, sizeof(fetchreq)) == -1)
443 return got_error_from_errno(
444 "imsg_compose FETCH_SERVER_PROGRESS");
446 err = flush_imsg(ibuf);
447 if (err) {
448 close(fd);
449 return err;
451 fd = -1;
453 TAILQ_FOREACH(pe, have_refs, entry) {
454 const char *name = pe->path;
455 size_t name_len = pe->path_len;
456 struct got_object_id *id = pe->data;
458 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
459 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
460 if (wbuf == NULL)
461 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
463 /* Keep in sync with struct got_imsg_fetch_have_ref! */
464 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
465 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
466 ibuf_free(wbuf);
467 return err;
469 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
470 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
471 ibuf_free(wbuf);
472 return err;
474 if (imsg_add(wbuf, name, name_len) == -1) {
475 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
476 ibuf_free(wbuf);
477 return err;
480 wbuf->fd = -1;
481 imsg_close(ibuf, wbuf);
482 err = flush_imsg(ibuf);
483 if (err)
484 return err;
487 TAILQ_FOREACH(pe, wanted_branches, entry) {
488 const char *name = pe->path;
489 size_t name_len = pe->path_len;
491 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
492 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
493 len);
494 if (wbuf == NULL)
495 return got_error_from_errno(
496 "imsg_create FETCH_WANTED_BRANCH");
498 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
499 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
500 err = got_error_from_errno(
501 "imsg_add FETCH_WANTED_BRANCH");
502 ibuf_free(wbuf);
503 return err;
505 if (imsg_add(wbuf, name, name_len) == -1) {
506 err = got_error_from_errno(
507 "imsg_add FETCH_WANTED_BRANCH");
508 ibuf_free(wbuf);
509 return err;
512 wbuf->fd = -1;
513 imsg_close(ibuf, wbuf);
514 err = flush_imsg(ibuf);
515 if (err)
516 return err;
519 TAILQ_FOREACH(pe, wanted_refs, entry) {
520 const char *name = pe->path;
521 size_t name_len = pe->path_len;
523 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
524 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
525 len);
526 if (wbuf == NULL)
527 return got_error_from_errno(
528 "imsg_create FETCH_WANTED_REF");
530 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
531 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
532 err = got_error_from_errno(
533 "imsg_add FETCH_WANTED_REF");
534 ibuf_free(wbuf);
535 return err;
537 if (imsg_add(wbuf, name, name_len) == -1) {
538 err = got_error_from_errno(
539 "imsg_add FETCH_WANTED_REF");
540 ibuf_free(wbuf);
541 return err;
544 wbuf->fd = -1;
545 imsg_close(ibuf, wbuf);
546 err = flush_imsg(ibuf);
547 if (err)
548 return err;
552 return NULL;
556 const struct got_error *
557 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
559 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
563 const struct got_error *
564 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
565 struct got_pathlist_head *symrefs)
567 const struct got_error *err = NULL;
568 struct ibuf *wbuf;
569 size_t len, nsymrefs = 0;
570 struct got_pathlist_entry *pe;
572 len = sizeof(struct got_imsg_fetch_symrefs);
573 TAILQ_FOREACH(pe, symrefs, entry) {
574 const char *target = pe->data;
575 len += sizeof(struct got_imsg_fetch_symref) +
576 pe->path_len + strlen(target);
577 nsymrefs++;
580 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
581 return got_error(GOT_ERR_NO_SPACE);
583 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
584 if (wbuf == NULL)
585 return got_error_from_errno("imsg_create FETCH_SYMREFS");
587 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
588 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
589 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
590 ibuf_free(wbuf);
591 return err;
594 TAILQ_FOREACH(pe, symrefs, entry) {
595 const char *name = pe->path;
596 size_t name_len = pe->path_len;
597 const char *target = pe->data;
598 size_t target_len = strlen(target);
600 /* Keep in sync with struct got_imsg_fetch_symref definition! */
601 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
602 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
603 ibuf_free(wbuf);
604 return err;
606 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
607 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
608 ibuf_free(wbuf);
609 return err;
611 if (imsg_add(wbuf, name, name_len) == -1) {
612 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
613 ibuf_free(wbuf);
614 return err;
616 if (imsg_add(wbuf, target, target_len) == -1) {
617 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
618 ibuf_free(wbuf);
619 return err;
623 wbuf->fd = -1;
624 imsg_close(ibuf, wbuf);
625 return flush_imsg(ibuf);
628 const struct got_error *
629 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
630 struct got_object_id *refid, const char *refname)
632 const struct got_error *err = NULL;
633 struct ibuf *wbuf;
634 size_t len, reflen = strlen(refname);
636 len = sizeof(struct got_imsg_fetch_ref) + reflen;
637 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
638 return got_error(GOT_ERR_NO_SPACE);
640 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
641 if (wbuf == NULL)
642 return got_error_from_errno("imsg_create FETCH_REF");
644 /* Keep in sync with struct got_imsg_fetch_ref definition! */
645 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
646 err = got_error_from_errno("imsg_add FETCH_REF");
647 ibuf_free(wbuf);
648 return err;
650 if (imsg_add(wbuf, refname, reflen) == -1) {
651 err = got_error_from_errno("imsg_add FETCH_REF");
652 ibuf_free(wbuf);
653 return err;
656 wbuf->fd = -1;
657 imsg_close(ibuf, wbuf);
658 return flush_imsg(ibuf);
661 const struct got_error *
662 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
663 size_t msglen)
665 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
666 return got_error(GOT_ERR_NO_SPACE);
668 if (msglen == 0)
669 return NULL;
671 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
672 msg, msglen) == -1)
673 return got_error_from_errno(
674 "imsg_compose FETCH_SERVER_PROGRESS");
676 return flush_imsg(ibuf);
679 const struct got_error *
680 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
682 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
683 &bytes, sizeof(bytes)) == -1)
684 return got_error_from_errno(
685 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
687 return flush_imsg(ibuf);
690 const struct got_error *
691 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
693 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
694 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
695 return got_error_from_errno("imsg_compose FETCH");
696 return flush_imsg(ibuf);
700 const struct got_error *
701 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
702 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
703 off_t *packfile_size, struct imsgbuf *ibuf)
705 const struct got_error *err = NULL;
706 struct imsg imsg;
707 size_t datalen;
708 struct got_imsg_fetch_symrefs *isymrefs = NULL;
709 size_t n, remain;
710 off_t off;
711 int i;
713 *done = 0;
714 *id = NULL;
715 *refname = NULL;
716 *server_progress = NULL;
717 *packfile_size = 0;
719 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
720 if (err)
721 return err;
723 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
724 switch (imsg.hdr.type) {
725 case GOT_IMSG_ERROR:
726 if (datalen < sizeof(struct got_imsg_error)) {
727 err = got_error(GOT_ERR_PRIVSEP_LEN);
728 break;
730 err = recv_imsg_error(&imsg, datalen);
731 break;
732 case GOT_IMSG_FETCH_SYMREFS:
733 if (datalen < sizeof(*isymrefs)) {
734 err = got_error(GOT_ERR_PRIVSEP_LEN);
735 break;
737 if (isymrefs != NULL) {
738 err = got_error(GOT_ERR_PRIVSEP_MSG);
739 break;
741 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
742 off = sizeof(*isymrefs);
743 remain = datalen - off;
744 for (n = 0; n < isymrefs->nsymrefs; n++) {
745 struct got_imsg_fetch_symref *s;
746 char *name, *target;
747 if (remain < sizeof(struct got_imsg_fetch_symref)) {
748 err = got_error(GOT_ERR_PRIVSEP_LEN);
749 goto done;
751 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
752 off += sizeof(*s);
753 remain -= sizeof(*s);
754 if (remain < s->name_len) {
755 err = got_error(GOT_ERR_PRIVSEP_LEN);
756 goto done;
758 name = strndup(imsg.data + off, s->name_len);
759 if (name == NULL) {
760 err = got_error_from_errno("strndup");
761 goto done;
763 off += s->name_len;
764 remain -= s->name_len;
765 if (remain < s->target_len) {
766 err = got_error(GOT_ERR_PRIVSEP_LEN);
767 free(name);
768 goto done;
770 target = strndup(imsg.data + off, s->target_len);
771 if (target == NULL) {
772 err = got_error_from_errno("strndup");
773 free(name);
774 goto done;
776 off += s->target_len;
777 remain -= s->target_len;
778 err = got_pathlist_append(symrefs, name, target);
779 if (err) {
780 free(name);
781 free(target);
782 goto done;
785 break;
786 case GOT_IMSG_FETCH_REF:
787 if (datalen <= SHA1_DIGEST_LENGTH) {
788 err = got_error(GOT_ERR_PRIVSEP_MSG);
789 break;
791 *id = malloc(sizeof(**id));
792 if (*id == NULL) {
793 err = got_error_from_errno("malloc");
794 break;
796 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
797 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
798 datalen - SHA1_DIGEST_LENGTH);
799 if (*refname == NULL) {
800 err = got_error_from_errno("strndup");
801 break;
803 break;
804 case GOT_IMSG_FETCH_SERVER_PROGRESS:
805 if (datalen == 0) {
806 err = got_error(GOT_ERR_PRIVSEP_LEN);
807 break;
809 *server_progress = strndup(imsg.data, datalen);
810 if (*server_progress == NULL) {
811 err = got_error_from_errno("strndup");
812 break;
814 for (i = 0; i < datalen; i++) {
815 if (!isprint((unsigned char)(*server_progress)[i]) &&
816 !isspace((unsigned char)(*server_progress)[i])) {
817 err = got_error(GOT_ERR_PRIVSEP_MSG);
818 free(*server_progress);
819 *server_progress = NULL;
820 goto done;
823 break;
824 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
825 if (datalen < sizeof(*packfile_size)) {
826 err = got_error(GOT_ERR_PRIVSEP_MSG);
827 break;
829 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
830 break;
831 case GOT_IMSG_FETCH_DONE:
832 *id = malloc(sizeof(**id));
833 if (*id == NULL) {
834 err = got_error_from_errno("malloc");
835 break;
837 if (datalen != SHA1_DIGEST_LENGTH) {
838 err = got_error(GOT_ERR_PRIVSEP_MSG);
839 break;
841 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
842 *done = 1;
843 break;
844 default:
845 err = got_error(GOT_ERR_PRIVSEP_MSG);
846 break;
848 done:
849 if (err) {
850 free(*id);
851 *id = NULL;
852 free(*refname);
853 *refname = NULL;
855 imsg_free(&imsg);
856 return err;
859 const struct got_error *
860 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
861 int fd)
863 const struct got_error *err = NULL;
865 /* Keep in sync with struct got_imsg_index_pack_request */
866 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
867 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
868 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
869 close(fd);
870 return err;
872 return flush_imsg(ibuf);
875 const struct got_error *
876 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
878 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
881 const struct got_error *
882 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
883 int nobj_indexed, int nobj_loose, int nobj_resolved)
885 struct got_imsg_index_pack_progress iprogress;
887 iprogress.nobj_total = nobj_total;
888 iprogress.nobj_indexed = nobj_indexed;
889 iprogress.nobj_loose = nobj_loose;
890 iprogress.nobj_resolved = nobj_resolved;
892 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
893 &iprogress, sizeof(iprogress)) == -1)
894 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
896 return flush_imsg(ibuf);
899 const struct got_error *
900 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
902 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
903 return got_error_from_errno("imsg_compose FETCH");
904 return flush_imsg(ibuf);
907 const struct got_error *
908 got_privsep_recv_index_progress(int *done, int *nobj_total,
909 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
910 struct imsgbuf *ibuf)
912 const struct got_error *err = NULL;
913 struct imsg imsg;
914 struct got_imsg_index_pack_progress *iprogress;
915 size_t datalen;
917 *done = 0;
918 *nobj_total = 0;
919 *nobj_indexed = 0;
920 *nobj_resolved = 0;
922 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
923 if (err)
924 return err;
926 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
927 switch (imsg.hdr.type) {
928 case GOT_IMSG_ERROR:
929 if (datalen < sizeof(struct got_imsg_error)) {
930 err = got_error(GOT_ERR_PRIVSEP_LEN);
931 break;
933 err = recv_imsg_error(&imsg, datalen);
934 break;
935 case GOT_IMSG_IDXPACK_PROGRESS:
936 if (datalen < sizeof(*iprogress)) {
937 err = got_error(GOT_ERR_PRIVSEP_LEN);
938 break;
940 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
941 *nobj_total = iprogress->nobj_total;
942 *nobj_indexed = iprogress->nobj_indexed;
943 *nobj_loose = iprogress->nobj_loose;
944 *nobj_resolved = iprogress->nobj_resolved;
945 break;
946 case GOT_IMSG_IDXPACK_DONE:
947 if (datalen != 0) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
949 break;
951 *done = 1;
952 break;
953 default:
954 err = got_error(GOT_ERR_PRIVSEP_MSG);
955 break;
958 imsg_free(&imsg);
959 return err;
962 const struct got_error *
963 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
964 struct imsgbuf *ibuf)
966 const struct got_error *err = NULL;
967 struct got_imsg_object *iobj;
968 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
970 if (datalen != sizeof(*iobj))
971 return got_error(GOT_ERR_PRIVSEP_LEN);
972 iobj = imsg->data;
974 *obj = calloc(1, sizeof(**obj));
975 if (*obj == NULL)
976 return got_error_from_errno("calloc");
978 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
979 (*obj)->type = iobj->type;
980 (*obj)->flags = iobj->flags;
981 (*obj)->hdrlen = iobj->hdrlen;
982 (*obj)->size = iobj->size;
983 /* path_packfile is handled by caller */
984 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
985 (*obj)->pack_offset = iobj->pack_offset;
986 (*obj)->pack_idx = iobj->pack_idx;
989 return err;
992 const struct got_error *
993 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
995 const struct got_error *err = NULL;
996 struct imsg imsg;
997 const size_t min_datalen =
998 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1000 *obj = NULL;
1002 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1003 if (err)
1004 return err;
1006 switch (imsg.hdr.type) {
1007 case GOT_IMSG_OBJECT:
1008 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1009 break;
1010 default:
1011 err = got_error(GOT_ERR_PRIVSEP_MSG);
1012 break;
1015 imsg_free(&imsg);
1017 return err;
1020 static const struct got_error *
1021 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1022 size_t logmsg_len)
1024 const struct got_error *err = NULL;
1025 size_t offset, remain;
1027 offset = 0;
1028 remain = logmsg_len;
1029 while (remain > 0) {
1030 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1032 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1033 commit->logmsg + offset, n) == -1) {
1034 err = got_error_from_errno("imsg_compose "
1035 "COMMIT_LOGMSG");
1036 break;
1039 err = flush_imsg(ibuf);
1040 if (err)
1041 break;
1043 offset += n;
1044 remain -= n;
1047 return err;
1050 const struct got_error *
1051 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1053 const struct got_error *err = NULL;
1054 struct got_imsg_commit_object *icommit;
1055 uint8_t *buf;
1056 size_t len, total;
1057 struct got_object_qid *qid;
1058 size_t author_len = strlen(commit->author);
1059 size_t committer_len = strlen(commit->committer);
1060 size_t logmsg_len = strlen(commit->logmsg);
1062 total = sizeof(*icommit) + author_len + committer_len +
1063 commit->nparents * SHA1_DIGEST_LENGTH;
1065 buf = malloc(total);
1066 if (buf == NULL)
1067 return got_error_from_errno("malloc");
1069 icommit = (struct got_imsg_commit_object *)buf;
1070 memcpy(icommit->tree_id, commit->tree_id->sha1,
1071 sizeof(icommit->tree_id));
1072 icommit->author_len = author_len;
1073 icommit->author_time = commit->author_time;
1074 icommit->author_gmtoff = commit->author_gmtoff;
1075 icommit->committer_len = committer_len;
1076 icommit->committer_time = commit->committer_time;
1077 icommit->committer_gmtoff = commit->committer_gmtoff;
1078 icommit->logmsg_len = logmsg_len;
1079 icommit->nparents = commit->nparents;
1081 len = sizeof(*icommit);
1082 memcpy(buf + len, commit->author, author_len);
1083 len += author_len;
1084 memcpy(buf + len, commit->committer, committer_len);
1085 len += committer_len;
1086 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1087 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1088 len += SHA1_DIGEST_LENGTH;
1091 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1092 err = got_error_from_errno("imsg_compose COMMIT");
1093 goto done;
1096 if (logmsg_len == 0 ||
1097 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1098 err = flush_imsg(ibuf);
1099 if (err)
1100 goto done;
1102 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1103 done:
1104 free(buf);
1105 return err;
1108 static const struct got_error *
1109 get_commit_from_imsg(struct got_commit_object **commit,
1110 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1112 const struct got_error *err = NULL;
1113 struct got_imsg_commit_object *icommit;
1114 size_t len = 0;
1115 int i;
1117 if (datalen < sizeof(*icommit))
1118 return got_error(GOT_ERR_PRIVSEP_LEN);
1120 icommit = imsg->data;
1121 if (datalen != sizeof(*icommit) + icommit->author_len +
1122 icommit->committer_len +
1123 icommit->nparents * SHA1_DIGEST_LENGTH)
1124 return got_error(GOT_ERR_PRIVSEP_LEN);
1126 if (icommit->nparents < 0)
1127 return got_error(GOT_ERR_PRIVSEP_LEN);
1129 len += sizeof(*icommit);
1131 *commit = got_object_commit_alloc_partial();
1132 if (*commit == NULL)
1133 return got_error_from_errno(
1134 "got_object_commit_alloc_partial");
1136 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1137 SHA1_DIGEST_LENGTH);
1138 (*commit)->author_time = icommit->author_time;
1139 (*commit)->author_gmtoff = icommit->author_gmtoff;
1140 (*commit)->committer_time = icommit->committer_time;
1141 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1143 if (icommit->author_len == 0) {
1144 (*commit)->author = strdup("");
1145 if ((*commit)->author == NULL) {
1146 err = got_error_from_errno("strdup");
1147 goto done;
1149 } else {
1150 (*commit)->author = malloc(icommit->author_len + 1);
1151 if ((*commit)->author == NULL) {
1152 err = got_error_from_errno("malloc");
1153 goto done;
1155 memcpy((*commit)->author, imsg->data + len,
1156 icommit->author_len);
1157 (*commit)->author[icommit->author_len] = '\0';
1159 len += icommit->author_len;
1161 if (icommit->committer_len == 0) {
1162 (*commit)->committer = strdup("");
1163 if ((*commit)->committer == NULL) {
1164 err = got_error_from_errno("strdup");
1165 goto done;
1167 } else {
1168 (*commit)->committer =
1169 malloc(icommit->committer_len + 1);
1170 if ((*commit)->committer == NULL) {
1171 err = got_error_from_errno("malloc");
1172 goto done;
1174 memcpy((*commit)->committer, imsg->data + len,
1175 icommit->committer_len);
1176 (*commit)->committer[icommit->committer_len] = '\0';
1178 len += icommit->committer_len;
1180 if (icommit->logmsg_len == 0) {
1181 (*commit)->logmsg = strdup("");
1182 if ((*commit)->logmsg == NULL) {
1183 err = got_error_from_errno("strdup");
1184 goto done;
1186 } else {
1187 size_t offset = 0, remain = icommit->logmsg_len;
1189 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1190 if ((*commit)->logmsg == NULL) {
1191 err = got_error_from_errno("malloc");
1192 goto done;
1194 while (remain > 0) {
1195 struct imsg imsg_log;
1196 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1197 remain);
1199 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1200 if (err)
1201 goto done;
1203 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1204 err = got_error(GOT_ERR_PRIVSEP_MSG);
1205 goto done;
1208 memcpy((*commit)->logmsg + offset,
1209 imsg_log.data, n);
1210 imsg_free(&imsg_log);
1211 offset += n;
1212 remain -= n;
1214 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1217 for (i = 0; i < icommit->nparents; i++) {
1218 struct got_object_qid *qid;
1220 err = got_object_qid_alloc_partial(&qid);
1221 if (err)
1222 break;
1223 memcpy(qid->id, imsg->data + len +
1224 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1225 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1226 (*commit)->nparents++;
1228 done:
1229 if (err) {
1230 got_object_commit_close(*commit);
1231 *commit = NULL;
1233 return err;
1236 const struct got_error *
1237 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1239 const struct got_error *err = NULL;
1240 struct imsg imsg;
1241 size_t datalen;
1242 const size_t min_datalen =
1243 MIN(sizeof(struct got_imsg_error),
1244 sizeof(struct got_imsg_commit_object));
1246 *commit = NULL;
1248 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1249 if (err)
1250 return err;
1252 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1254 switch (imsg.hdr.type) {
1255 case GOT_IMSG_COMMIT:
1256 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1257 break;
1258 default:
1259 err = got_error(GOT_ERR_PRIVSEP_MSG);
1260 break;
1263 imsg_free(&imsg);
1265 return err;
1268 const struct got_error *
1269 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1270 int nentries)
1272 const struct got_error *err = NULL;
1273 struct got_imsg_tree_object itree;
1274 struct got_pathlist_entry *pe;
1275 size_t totlen;
1276 int nimsg; /* number of imsg queued in ibuf */
1278 itree.nentries = nentries;
1279 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1280 == -1)
1281 return got_error_from_errno("imsg_compose TREE");
1283 totlen = sizeof(itree);
1284 nimsg = 1;
1285 TAILQ_FOREACH(pe, entries, entry) {
1286 const char *name = pe->path;
1287 struct got_parsed_tree_entry *pte = pe->data;
1288 struct ibuf *wbuf;
1289 size_t namelen = strlen(name);
1290 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1292 if (len > MAX_IMSGSIZE)
1293 return got_error(GOT_ERR_NO_SPACE);
1295 nimsg++;
1296 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1297 err = flush_imsg(ibuf);
1298 if (err)
1299 return err;
1300 nimsg = 0;
1303 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1304 if (wbuf == NULL)
1305 return got_error_from_errno("imsg_create TREE_ENTRY");
1307 /* Keep in sync with struct got_imsg_tree_object definition! */
1308 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1309 err = got_error_from_errno("imsg_add TREE_ENTRY");
1310 ibuf_free(wbuf);
1311 return err;
1313 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1314 err = got_error_from_errno("imsg_add TREE_ENTRY");
1315 ibuf_free(wbuf);
1316 return err;
1319 if (imsg_add(wbuf, name, namelen) == -1) {
1320 err = got_error_from_errno("imsg_add TREE_ENTRY");
1321 ibuf_free(wbuf);
1322 return err;
1325 wbuf->fd = -1;
1326 imsg_close(ibuf, wbuf);
1328 totlen += len;
1331 return flush_imsg(ibuf);
1334 const struct got_error *
1335 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1337 const struct got_error *err = NULL;
1338 const size_t min_datalen =
1339 MIN(sizeof(struct got_imsg_error),
1340 sizeof(struct got_imsg_tree_object));
1341 struct got_imsg_tree_object *itree;
1342 int nentries = 0;
1344 *tree = NULL;
1345 get_more:
1346 err = read_imsg(ibuf);
1347 if (err)
1348 goto done;
1350 for (;;) {
1351 struct imsg imsg;
1352 size_t n;
1353 size_t datalen;
1354 struct got_imsg_tree_entry *ite;
1355 struct got_tree_entry *te = NULL;
1357 n = imsg_get(ibuf, &imsg);
1358 if (n == 0) {
1359 if (*tree && (*tree)->nentries != nentries)
1360 goto get_more;
1361 break;
1364 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1365 return got_error(GOT_ERR_PRIVSEP_LEN);
1367 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1369 switch (imsg.hdr.type) {
1370 case GOT_IMSG_ERROR:
1371 err = recv_imsg_error(&imsg, datalen);
1372 break;
1373 case GOT_IMSG_TREE:
1374 /* This message should only appear once. */
1375 if (*tree != NULL) {
1376 err = got_error(GOT_ERR_PRIVSEP_MSG);
1377 break;
1379 if (datalen != sizeof(*itree)) {
1380 err = got_error(GOT_ERR_PRIVSEP_LEN);
1381 break;
1383 itree = imsg.data;
1384 *tree = malloc(sizeof(**tree));
1385 if (*tree == NULL) {
1386 err = got_error_from_errno("malloc");
1387 break;
1389 (*tree)->entries = calloc(itree->nentries,
1390 sizeof(struct got_tree_entry));
1391 if ((*tree)->entries == NULL) {
1392 err = got_error_from_errno("malloc");
1393 break;
1395 (*tree)->nentries = itree->nentries;
1396 (*tree)->refcnt = 0;
1397 break;
1398 case GOT_IMSG_TREE_ENTRY:
1399 /* This message should be preceeded by GOT_IMSG_TREE. */
1400 if (*tree == NULL) {
1401 err = got_error(GOT_ERR_PRIVSEP_MSG);
1402 break;
1404 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1405 err = got_error(GOT_ERR_PRIVSEP_LEN);
1406 break;
1409 /* Remaining data contains the entry's name. */
1410 datalen -= sizeof(*ite);
1411 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1412 err = got_error(GOT_ERR_PRIVSEP_LEN);
1413 break;
1415 ite = imsg.data;
1417 if (datalen + 1 > sizeof(te->name)) {
1418 err = got_error(GOT_ERR_NO_SPACE);
1419 break;
1421 te = &(*tree)->entries[nentries];
1422 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1423 te->name[datalen] = '\0';
1425 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1426 te->mode = ite->mode;
1427 te->idx = nentries;
1428 nentries++;
1429 break;
1430 default:
1431 err = got_error(GOT_ERR_PRIVSEP_MSG);
1432 break;
1435 imsg_free(&imsg);
1437 done:
1438 if (*tree && (*tree)->nentries != nentries) {
1439 if (err == NULL)
1440 err = got_error(GOT_ERR_PRIVSEP_LEN);
1441 got_object_tree_close(*tree);
1442 *tree = NULL;
1445 return err;
1448 const struct got_error *
1449 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1450 const uint8_t *data)
1452 struct got_imsg_blob iblob;
1454 iblob.size = size;
1455 iblob.hdrlen = hdrlen;
1457 if (data) {
1458 uint8_t *buf;
1460 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1461 return got_error(GOT_ERR_NO_SPACE);
1463 buf = malloc(sizeof(iblob) + size);
1464 if (buf == NULL)
1465 return got_error_from_errno("malloc");
1467 memcpy(buf, &iblob, sizeof(iblob));
1468 memcpy(buf + sizeof(iblob), data, size);
1469 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1470 sizeof(iblob) + size) == -1) {
1471 free(buf);
1472 return got_error_from_errno("imsg_compose BLOB");
1474 free(buf);
1475 } else {
1476 /* Data has already been written to file descriptor. */
1477 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1478 sizeof(iblob)) == -1)
1479 return got_error_from_errno("imsg_compose BLOB");
1483 return flush_imsg(ibuf);
1486 const struct got_error *
1487 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1488 struct imsgbuf *ibuf)
1490 const struct got_error *err = NULL;
1491 struct imsg imsg;
1492 struct got_imsg_blob *iblob;
1493 size_t datalen;
1495 *outbuf = NULL;
1497 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1498 if (err)
1499 return err;
1501 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1503 switch (imsg.hdr.type) {
1504 case GOT_IMSG_BLOB:
1505 if (datalen < sizeof(*iblob)) {
1506 err = got_error(GOT_ERR_PRIVSEP_LEN);
1507 break;
1509 iblob = imsg.data;
1510 *size = iblob->size;
1511 *hdrlen = iblob->hdrlen;
1513 if (datalen == sizeof(*iblob)) {
1514 /* Data has been written to file descriptor. */
1515 break;
1518 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1519 err = got_error(GOT_ERR_PRIVSEP_LEN);
1520 break;
1523 *outbuf = malloc(*size);
1524 if (*outbuf == NULL) {
1525 err = got_error_from_errno("malloc");
1526 break;
1528 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1529 break;
1530 default:
1531 err = got_error(GOT_ERR_PRIVSEP_MSG);
1532 break;
1535 imsg_free(&imsg);
1537 return err;
1540 static const struct got_error *
1541 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1543 const struct got_error *err = NULL;
1544 size_t offset, remain;
1546 offset = 0;
1547 remain = tagmsg_len;
1548 while (remain > 0) {
1549 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1551 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1552 tag->tagmsg + offset, n) == -1) {
1553 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1554 break;
1557 err = flush_imsg(ibuf);
1558 if (err)
1559 break;
1561 offset += n;
1562 remain -= n;
1565 return err;
1568 const struct got_error *
1569 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1571 const struct got_error *err = NULL;
1572 struct got_imsg_tag_object *itag;
1573 uint8_t *buf;
1574 size_t len, total;
1575 size_t tag_len = strlen(tag->tag);
1576 size_t tagger_len = strlen(tag->tagger);
1577 size_t tagmsg_len = strlen(tag->tagmsg);
1579 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1581 buf = malloc(total);
1582 if (buf == NULL)
1583 return got_error_from_errno("malloc");
1585 itag = (struct got_imsg_tag_object *)buf;
1586 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1587 itag->obj_type = tag->obj_type;
1588 itag->tag_len = tag_len;
1589 itag->tagger_len = tagger_len;
1590 itag->tagger_time = tag->tagger_time;
1591 itag->tagger_gmtoff = tag->tagger_gmtoff;
1592 itag->tagmsg_len = tagmsg_len;
1594 len = sizeof(*itag);
1595 memcpy(buf + len, tag->tag, tag_len);
1596 len += tag_len;
1597 memcpy(buf + len, tag->tagger, tagger_len);
1598 len += tagger_len;
1600 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1601 err = got_error_from_errno("imsg_compose TAG");
1602 goto done;
1605 if (tagmsg_len == 0 ||
1606 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1607 err = flush_imsg(ibuf);
1608 if (err)
1609 goto done;
1611 err = send_tagmsg(ibuf, tag, tagmsg_len);
1612 done:
1613 free(buf);
1614 return err;
1617 const struct got_error *
1618 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1620 const struct got_error *err = NULL;
1621 struct imsg imsg;
1622 struct got_imsg_tag_object *itag;
1623 size_t len, datalen;
1624 const size_t min_datalen =
1625 MIN(sizeof(struct got_imsg_error),
1626 sizeof(struct got_imsg_tag_object));
1628 *tag = NULL;
1630 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1631 if (err)
1632 return err;
1634 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1635 len = 0;
1637 switch (imsg.hdr.type) {
1638 case GOT_IMSG_TAG:
1639 if (datalen < sizeof(*itag)) {
1640 err = got_error(GOT_ERR_PRIVSEP_LEN);
1641 break;
1643 itag = imsg.data;
1644 if (datalen != sizeof(*itag) + itag->tag_len +
1645 itag->tagger_len) {
1646 err = got_error(GOT_ERR_PRIVSEP_LEN);
1647 break;
1649 len += sizeof(*itag);
1651 *tag = calloc(1, sizeof(**tag));
1652 if (*tag == NULL) {
1653 err = got_error_from_errno("calloc");
1654 break;
1657 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1659 if (itag->tag_len == 0) {
1660 (*tag)->tag = strdup("");
1661 if ((*tag)->tag == NULL) {
1662 err = got_error_from_errno("strdup");
1663 break;
1665 } else {
1666 (*tag)->tag = malloc(itag->tag_len + 1);
1667 if ((*tag)->tag == NULL) {
1668 err = got_error_from_errno("malloc");
1669 break;
1671 memcpy((*tag)->tag, imsg.data + len,
1672 itag->tag_len);
1673 (*tag)->tag[itag->tag_len] = '\0';
1675 len += itag->tag_len;
1677 (*tag)->obj_type = itag->obj_type;
1678 (*tag)->tagger_time = itag->tagger_time;
1679 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1681 if (itag->tagger_len == 0) {
1682 (*tag)->tagger = strdup("");
1683 if ((*tag)->tagger == NULL) {
1684 err = got_error_from_errno("strdup");
1685 break;
1687 } else {
1688 (*tag)->tagger = malloc(itag->tagger_len + 1);
1689 if ((*tag)->tagger == NULL) {
1690 err = got_error_from_errno("malloc");
1691 break;
1693 memcpy((*tag)->tagger, imsg.data + len,
1694 itag->tagger_len);
1695 (*tag)->tagger[itag->tagger_len] = '\0';
1697 len += itag->tagger_len;
1699 if (itag->tagmsg_len == 0) {
1700 (*tag)->tagmsg = strdup("");
1701 if ((*tag)->tagmsg == NULL) {
1702 err = got_error_from_errno("strdup");
1703 break;
1705 } else {
1706 size_t offset = 0, remain = itag->tagmsg_len;
1708 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1709 if ((*tag)->tagmsg == NULL) {
1710 err = got_error_from_errno("malloc");
1711 break;
1713 while (remain > 0) {
1714 struct imsg imsg_log;
1715 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1716 remain);
1718 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1719 if (err)
1720 return err;
1722 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1723 return got_error(GOT_ERR_PRIVSEP_MSG);
1725 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1726 n);
1727 imsg_free(&imsg_log);
1728 offset += n;
1729 remain -= n;
1731 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1734 break;
1735 default:
1736 err = got_error(GOT_ERR_PRIVSEP_MSG);
1737 break;
1740 imsg_free(&imsg);
1742 return err;
1745 const struct got_error *
1746 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1747 struct got_packidx *packidx)
1749 const struct got_error *err = NULL;
1750 struct got_imsg_packidx ipackidx;
1751 struct got_imsg_pack ipack;
1752 int fd;
1754 ipackidx.len = packidx->len;
1755 fd = dup(packidx->fd);
1756 if (fd == -1)
1757 return got_error_from_errno("dup");
1759 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1760 sizeof(ipackidx)) == -1) {
1761 err = got_error_from_errno("imsg_compose PACKIDX");
1762 close(fd);
1763 return err;
1766 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1767 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1768 return got_error(GOT_ERR_NO_SPACE);
1769 ipack.filesize = pack->filesize;
1771 fd = dup(pack->fd);
1772 if (fd == -1)
1773 return got_error_from_errno("dup");
1775 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1776 == -1) {
1777 err = got_error_from_errno("imsg_compose PACK");
1778 close(fd);
1779 return err;
1782 return flush_imsg(ibuf);
1785 const struct got_error *
1786 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1787 struct got_object_id *id)
1789 struct got_imsg_packed_object iobj;
1791 iobj.idx = idx;
1792 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1794 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1795 &iobj, sizeof(iobj)) == -1)
1796 return got_error_from_errno("imsg_compose "
1797 "PACKED_OBJECT_REQUEST");
1799 return flush_imsg(ibuf);
1802 const struct got_error *
1803 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1805 const struct got_error *err = NULL;
1807 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1808 NULL, 0) == -1) {
1809 err = got_error_from_errno("imsg_compose "
1810 "GITCONFIG_PARSE_REQUEST");
1811 close(fd);
1812 return err;
1815 return flush_imsg(ibuf);
1818 const struct got_error *
1819 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1821 if (imsg_compose(ibuf,
1822 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1823 NULL, 0) == -1)
1824 return got_error_from_errno("imsg_compose "
1825 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1827 return flush_imsg(ibuf);
1830 const struct got_error *
1831 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1833 if (imsg_compose(ibuf,
1834 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1835 return got_error_from_errno("imsg_compose "
1836 "GITCONFIG_AUTHOR_NAME_REQUEST");
1838 return flush_imsg(ibuf);
1841 const struct got_error *
1842 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1844 if (imsg_compose(ibuf,
1845 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1846 return got_error_from_errno("imsg_compose "
1847 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1849 return flush_imsg(ibuf);
1852 const struct got_error *
1853 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1855 if (imsg_compose(ibuf,
1856 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1857 return got_error_from_errno("imsg_compose "
1858 "GITCONFIG_REMOTE_REQUEST");
1860 return flush_imsg(ibuf);
1863 const struct got_error *
1864 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1866 if (imsg_compose(ibuf,
1867 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1868 return got_error_from_errno("imsg_compose "
1869 "GITCONFIG_OWNER_REQUEST");
1871 return flush_imsg(ibuf);
1874 const struct got_error *
1875 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1877 size_t len = value ? strlen(value) + 1 : 0;
1879 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1880 value, len) == -1)
1881 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1883 return flush_imsg(ibuf);
1886 const struct got_error *
1887 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1889 const struct got_error *err = NULL;
1890 struct imsg imsg;
1891 size_t datalen;
1892 const size_t min_datalen = 0;
1894 *str = NULL;
1896 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1897 if (err)
1898 return err;
1899 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1901 switch (imsg.hdr.type) {
1902 case GOT_IMSG_GITCONFIG_STR_VAL:
1903 if (datalen == 0)
1904 break;
1905 *str = malloc(datalen);
1906 if (*str == NULL) {
1907 err = got_error_from_errno("malloc");
1908 break;
1910 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1911 err = got_error(GOT_ERR_NO_SPACE);
1912 break;
1913 default:
1914 err = got_error(GOT_ERR_PRIVSEP_MSG);
1915 break;
1918 imsg_free(&imsg);
1919 return err;
1922 const struct got_error *
1923 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1925 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1926 &value, sizeof(value)) == -1)
1927 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1929 return flush_imsg(ibuf);
1932 const struct got_error *
1933 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1935 const struct got_error *err = NULL;
1936 struct imsg imsg;
1937 size_t datalen;
1938 const size_t min_datalen =
1939 MIN(sizeof(struct got_imsg_error), sizeof(int));
1941 *val = 0;
1943 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1944 if (err)
1945 return err;
1946 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1948 switch (imsg.hdr.type) {
1949 case GOT_IMSG_GITCONFIG_INT_VAL:
1950 if (datalen != sizeof(*val)) {
1951 err = got_error(GOT_ERR_PRIVSEP_LEN);
1952 break;
1954 memcpy(val, imsg.data, sizeof(*val));
1955 break;
1956 default:
1957 err = got_error(GOT_ERR_PRIVSEP_MSG);
1958 break;
1961 imsg_free(&imsg);
1962 return err;
1965 const struct got_error *
1966 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1967 struct got_remote_repo *remotes, int nremotes)
1969 const struct got_error *err = NULL;
1970 struct got_imsg_remotes iremotes;
1971 int i;
1973 iremotes.nremotes = nremotes;
1974 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1975 &iremotes, sizeof(iremotes)) == -1)
1976 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1978 err = flush_imsg(ibuf);
1979 imsg_clear(ibuf);
1980 if (err)
1981 return err;
1983 for (i = 0; i < nremotes; i++) {
1984 struct got_imsg_remote iremote;
1985 size_t len = sizeof(iremote);
1986 struct ibuf *wbuf;
1988 iremote.mirror_references = remotes[i].mirror_references;
1989 iremote.name_len = strlen(remotes[i].name);
1990 len += iremote.name_len;
1991 iremote.url_len = strlen(remotes[i].url);
1992 len += iremote.url_len;
1994 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1995 if (wbuf == NULL)
1996 return got_error_from_errno(
1997 "imsg_create GITCONFIG_REMOTE");
1999 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
2000 err = got_error_from_errno(
2001 "imsg_add GITCONFIG_REMOTE");
2002 ibuf_free(wbuf);
2003 return err;
2006 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
2007 err = got_error_from_errno(
2008 "imsg_add GITCONFIG_REMOTE");
2009 ibuf_free(wbuf);
2010 return err;
2012 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
2013 err = got_error_from_errno(
2014 "imsg_add GITCONFIG_REMOTE");
2015 ibuf_free(wbuf);
2016 return err;
2019 wbuf->fd = -1;
2020 imsg_close(ibuf, wbuf);
2021 err = flush_imsg(ibuf);
2022 if (err)
2023 return err;
2026 return NULL;
2029 const struct got_error *
2030 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2031 int *nremotes, struct imsgbuf *ibuf)
2033 const struct got_error *err = NULL;
2034 struct imsg imsg;
2035 size_t datalen;
2036 struct got_imsg_remotes iremotes;
2037 struct got_imsg_remote iremote;
2039 *remotes = NULL;
2040 *nremotes = 0;
2041 iremotes.nremotes = 0;
2043 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2044 if (err)
2045 return err;
2046 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2048 switch (imsg.hdr.type) {
2049 case GOT_IMSG_GITCONFIG_REMOTES:
2050 if (datalen != sizeof(iremotes)) {
2051 err = got_error(GOT_ERR_PRIVSEP_LEN);
2052 break;
2054 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2055 if (iremotes.nremotes == 0) {
2056 imsg_free(&imsg);
2057 return NULL;
2059 break;
2060 default:
2061 imsg_free(&imsg);
2062 return got_error(GOT_ERR_PRIVSEP_MSG);
2065 imsg_free(&imsg);
2067 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2068 if (*remotes == NULL)
2069 return got_error_from_errno("recallocarray");
2071 while (*nremotes < iremotes.nremotes) {
2072 struct got_remote_repo *remote;
2074 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2075 if (err)
2076 break;
2077 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2079 switch (imsg.hdr.type) {
2080 case GOT_IMSG_GITCONFIG_REMOTE:
2081 remote = &(*remotes)[*nremotes];
2082 if (datalen < sizeof(iremote)) {
2083 err = got_error(GOT_ERR_PRIVSEP_LEN);
2084 break;
2086 memcpy(&iremote, imsg.data, sizeof(iremote));
2087 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2088 (sizeof(iremote) + iremote.name_len +
2089 iremote.url_len) > datalen) {
2090 err = got_error(GOT_ERR_PRIVSEP_LEN);
2091 break;
2093 remote->name = strndup(imsg.data + sizeof(iremote),
2094 iremote.name_len);
2095 if (remote->name == NULL) {
2096 err = got_error_from_errno("strndup");
2097 break;
2099 remote->url = strndup(imsg.data + sizeof(iremote) +
2100 iremote.name_len, iremote.url_len);
2101 if (remote->url == NULL) {
2102 err = got_error_from_errno("strndup");
2103 free(remote->name);
2104 break;
2106 remote->mirror_references = iremote.mirror_references;
2107 (*nremotes)++;
2108 break;
2109 default:
2110 err = got_error(GOT_ERR_PRIVSEP_MSG);
2111 break;
2114 imsg_free(&imsg);
2115 if (err)
2116 break;
2119 if (err) {
2120 int i;
2121 for (i = 0; i < *nremotes; i++) {
2122 free((*remotes)[i].name);
2123 free((*remotes)[i].url);
2125 free(*remotes);
2126 *remotes = NULL;
2127 *nremotes = 0;
2129 return err;
2132 const struct got_error *
2133 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2134 struct got_object_id *id, int idx, const char *path)
2136 const struct got_error *err = NULL;
2137 struct ibuf *wbuf;
2138 size_t path_len = strlen(path) + 1;
2140 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2141 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2142 if (wbuf == NULL)
2143 return got_error_from_errno(
2144 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2145 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2146 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2147 ibuf_free(wbuf);
2148 return err;
2150 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2151 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2152 ibuf_free(wbuf);
2153 return err;
2155 if (imsg_add(wbuf, path, path_len) == -1) {
2156 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2157 ibuf_free(wbuf);
2158 return err;
2161 wbuf->fd = -1;
2162 imsg_close(ibuf, wbuf);
2164 return flush_imsg(ibuf);
2167 const struct got_error *
2168 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2169 size_t ncommits, struct imsgbuf *ibuf)
2171 const struct got_error *err;
2172 struct ibuf *wbuf;
2173 int i;
2175 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2176 sizeof(struct got_imsg_traversed_commits) +
2177 ncommits * SHA1_DIGEST_LENGTH);
2178 if (wbuf == NULL)
2179 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2181 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2182 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2183 ibuf_free(wbuf);
2184 return err;
2186 for (i = 0; i < ncommits; i++) {
2187 struct got_object_id *id = &commit_ids[i];
2188 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2189 err = got_error_from_errno(
2190 "imsg_add TRAVERSED_COMMITS");
2191 ibuf_free(wbuf);
2192 return err;
2196 wbuf->fd = -1;
2197 imsg_close(ibuf, wbuf);
2199 return flush_imsg(ibuf);
2202 const struct got_error *
2203 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2204 struct got_object_id **changed_commit_id,
2205 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2207 const struct got_error *err = NULL;
2208 struct imsg imsg;
2209 struct got_imsg_traversed_commits *icommits;
2210 size_t datalen;
2211 int i, done = 0;
2213 *changed_commit = NULL;
2214 *changed_commit_id = NULL;
2216 while (!done) {
2217 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2218 if (err)
2219 return err;
2221 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2222 switch (imsg.hdr.type) {
2223 case GOT_IMSG_TRAVERSED_COMMITS:
2224 icommits = imsg.data;
2225 if (datalen != sizeof(*icommits) +
2226 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2227 err = got_error(GOT_ERR_PRIVSEP_LEN);
2228 break;
2230 for (i = 0; i < icommits->ncommits; i++) {
2231 struct got_object_qid *qid;
2232 uint8_t *sha1 = (uint8_t *)imsg.data +
2233 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2234 err = got_object_qid_alloc_partial(&qid);
2235 if (err)
2236 break;
2237 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2238 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2240 /* The last commit may contain a change. */
2241 if (i == icommits->ncommits - 1) {
2242 *changed_commit_id =
2243 got_object_id_dup(qid->id);
2244 if (*changed_commit_id == NULL) {
2245 err = got_error_from_errno(
2246 "got_object_id_dup");
2247 break;
2251 break;
2252 case GOT_IMSG_COMMIT:
2253 if (*changed_commit_id == NULL) {
2254 err = got_error(GOT_ERR_PRIVSEP_MSG);
2255 break;
2257 err = get_commit_from_imsg(changed_commit, &imsg,
2258 datalen, ibuf);
2259 break;
2260 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2261 done = 1;
2262 break;
2263 default:
2264 err = got_error(GOT_ERR_PRIVSEP_MSG);
2265 break;
2268 imsg_free(&imsg);
2269 if (err)
2270 break;
2273 if (err)
2274 got_object_id_queue_free(commit_ids);
2275 return err;
2278 const struct got_error *
2279 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2281 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2282 NULL, 0) == -1)
2283 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2285 return flush_imsg(ibuf);
2288 const struct got_error *
2289 got_privsep_unveil_exec_helpers(void)
2291 const char *helpers[] = {
2292 GOT_PATH_PROG_READ_PACK,
2293 GOT_PATH_PROG_READ_OBJECT,
2294 GOT_PATH_PROG_READ_COMMIT,
2295 GOT_PATH_PROG_READ_TREE,
2296 GOT_PATH_PROG_READ_BLOB,
2297 GOT_PATH_PROG_READ_TAG,
2298 GOT_PATH_PROG_READ_GITCONFIG,
2299 GOT_PATH_PROG_FETCH_PACK,
2300 GOT_PATH_PROG_INDEX_PACK,
2302 int i;
2304 for (i = 0; i < nitems(helpers); i++) {
2305 if (unveil(helpers[i], "x") == 0)
2306 continue;
2307 return got_error_from_errno2("unveil", helpers[i]);
2310 return NULL;
2313 void
2314 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2316 if (close(imsg_fds[0]) != 0) {
2317 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2318 _exit(1);
2321 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2322 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2323 _exit(1);
2325 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2326 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2327 _exit(1);
2330 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2331 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2332 strerror(errno));
2333 _exit(1);