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 const struct got_error *
372 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
374 const struct got_error *err = NULL;
376 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
377 == -1) {
378 err = got_error_from_errno("imsg_compose TMPFD");
379 close(fd);
380 return err;
383 return flush_imsg(ibuf);
386 const struct got_error *
387 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
389 struct got_imsg_object iobj;
391 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
392 iobj.type = obj->type;
393 iobj.flags = obj->flags;
394 iobj.hdrlen = obj->hdrlen;
395 iobj.size = obj->size;
396 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
397 iobj.pack_offset = obj->pack_offset;
398 iobj.pack_idx = obj->pack_idx;
401 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
402 == -1)
403 return got_error_from_errno("imsg_compose OBJECT");
405 return flush_imsg(ibuf);
408 const struct got_error *
409 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
410 struct got_pathlist_head *have_refs)
412 const struct got_error *err = NULL;
413 struct ibuf *wbuf;
414 size_t len, n_have_refs = 0;
415 struct got_pathlist_entry *pe;
417 len = sizeof(struct got_imsg_fetch_symrefs);
418 TAILQ_FOREACH(pe, have_refs, entry) {
419 struct got_object_id *id = pe->data;
420 len += sizeof(struct got_imsg_fetch_have_ref) +
421 pe->path_len + sizeof(id->sha1);
422 n_have_refs++;
424 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
425 close(fd);
426 return got_error(GOT_ERR_NO_SPACE);
429 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
430 if (wbuf == NULL) {
431 close(fd);
432 return got_error_from_errno("imsg_create FETCH_REQUEST");
435 /* Keep in sync with struct got_imsg_fetch_have_refs definition! */
436 if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
437 err = got_error_from_errno("imsg_add FETCH_REQUEST");
438 ibuf_free(wbuf);
439 close(fd);
440 return err;
443 TAILQ_FOREACH(pe, have_refs, entry) {
444 const char *name = pe->path;
445 size_t name_len = pe->path_len;
446 struct got_object_id *id = pe->data;
448 /* Keep in sync with struct got_imsg_fetch_have_ref! */
449 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
450 err = got_error_from_errno("imsg_add FETCH_REQUEST");
451 ibuf_free(wbuf);
452 close(fd);
453 return err;
455 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
456 err = got_error_from_errno("imsg_add FETCH_REQUEST");
457 ibuf_free(wbuf);
458 close(fd);
459 return err;
461 if (imsg_add(wbuf, name, name_len) == -1) {
462 err = got_error_from_errno("imsg_add FETCH_REQUEST");
463 ibuf_free(wbuf);
464 close(fd);
465 return err;
469 wbuf->fd = fd;
470 imsg_close(ibuf, wbuf);
471 return flush_imsg(ibuf);
474 const struct got_error *
475 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
476 struct got_pathlist_head *symrefs)
478 const struct got_error *err = NULL;
479 struct ibuf *wbuf;
480 size_t len, nsymrefs = 0;
481 struct got_pathlist_entry *pe;
483 len = sizeof(struct got_imsg_fetch_symrefs);
484 TAILQ_FOREACH(pe, symrefs, entry) {
485 const char *target = pe->data;
486 len += sizeof(struct got_imsg_fetch_symref) +
487 pe->path_len + strlen(target);
488 nsymrefs++;
491 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
492 return got_error(GOT_ERR_NO_SPACE);
494 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
495 if (wbuf == NULL)
496 return got_error_from_errno("imsg_create FETCH_SYMREFS");
498 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
499 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
500 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
501 ibuf_free(wbuf);
502 return err;
505 TAILQ_FOREACH(pe, symrefs, entry) {
506 const char *name = pe->path;
507 size_t name_len = pe->path_len;
508 const char *target = pe->data;
509 size_t target_len = strlen(target);
511 /* Keep in sync with struct got_imsg_fetch_symref definition! */
512 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
513 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
514 ibuf_free(wbuf);
515 return err;
517 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
518 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
519 ibuf_free(wbuf);
520 return err;
522 if (imsg_add(wbuf, name, name_len) == -1) {
523 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
524 ibuf_free(wbuf);
525 return err;
527 if (imsg_add(wbuf, target, target_len) == -1) {
528 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
529 ibuf_free(wbuf);
530 return err;
534 wbuf->fd = -1;
535 imsg_close(ibuf, wbuf);
536 return flush_imsg(ibuf);
539 const struct got_error *
540 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
541 struct got_object_id *refid, const char *refname)
543 const struct got_error *err = NULL;
544 struct ibuf *wbuf;
545 size_t len, reflen = strlen(refname);
547 len = sizeof(struct got_imsg_fetch_ref) + reflen;
548 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
549 return got_error(GOT_ERR_NO_SPACE);
551 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
552 if (wbuf == NULL)
553 return got_error_from_errno("imsg_create FETCH_REF");
555 /* Keep in sync with struct got_imsg_fetch_ref definition! */
556 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
557 err = got_error_from_errno("imsg_add FETCH_REF");
558 ibuf_free(wbuf);
559 return err;
561 if (imsg_add(wbuf, refname, reflen) == -1) {
562 err = got_error_from_errno("imsg_add FETCH_REF");
563 ibuf_free(wbuf);
564 return err;
567 wbuf->fd = -1;
568 imsg_close(ibuf, wbuf);
569 return flush_imsg(ibuf);
572 const struct got_error *
573 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
574 size_t msglen)
576 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
577 return got_error(GOT_ERR_NO_SPACE);
579 if (msglen == 0)
580 return NULL;
582 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
583 msg, msglen) == -1)
584 return got_error_from_errno(
585 "imsg_compose FETCH_SERVER_PROGRESS");
587 return flush_imsg(ibuf);
590 const struct got_error *
591 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
593 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
594 &bytes, sizeof(bytes)) == -1)
595 return got_error_from_errno(
596 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
598 return flush_imsg(ibuf);
601 const struct got_error *
602 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
604 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
605 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
606 return got_error_from_errno("imsg_compose FETCH");
607 return flush_imsg(ibuf);
611 const struct got_error *
612 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
613 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
614 off_t *packfile_size, struct imsgbuf *ibuf)
616 const struct got_error *err = NULL;
617 struct imsg imsg;
618 size_t datalen;
619 struct got_imsg_fetch_symrefs *isymrefs = NULL;
620 size_t n, remain;
621 off_t off;
622 int i;
624 *done = 0;
625 *id = NULL;
626 *refname = NULL;
627 *server_progress = NULL;
629 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
630 if (err)
631 return err;
633 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
634 switch (imsg.hdr.type) {
635 case GOT_IMSG_ERROR:
636 if (datalen < sizeof(struct got_imsg_error)) {
637 err = got_error(GOT_ERR_PRIVSEP_LEN);
638 break;
640 err = recv_imsg_error(&imsg, datalen);
641 break;
642 case GOT_IMSG_FETCH_SYMREFS:
643 if (datalen < sizeof(*isymrefs)) {
644 err = got_error(GOT_ERR_PRIVSEP_LEN);
645 break;
647 if (isymrefs != NULL) {
648 err = got_error(GOT_ERR_PRIVSEP_MSG);
649 break;
651 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
652 off = sizeof(*isymrefs);
653 remain = datalen - off;
654 for (n = 0; n < isymrefs->nsymrefs; n++) {
655 struct got_imsg_fetch_symref *s;
656 char *name, *target;
657 if (remain < sizeof(struct got_imsg_fetch_symref)) {
658 err = got_error(GOT_ERR_PRIVSEP_LEN);
659 goto done;
661 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
662 off += sizeof(*s);
663 remain -= sizeof(*s);
664 if (remain < s->name_len) {
665 err = got_error(GOT_ERR_PRIVSEP_LEN);
666 goto done;
668 name = strndup(imsg.data + off, s->name_len);
669 if (name == NULL) {
670 err = got_error_from_errno("strndup");
671 goto done;
673 off += s->name_len;
674 remain -= s->name_len;
675 if (remain < s->target_len) {
676 err = got_error(GOT_ERR_PRIVSEP_LEN);
677 free(name);
678 goto done;
680 target = strndup(imsg.data + off, s->target_len);
681 if (target == NULL) {
682 err = got_error_from_errno("strndup");
683 free(name);
684 goto done;
686 off += s->target_len;
687 remain -= s->target_len;
688 err = got_pathlist_append(symrefs, name, target);
689 if (err) {
690 free(name);
691 free(target);
692 goto done;
695 break;
696 case GOT_IMSG_FETCH_REF:
697 if (datalen <= SHA1_DIGEST_LENGTH) {
698 err = got_error(GOT_ERR_PRIVSEP_MSG);
699 break;
701 *id = malloc(sizeof(**id));
702 if (*id == NULL) {
703 err = got_error_from_errno("malloc");
704 break;
706 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
707 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
708 datalen - SHA1_DIGEST_LENGTH);
709 if (*refname == NULL) {
710 err = got_error_from_errno("strndup");
711 break;
713 break;
714 case GOT_IMSG_FETCH_SERVER_PROGRESS:
715 if (datalen == 0) {
716 err = got_error(GOT_ERR_PRIVSEP_LEN);
717 break;
719 *server_progress = strndup(imsg.data, datalen);
720 if (*server_progress == NULL) {
721 err = got_error_from_errno("strndup");
722 break;
724 for (i = 0; i < datalen; i++) {
725 if (!isprint((unsigned char)(*server_progress)[i]) &&
726 !isspace((unsigned char)(*server_progress)[i])) {
727 err = got_error(GOT_ERR_PRIVSEP_MSG);
728 free(*server_progress);
729 *server_progress = NULL;
730 goto done;
733 break;
734 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
735 if (datalen < sizeof(*packfile_size)) {
736 err = got_error(GOT_ERR_PRIVSEP_MSG);
737 break;
739 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
740 break;
741 case GOT_IMSG_FETCH_DONE:
742 *id = malloc(sizeof(**id));
743 if (*id == NULL) {
744 err = got_error_from_errno("malloc");
745 break;
747 if (datalen != SHA1_DIGEST_LENGTH) {
748 err = got_error(GOT_ERR_PRIVSEP_MSG);
749 break;
751 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
752 *done = 1;
753 break;
754 default:
755 err = got_error(GOT_ERR_PRIVSEP_MSG);
756 break;
758 done:
759 if (err) {
760 free(*id);
761 *id = NULL;
762 free(*refname);
763 *refname = NULL;
765 imsg_free(&imsg);
766 return err;
769 const struct got_error *
770 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
771 int fd)
773 const struct got_error *err = NULL;
775 /* Keep in sync with struct got_imsg_index_pack_request */
776 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
777 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
778 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
779 close(fd);
780 return err;
782 return flush_imsg(ibuf);
785 const struct got_error *
786 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
787 int nobj_indexed, int nobj_loose, int nobj_resolved)
789 struct got_imsg_index_pack_progress iprogress;
791 iprogress.nobj_total = nobj_total;
792 iprogress.nobj_indexed = nobj_indexed;
793 iprogress.nobj_loose = nobj_loose;
794 iprogress.nobj_resolved = nobj_resolved;
796 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
797 &iprogress, sizeof(iprogress)) == -1)
798 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
800 return flush_imsg(ibuf);
803 const struct got_error *
804 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
806 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
807 return got_error_from_errno("imsg_compose FETCH");
808 return flush_imsg(ibuf);
811 const struct got_error *
812 got_privsep_recv_index_progress(int *done, int *nobj_total,
813 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
814 struct imsgbuf *ibuf)
816 const struct got_error *err = NULL;
817 struct imsg imsg;
818 struct got_imsg_index_pack_progress *iprogress;
819 size_t datalen;
821 *done = 0;
822 *nobj_total = 0;
823 *nobj_indexed = 0;
824 *nobj_resolved = 0;
826 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
827 if (err)
828 return err;
830 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
831 switch (imsg.hdr.type) {
832 case GOT_IMSG_ERROR:
833 if (datalen < sizeof(struct got_imsg_error)) {
834 err = got_error(GOT_ERR_PRIVSEP_LEN);
835 break;
837 err = recv_imsg_error(&imsg, datalen);
838 break;
839 case GOT_IMSG_IDXPACK_PROGRESS:
840 if (datalen < sizeof(*iprogress)) {
841 err = got_error(GOT_ERR_PRIVSEP_LEN);
842 break;
844 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
845 *nobj_total = iprogress->nobj_total;
846 *nobj_indexed = iprogress->nobj_indexed;
847 *nobj_loose = iprogress->nobj_loose;
848 *nobj_resolved = iprogress->nobj_resolved;
849 break;
850 case GOT_IMSG_IDXPACK_DONE:
851 if (datalen != 0) {
852 err = got_error(GOT_ERR_PRIVSEP_LEN);
853 break;
855 *done = 1;
856 break;
857 default:
858 err = got_error(GOT_ERR_PRIVSEP_MSG);
859 break;
862 imsg_free(&imsg);
863 return err;
866 const struct got_error *
867 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
868 struct imsgbuf *ibuf)
870 const struct got_error *err = NULL;
871 struct got_imsg_object *iobj;
872 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
874 if (datalen != sizeof(*iobj))
875 return got_error(GOT_ERR_PRIVSEP_LEN);
876 iobj = imsg->data;
878 *obj = calloc(1, sizeof(**obj));
879 if (*obj == NULL)
880 return got_error_from_errno("calloc");
882 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
883 (*obj)->type = iobj->type;
884 (*obj)->flags = iobj->flags;
885 (*obj)->hdrlen = iobj->hdrlen;
886 (*obj)->size = iobj->size;
887 /* path_packfile is handled by caller */
888 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
889 (*obj)->pack_offset = iobj->pack_offset;
890 (*obj)->pack_idx = iobj->pack_idx;
893 return err;
896 const struct got_error *
897 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
899 const struct got_error *err = NULL;
900 struct imsg imsg;
901 const size_t min_datalen =
902 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
904 *obj = NULL;
906 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
907 if (err)
908 return err;
910 switch (imsg.hdr.type) {
911 case GOT_IMSG_OBJECT:
912 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
913 break;
914 default:
915 err = got_error(GOT_ERR_PRIVSEP_MSG);
916 break;
919 imsg_free(&imsg);
921 return err;
924 static const struct got_error *
925 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
926 size_t logmsg_len)
928 const struct got_error *err = NULL;
929 size_t offset, remain;
931 offset = 0;
932 remain = logmsg_len;
933 while (remain > 0) {
934 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
936 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
937 commit->logmsg + offset, n) == -1) {
938 err = got_error_from_errno("imsg_compose "
939 "COMMIT_LOGMSG");
940 break;
943 err = flush_imsg(ibuf);
944 if (err)
945 break;
947 offset += n;
948 remain -= n;
951 return err;
954 const struct got_error *
955 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
957 const struct got_error *err = NULL;
958 struct got_imsg_commit_object *icommit;
959 uint8_t *buf;
960 size_t len, total;
961 struct got_object_qid *qid;
962 size_t author_len = strlen(commit->author);
963 size_t committer_len = strlen(commit->committer);
964 size_t logmsg_len = strlen(commit->logmsg);
966 total = sizeof(*icommit) + author_len + committer_len +
967 commit->nparents * SHA1_DIGEST_LENGTH;
969 buf = malloc(total);
970 if (buf == NULL)
971 return got_error_from_errno("malloc");
973 icommit = (struct got_imsg_commit_object *)buf;
974 memcpy(icommit->tree_id, commit->tree_id->sha1,
975 sizeof(icommit->tree_id));
976 icommit->author_len = author_len;
977 icommit->author_time = commit->author_time;
978 icommit->author_gmtoff = commit->author_gmtoff;
979 icommit->committer_len = committer_len;
980 icommit->committer_time = commit->committer_time;
981 icommit->committer_gmtoff = commit->committer_gmtoff;
982 icommit->logmsg_len = logmsg_len;
983 icommit->nparents = commit->nparents;
985 len = sizeof(*icommit);
986 memcpy(buf + len, commit->author, author_len);
987 len += author_len;
988 memcpy(buf + len, commit->committer, committer_len);
989 len += committer_len;
990 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
991 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
992 len += SHA1_DIGEST_LENGTH;
995 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
996 err = got_error_from_errno("imsg_compose COMMIT");
997 goto done;
1000 if (logmsg_len == 0 ||
1001 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1002 err = flush_imsg(ibuf);
1003 if (err)
1004 goto done;
1006 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1007 done:
1008 free(buf);
1009 return err;
1012 static const struct got_error *
1013 get_commit_from_imsg(struct got_commit_object **commit,
1014 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1016 const struct got_error *err = NULL;
1017 struct got_imsg_commit_object *icommit;
1018 size_t len = 0;
1019 int i;
1021 if (datalen < sizeof(*icommit))
1022 return got_error(GOT_ERR_PRIVSEP_LEN);
1024 icommit = imsg->data;
1025 if (datalen != sizeof(*icommit) + icommit->author_len +
1026 icommit->committer_len +
1027 icommit->nparents * SHA1_DIGEST_LENGTH)
1028 return got_error(GOT_ERR_PRIVSEP_LEN);
1030 if (icommit->nparents < 0)
1031 return got_error(GOT_ERR_PRIVSEP_LEN);
1033 len += sizeof(*icommit);
1035 *commit = got_object_commit_alloc_partial();
1036 if (*commit == NULL)
1037 return got_error_from_errno(
1038 "got_object_commit_alloc_partial");
1040 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1041 SHA1_DIGEST_LENGTH);
1042 (*commit)->author_time = icommit->author_time;
1043 (*commit)->author_gmtoff = icommit->author_gmtoff;
1044 (*commit)->committer_time = icommit->committer_time;
1045 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1047 if (icommit->author_len == 0) {
1048 (*commit)->author = strdup("");
1049 if ((*commit)->author == NULL) {
1050 err = got_error_from_errno("strdup");
1051 goto done;
1053 } else {
1054 (*commit)->author = malloc(icommit->author_len + 1);
1055 if ((*commit)->author == NULL) {
1056 err = got_error_from_errno("malloc");
1057 goto done;
1059 memcpy((*commit)->author, imsg->data + len,
1060 icommit->author_len);
1061 (*commit)->author[icommit->author_len] = '\0';
1063 len += icommit->author_len;
1065 if (icommit->committer_len == 0) {
1066 (*commit)->committer = strdup("");
1067 if ((*commit)->committer == NULL) {
1068 err = got_error_from_errno("strdup");
1069 goto done;
1071 } else {
1072 (*commit)->committer =
1073 malloc(icommit->committer_len + 1);
1074 if ((*commit)->committer == NULL) {
1075 err = got_error_from_errno("malloc");
1076 goto done;
1078 memcpy((*commit)->committer, imsg->data + len,
1079 icommit->committer_len);
1080 (*commit)->committer[icommit->committer_len] = '\0';
1082 len += icommit->committer_len;
1084 if (icommit->logmsg_len == 0) {
1085 (*commit)->logmsg = strdup("");
1086 if ((*commit)->logmsg == NULL) {
1087 err = got_error_from_errno("strdup");
1088 goto done;
1090 } else {
1091 size_t offset = 0, remain = icommit->logmsg_len;
1093 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1094 if ((*commit)->logmsg == NULL) {
1095 err = got_error_from_errno("malloc");
1096 goto done;
1098 while (remain > 0) {
1099 struct imsg imsg_log;
1100 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1101 remain);
1103 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1104 if (err)
1105 goto done;
1107 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1108 err = got_error(GOT_ERR_PRIVSEP_MSG);
1109 goto done;
1112 memcpy((*commit)->logmsg + offset,
1113 imsg_log.data, n);
1114 imsg_free(&imsg_log);
1115 offset += n;
1116 remain -= n;
1118 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1121 for (i = 0; i < icommit->nparents; i++) {
1122 struct got_object_qid *qid;
1124 err = got_object_qid_alloc_partial(&qid);
1125 if (err)
1126 break;
1127 memcpy(qid->id, imsg->data + len +
1128 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1129 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1130 (*commit)->nparents++;
1132 done:
1133 if (err) {
1134 got_object_commit_close(*commit);
1135 *commit = NULL;
1137 return err;
1140 const struct got_error *
1141 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1143 const struct got_error *err = NULL;
1144 struct imsg imsg;
1145 size_t datalen;
1146 const size_t min_datalen =
1147 MIN(sizeof(struct got_imsg_error),
1148 sizeof(struct got_imsg_commit_object));
1150 *commit = NULL;
1152 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1153 if (err)
1154 return err;
1156 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1158 switch (imsg.hdr.type) {
1159 case GOT_IMSG_COMMIT:
1160 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1161 break;
1162 default:
1163 err = got_error(GOT_ERR_PRIVSEP_MSG);
1164 break;
1167 imsg_free(&imsg);
1169 return err;
1172 const struct got_error *
1173 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1174 int nentries)
1176 const struct got_error *err = NULL;
1177 struct got_imsg_tree_object itree;
1178 struct got_pathlist_entry *pe;
1179 size_t totlen;
1180 int nimsg; /* number of imsg queued in ibuf */
1182 itree.nentries = nentries;
1183 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1184 == -1)
1185 return got_error_from_errno("imsg_compose TREE");
1187 totlen = sizeof(itree);
1188 nimsg = 1;
1189 TAILQ_FOREACH(pe, entries, entry) {
1190 const char *name = pe->path;
1191 struct got_parsed_tree_entry *pte = pe->data;
1192 struct ibuf *wbuf;
1193 size_t namelen = strlen(name);
1194 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1196 if (len > MAX_IMSGSIZE)
1197 return got_error(GOT_ERR_NO_SPACE);
1199 nimsg++;
1200 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1201 err = flush_imsg(ibuf);
1202 if (err)
1203 return err;
1204 nimsg = 0;
1207 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1208 if (wbuf == NULL)
1209 return got_error_from_errno("imsg_create TREE_ENTRY");
1211 /* Keep in sync with struct got_imsg_tree_object definition! */
1212 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1213 err = got_error_from_errno("imsg_add TREE_ENTRY");
1214 ibuf_free(wbuf);
1215 return err;
1217 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1218 err = got_error_from_errno("imsg_add TREE_ENTRY");
1219 ibuf_free(wbuf);
1220 return err;
1223 if (imsg_add(wbuf, name, namelen) == -1) {
1224 err = got_error_from_errno("imsg_add TREE_ENTRY");
1225 ibuf_free(wbuf);
1226 return err;
1229 wbuf->fd = -1;
1230 imsg_close(ibuf, wbuf);
1232 totlen += len;
1235 return flush_imsg(ibuf);
1238 const struct got_error *
1239 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1241 const struct got_error *err = NULL;
1242 const size_t min_datalen =
1243 MIN(sizeof(struct got_imsg_error),
1244 sizeof(struct got_imsg_tree_object));
1245 struct got_imsg_tree_object *itree;
1246 int nentries = 0;
1248 *tree = NULL;
1249 get_more:
1250 err = read_imsg(ibuf);
1251 if (err)
1252 goto done;
1254 for (;;) {
1255 struct imsg imsg;
1256 size_t n;
1257 size_t datalen;
1258 struct got_imsg_tree_entry *ite;
1259 struct got_tree_entry *te = NULL;
1261 n = imsg_get(ibuf, &imsg);
1262 if (n == 0) {
1263 if (*tree && (*tree)->nentries != nentries)
1264 goto get_more;
1265 break;
1268 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1269 return got_error(GOT_ERR_PRIVSEP_LEN);
1271 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1273 switch (imsg.hdr.type) {
1274 case GOT_IMSG_ERROR:
1275 err = recv_imsg_error(&imsg, datalen);
1276 break;
1277 case GOT_IMSG_TREE:
1278 /* This message should only appear once. */
1279 if (*tree != NULL) {
1280 err = got_error(GOT_ERR_PRIVSEP_MSG);
1281 break;
1283 if (datalen != sizeof(*itree)) {
1284 err = got_error(GOT_ERR_PRIVSEP_LEN);
1285 break;
1287 itree = imsg.data;
1288 *tree = malloc(sizeof(**tree));
1289 if (*tree == NULL) {
1290 err = got_error_from_errno("malloc");
1291 break;
1293 (*tree)->entries = calloc(itree->nentries,
1294 sizeof(struct got_tree_entry));
1295 if ((*tree)->entries == NULL) {
1296 err = got_error_from_errno("malloc");
1297 break;
1299 (*tree)->nentries = itree->nentries;
1300 (*tree)->refcnt = 0;
1301 break;
1302 case GOT_IMSG_TREE_ENTRY:
1303 /* This message should be preceeded by GOT_IMSG_TREE. */
1304 if (*tree == NULL) {
1305 err = got_error(GOT_ERR_PRIVSEP_MSG);
1306 break;
1308 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1309 err = got_error(GOT_ERR_PRIVSEP_LEN);
1310 break;
1313 /* Remaining data contains the entry's name. */
1314 datalen -= sizeof(*ite);
1315 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1316 err = got_error(GOT_ERR_PRIVSEP_LEN);
1317 break;
1319 ite = imsg.data;
1321 if (datalen + 1 > sizeof(te->name)) {
1322 err = got_error(GOT_ERR_NO_SPACE);
1323 break;
1325 te = &(*tree)->entries[nentries];
1326 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1327 te->name[datalen] = '\0';
1329 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1330 te->mode = ite->mode;
1331 te->idx = nentries;
1332 nentries++;
1333 break;
1334 default:
1335 err = got_error(GOT_ERR_PRIVSEP_MSG);
1336 break;
1339 imsg_free(&imsg);
1341 done:
1342 if (*tree && (*tree)->nentries != nentries) {
1343 if (err == NULL)
1344 err = got_error(GOT_ERR_PRIVSEP_LEN);
1345 got_object_tree_close(*tree);
1346 *tree = NULL;
1349 return err;
1352 const struct got_error *
1353 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1354 const uint8_t *data)
1356 struct got_imsg_blob iblob;
1358 iblob.size = size;
1359 iblob.hdrlen = hdrlen;
1361 if (data) {
1362 uint8_t *buf;
1364 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1365 return got_error(GOT_ERR_NO_SPACE);
1367 buf = malloc(sizeof(iblob) + size);
1368 if (buf == NULL)
1369 return got_error_from_errno("malloc");
1371 memcpy(buf, &iblob, sizeof(iblob));
1372 memcpy(buf + sizeof(iblob), data, size);
1373 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1374 sizeof(iblob) + size) == -1) {
1375 free(buf);
1376 return got_error_from_errno("imsg_compose BLOB");
1378 free(buf);
1379 } else {
1380 /* Data has already been written to file descriptor. */
1381 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1382 sizeof(iblob)) == -1)
1383 return got_error_from_errno("imsg_compose BLOB");
1387 return flush_imsg(ibuf);
1390 const struct got_error *
1391 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1392 struct imsgbuf *ibuf)
1394 const struct got_error *err = NULL;
1395 struct imsg imsg;
1396 struct got_imsg_blob *iblob;
1397 size_t datalen;
1399 *outbuf = NULL;
1401 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1402 if (err)
1403 return err;
1405 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1407 switch (imsg.hdr.type) {
1408 case GOT_IMSG_BLOB:
1409 if (datalen < sizeof(*iblob)) {
1410 err = got_error(GOT_ERR_PRIVSEP_LEN);
1411 break;
1413 iblob = imsg.data;
1414 *size = iblob->size;
1415 *hdrlen = iblob->hdrlen;
1417 if (datalen == sizeof(*iblob)) {
1418 /* Data has been written to file descriptor. */
1419 break;
1422 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1423 err = got_error(GOT_ERR_PRIVSEP_LEN);
1424 break;
1427 *outbuf = malloc(*size);
1428 if (*outbuf == NULL) {
1429 err = got_error_from_errno("malloc");
1430 break;
1432 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1433 break;
1434 default:
1435 err = got_error(GOT_ERR_PRIVSEP_MSG);
1436 break;
1439 imsg_free(&imsg);
1441 return err;
1444 static const struct got_error *
1445 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1447 const struct got_error *err = NULL;
1448 size_t offset, remain;
1450 offset = 0;
1451 remain = tagmsg_len;
1452 while (remain > 0) {
1453 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1455 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1456 tag->tagmsg + offset, n) == -1) {
1457 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1458 break;
1461 err = flush_imsg(ibuf);
1462 if (err)
1463 break;
1465 offset += n;
1466 remain -= n;
1469 return err;
1472 const struct got_error *
1473 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1475 const struct got_error *err = NULL;
1476 struct got_imsg_tag_object *itag;
1477 uint8_t *buf;
1478 size_t len, total;
1479 size_t tag_len = strlen(tag->tag);
1480 size_t tagger_len = strlen(tag->tagger);
1481 size_t tagmsg_len = strlen(tag->tagmsg);
1483 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1485 buf = malloc(total);
1486 if (buf == NULL)
1487 return got_error_from_errno("malloc");
1489 itag = (struct got_imsg_tag_object *)buf;
1490 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1491 itag->obj_type = tag->obj_type;
1492 itag->tag_len = tag_len;
1493 itag->tagger_len = tagger_len;
1494 itag->tagger_time = tag->tagger_time;
1495 itag->tagger_gmtoff = tag->tagger_gmtoff;
1496 itag->tagmsg_len = tagmsg_len;
1498 len = sizeof(*itag);
1499 memcpy(buf + len, tag->tag, tag_len);
1500 len += tag_len;
1501 memcpy(buf + len, tag->tagger, tagger_len);
1502 len += tagger_len;
1504 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1505 err = got_error_from_errno("imsg_compose TAG");
1506 goto done;
1509 if (tagmsg_len == 0 ||
1510 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1511 err = flush_imsg(ibuf);
1512 if (err)
1513 goto done;
1515 err = send_tagmsg(ibuf, tag, tagmsg_len);
1516 done:
1517 free(buf);
1518 return err;
1521 const struct got_error *
1522 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1524 const struct got_error *err = NULL;
1525 struct imsg imsg;
1526 struct got_imsg_tag_object *itag;
1527 size_t len, datalen;
1528 const size_t min_datalen =
1529 MIN(sizeof(struct got_imsg_error),
1530 sizeof(struct got_imsg_tag_object));
1532 *tag = NULL;
1534 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1535 if (err)
1536 return err;
1538 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1539 len = 0;
1541 switch (imsg.hdr.type) {
1542 case GOT_IMSG_TAG:
1543 if (datalen < sizeof(*itag)) {
1544 err = got_error(GOT_ERR_PRIVSEP_LEN);
1545 break;
1547 itag = imsg.data;
1548 if (datalen != sizeof(*itag) + itag->tag_len +
1549 itag->tagger_len) {
1550 err = got_error(GOT_ERR_PRIVSEP_LEN);
1551 break;
1553 len += sizeof(*itag);
1555 *tag = calloc(1, sizeof(**tag));
1556 if (*tag == NULL) {
1557 err = got_error_from_errno("calloc");
1558 break;
1561 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1563 if (itag->tag_len == 0) {
1564 (*tag)->tag = strdup("");
1565 if ((*tag)->tag == NULL) {
1566 err = got_error_from_errno("strdup");
1567 break;
1569 } else {
1570 (*tag)->tag = malloc(itag->tag_len + 1);
1571 if ((*tag)->tag == NULL) {
1572 err = got_error_from_errno("malloc");
1573 break;
1575 memcpy((*tag)->tag, imsg.data + len,
1576 itag->tag_len);
1577 (*tag)->tag[itag->tag_len] = '\0';
1579 len += itag->tag_len;
1581 (*tag)->obj_type = itag->obj_type;
1582 (*tag)->tagger_time = itag->tagger_time;
1583 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1585 if (itag->tagger_len == 0) {
1586 (*tag)->tagger = strdup("");
1587 if ((*tag)->tagger == NULL) {
1588 err = got_error_from_errno("strdup");
1589 break;
1591 } else {
1592 (*tag)->tagger = malloc(itag->tagger_len + 1);
1593 if ((*tag)->tagger == NULL) {
1594 err = got_error_from_errno("malloc");
1595 break;
1597 memcpy((*tag)->tagger, imsg.data + len,
1598 itag->tagger_len);
1599 (*tag)->tagger[itag->tagger_len] = '\0';
1601 len += itag->tagger_len;
1603 if (itag->tagmsg_len == 0) {
1604 (*tag)->tagmsg = strdup("");
1605 if ((*tag)->tagmsg == NULL) {
1606 err = got_error_from_errno("strdup");
1607 break;
1609 } else {
1610 size_t offset = 0, remain = itag->tagmsg_len;
1612 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1613 if ((*tag)->tagmsg == NULL) {
1614 err = got_error_from_errno("malloc");
1615 break;
1617 while (remain > 0) {
1618 struct imsg imsg_log;
1619 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1620 remain);
1622 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1623 if (err)
1624 return err;
1626 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1627 return got_error(GOT_ERR_PRIVSEP_MSG);
1629 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1630 n);
1631 imsg_free(&imsg_log);
1632 offset += n;
1633 remain -= n;
1635 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1638 break;
1639 default:
1640 err = got_error(GOT_ERR_PRIVSEP_MSG);
1641 break;
1644 imsg_free(&imsg);
1646 return err;
1649 const struct got_error *
1650 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1651 struct got_packidx *packidx)
1653 const struct got_error *err = NULL;
1654 struct got_imsg_packidx ipackidx;
1655 struct got_imsg_pack ipack;
1656 int fd;
1658 ipackidx.len = packidx->len;
1659 fd = dup(packidx->fd);
1660 if (fd == -1)
1661 return got_error_from_errno("dup");
1663 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1664 sizeof(ipackidx)) == -1) {
1665 err = got_error_from_errno("imsg_compose PACKIDX");
1666 close(fd);
1667 return err;
1670 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1671 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1672 return got_error(GOT_ERR_NO_SPACE);
1673 ipack.filesize = pack->filesize;
1675 fd = dup(pack->fd);
1676 if (fd == -1)
1677 return got_error_from_errno("dup");
1679 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1680 == -1) {
1681 err = got_error_from_errno("imsg_compose PACK");
1682 close(fd);
1683 return err;
1686 return flush_imsg(ibuf);
1689 const struct got_error *
1690 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1691 struct got_object_id *id)
1693 struct got_imsg_packed_object iobj;
1695 iobj.idx = idx;
1696 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1698 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1699 &iobj, sizeof(iobj)) == -1)
1700 return got_error_from_errno("imsg_compose "
1701 "PACKED_OBJECT_REQUEST");
1703 return flush_imsg(ibuf);
1706 const struct got_error *
1707 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1709 const struct got_error *err = NULL;
1711 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1712 NULL, 0) == -1) {
1713 err = got_error_from_errno("imsg_compose "
1714 "GITCONFIG_PARSE_REQUEST");
1715 close(fd);
1716 return err;
1719 return flush_imsg(ibuf);
1722 const struct got_error *
1723 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1725 if (imsg_compose(ibuf,
1726 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1727 NULL, 0) == -1)
1728 return got_error_from_errno("imsg_compose "
1729 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1731 return flush_imsg(ibuf);
1734 const struct got_error *
1735 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1737 if (imsg_compose(ibuf,
1738 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1739 return got_error_from_errno("imsg_compose "
1740 "GITCONFIG_AUTHOR_NAME_REQUEST");
1742 return flush_imsg(ibuf);
1745 const struct got_error *
1746 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1748 if (imsg_compose(ibuf,
1749 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1750 return got_error_from_errno("imsg_compose "
1751 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1753 return flush_imsg(ibuf);
1756 const struct got_error *
1757 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1759 if (imsg_compose(ibuf,
1760 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1761 return got_error_from_errno("imsg_compose "
1762 "GITCONFIG_REMOTE_REQUEST");
1764 return flush_imsg(ibuf);
1767 const struct got_error *
1768 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1770 if (imsg_compose(ibuf,
1771 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1772 return got_error_from_errno("imsg_compose "
1773 "GITCONFIG_OWNER_REQUEST");
1775 return flush_imsg(ibuf);
1778 const struct got_error *
1779 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1781 size_t len = value ? strlen(value) + 1 : 0;
1783 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1784 value, len) == -1)
1785 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1787 return flush_imsg(ibuf);
1790 const struct got_error *
1791 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1793 const struct got_error *err = NULL;
1794 struct imsg imsg;
1795 size_t datalen;
1796 const size_t min_datalen = 0;
1798 *str = NULL;
1800 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1801 if (err)
1802 return err;
1803 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1805 switch (imsg.hdr.type) {
1806 case GOT_IMSG_GITCONFIG_STR_VAL:
1807 if (datalen == 0)
1808 break;
1809 *str = malloc(datalen);
1810 if (*str == NULL) {
1811 err = got_error_from_errno("malloc");
1812 break;
1814 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1815 err = got_error(GOT_ERR_NO_SPACE);
1816 break;
1817 default:
1818 err = got_error(GOT_ERR_PRIVSEP_MSG);
1819 break;
1822 imsg_free(&imsg);
1823 return err;
1826 const struct got_error *
1827 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1829 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1830 &value, sizeof(value)) == -1)
1831 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1833 return flush_imsg(ibuf);
1836 const struct got_error *
1837 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1839 const struct got_error *err = NULL;
1840 struct imsg imsg;
1841 size_t datalen;
1842 const size_t min_datalen =
1843 MIN(sizeof(struct got_imsg_error), sizeof(int));
1845 *val = 0;
1847 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1848 if (err)
1849 return err;
1850 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1852 switch (imsg.hdr.type) {
1853 case GOT_IMSG_GITCONFIG_INT_VAL:
1854 if (datalen != sizeof(*val)) {
1855 err = got_error(GOT_ERR_PRIVSEP_LEN);
1856 break;
1858 memcpy(val, imsg.data, sizeof(*val));
1859 break;
1860 default:
1861 err = got_error(GOT_ERR_PRIVSEP_MSG);
1862 break;
1865 imsg_free(&imsg);
1866 return err;
1869 const struct got_error *
1870 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1871 struct got_remote_repo *remotes, int nremotes)
1873 const struct got_error *err = NULL;
1874 struct got_imsg_remotes iremotes;
1875 int i;
1877 iremotes.nremotes = nremotes;
1878 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1879 &iremotes, sizeof(iremotes)) == -1)
1880 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1882 err = flush_imsg(ibuf);
1883 imsg_clear(ibuf);
1884 if (err)
1885 return err;
1887 for (i = 0; i < nremotes; i++) {
1888 struct got_imsg_remote iremote;
1889 size_t len = sizeof(iremote);
1890 struct ibuf *wbuf;
1892 iremote.name_len = strlen(remotes[i].name);
1893 len += iremote.name_len;
1894 iremote.url_len = strlen(remotes[i].url);
1895 len += iremote.url_len;
1897 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1898 if (wbuf == NULL)
1899 return got_error_from_errno(
1900 "imsg_create GITCONFIG_REMOTE");
1902 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1903 err = got_error_from_errno(
1904 "imsg_add GIITCONFIG_REMOTE");
1905 ibuf_free(wbuf);
1906 return err;
1909 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1910 err = got_error_from_errno(
1911 "imsg_add GIITCONFIG_REMOTE");
1912 ibuf_free(wbuf);
1913 return err;
1915 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1916 err = got_error_from_errno(
1917 "imsg_add GIITCONFIG_REMOTE");
1918 ibuf_free(wbuf);
1919 return err;
1922 wbuf->fd = -1;
1923 imsg_close(ibuf, wbuf);
1924 err = flush_imsg(ibuf);
1925 if (err)
1926 return err;
1929 return NULL;
1932 const struct got_error *
1933 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1934 int *nremotes, struct imsgbuf *ibuf)
1936 const struct got_error *err = NULL;
1937 struct imsg imsg;
1938 size_t datalen;
1939 struct got_imsg_remotes iremotes;
1940 struct got_imsg_remote iremote;
1942 *remotes = NULL;
1943 *nremotes = 0;
1944 iremotes.nremotes = 0;
1946 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1947 if (err)
1948 return err;
1949 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1951 switch (imsg.hdr.type) {
1952 case GOT_IMSG_GITCONFIG_REMOTES:
1953 if (datalen != sizeof(iremotes)) {
1954 err = got_error(GOT_ERR_PRIVSEP_LEN);
1955 break;
1957 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1958 if (iremotes.nremotes == 0) {
1959 imsg_free(&imsg);
1960 return NULL;
1962 break;
1963 default:
1964 imsg_free(&imsg);
1965 return got_error(GOT_ERR_PRIVSEP_MSG);
1968 imsg_free(&imsg);
1970 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1971 if (*remotes == NULL)
1972 return got_error_from_errno("recallocarray");
1974 while (*nremotes < iremotes.nremotes) {
1975 struct got_remote_repo *remote;
1977 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1978 if (err)
1979 break;
1980 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1982 switch (imsg.hdr.type) {
1983 case GOT_IMSG_GITCONFIG_REMOTE:
1984 remote = &(*remotes)[*nremotes];
1985 if (datalen < sizeof(iremote)) {
1986 err = got_error(GOT_ERR_PRIVSEP_LEN);
1987 break;
1989 memcpy(&iremote, imsg.data, sizeof(iremote));
1990 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1991 (sizeof(iremote) + iremote.name_len +
1992 iremote.url_len) > datalen) {
1993 err = got_error(GOT_ERR_PRIVSEP_LEN);
1994 break;
1996 remote->name = strndup(imsg.data + sizeof(iremote),
1997 iremote.name_len);
1998 if (remote->name == NULL) {
1999 err = got_error_from_errno("strndup");
2000 break;
2002 remote->url = strndup(imsg.data + sizeof(iremote) +
2003 iremote.name_len, iremote.url_len);
2004 if (remote->url == NULL) {
2005 err = got_error_from_errno("strndup");
2006 free(remote->name);
2007 break;
2009 (*nremotes)++;
2010 break;
2011 default:
2012 err = got_error(GOT_ERR_PRIVSEP_MSG);
2013 break;
2016 imsg_free(&imsg);
2017 if (err)
2018 break;
2021 if (err) {
2022 int i;
2023 for (i = 0; i < *nremotes; i++) {
2024 free((*remotes)[i].name);
2025 free((*remotes)[i].url);
2027 free(*remotes);
2028 *remotes = NULL;
2029 *nremotes = 0;
2031 return err;
2034 const struct got_error *
2035 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2036 struct got_object_id *id, int idx, const char *path)
2038 const struct got_error *err = NULL;
2039 struct ibuf *wbuf;
2040 size_t path_len = strlen(path) + 1;
2042 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2043 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2044 if (wbuf == NULL)
2045 return got_error_from_errno(
2046 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2047 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2048 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2049 ibuf_free(wbuf);
2050 return err;
2052 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2053 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2054 ibuf_free(wbuf);
2055 return err;
2057 if (imsg_add(wbuf, path, path_len) == -1) {
2058 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2059 ibuf_free(wbuf);
2060 return err;
2063 wbuf->fd = -1;
2064 imsg_close(ibuf, wbuf);
2066 return flush_imsg(ibuf);
2069 const struct got_error *
2070 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2071 size_t ncommits, struct imsgbuf *ibuf)
2073 const struct got_error *err;
2074 struct ibuf *wbuf;
2075 int i;
2077 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2078 sizeof(struct got_imsg_traversed_commits) +
2079 ncommits * SHA1_DIGEST_LENGTH);
2080 if (wbuf == NULL)
2081 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2083 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2084 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2085 ibuf_free(wbuf);
2086 return err;
2088 for (i = 0; i < ncommits; i++) {
2089 struct got_object_id *id = &commit_ids[i];
2090 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2091 err = got_error_from_errno(
2092 "imsg_add TRAVERSED_COMMITS");
2093 ibuf_free(wbuf);
2094 return err;
2098 wbuf->fd = -1;
2099 imsg_close(ibuf, wbuf);
2101 return flush_imsg(ibuf);
2104 const struct got_error *
2105 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2106 struct got_object_id **changed_commit_id,
2107 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2109 const struct got_error *err = NULL;
2110 struct imsg imsg;
2111 struct got_imsg_traversed_commits *icommits;
2112 size_t datalen;
2113 int i, done = 0;
2115 *changed_commit = NULL;
2116 *changed_commit_id = NULL;
2118 while (!done) {
2119 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2120 if (err)
2121 return err;
2123 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2124 switch (imsg.hdr.type) {
2125 case GOT_IMSG_TRAVERSED_COMMITS:
2126 icommits = imsg.data;
2127 if (datalen != sizeof(*icommits) +
2128 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2129 err = got_error(GOT_ERR_PRIVSEP_LEN);
2130 break;
2132 for (i = 0; i < icommits->ncommits; i++) {
2133 struct got_object_qid *qid;
2134 uint8_t *sha1 = (uint8_t *)imsg.data +
2135 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2136 err = got_object_qid_alloc_partial(&qid);
2137 if (err)
2138 break;
2139 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2140 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2142 /* The last commit may contain a change. */
2143 if (i == icommits->ncommits - 1) {
2144 *changed_commit_id =
2145 got_object_id_dup(qid->id);
2146 if (*changed_commit_id == NULL) {
2147 err = got_error_from_errno(
2148 "got_object_id_dup");
2149 break;
2153 break;
2154 case GOT_IMSG_COMMIT:
2155 if (*changed_commit_id == NULL) {
2156 err = got_error(GOT_ERR_PRIVSEP_MSG);
2157 break;
2159 err = get_commit_from_imsg(changed_commit, &imsg,
2160 datalen, ibuf);
2161 break;
2162 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2163 done = 1;
2164 break;
2165 default:
2166 err = got_error(GOT_ERR_PRIVSEP_MSG);
2167 break;
2170 imsg_free(&imsg);
2171 if (err)
2172 break;
2175 if (err)
2176 got_object_id_queue_free(commit_ids);
2177 return err;
2180 const struct got_error *
2181 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2183 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2184 NULL, 0) == -1)
2185 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2187 return flush_imsg(ibuf);
2190 const struct got_error *
2191 got_privsep_unveil_exec_helpers(void)
2193 const char *helpers[] = {
2194 GOT_PATH_PROG_READ_PACK,
2195 GOT_PATH_PROG_READ_OBJECT,
2196 GOT_PATH_PROG_READ_COMMIT,
2197 GOT_PATH_PROG_READ_TREE,
2198 GOT_PATH_PROG_READ_BLOB,
2199 GOT_PATH_PROG_READ_TAG,
2200 GOT_PATH_PROG_READ_GITCONFIG,
2202 int i;
2204 for (i = 0; i < nitems(helpers); i++) {
2205 if (unveil(helpers[i], "x") == 0)
2206 continue;
2207 return got_error_from_errno2("unveil", helpers[i]);
2210 return NULL;
2213 void
2214 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2216 if (close(imsg_fds[0]) != 0) {
2217 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2218 _exit(1);
2221 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2222 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2223 _exit(1);
2225 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2226 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2227 _exit(1);
2230 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2231 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2232 strerror(errno));
2233 _exit(1);