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, int fd, struct got_object_id *hash)
772 const struct got_error *err = NULL;
774 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
775 hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
776 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
777 close(fd);
778 return err;
780 return flush_imsg(ibuf);
783 const struct got_error *
784 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobjects_total,
785 int nobjects_indexed)
787 struct got_imsg_index_pack_progress iprogress;
789 iprogress.nobjects_total = nobjects_total;
790 iprogress.nobjects_indexed = nobjects_indexed;
792 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
793 &iprogress, sizeof(iprogress)) == -1)
794 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
796 return flush_imsg(ibuf);
799 const struct got_error *
800 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
802 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
803 return got_error_from_errno("imsg_compose FETCH");
804 return flush_imsg(ibuf);
807 const struct got_error *
808 got_privsep_recv_index_progress(int *done, int *nobjects_total,
809 int *nobjects_indexed, struct imsgbuf *ibuf)
811 const struct got_error *err = NULL;
812 struct imsg imsg;
813 struct got_imsg_index_pack_progress *iprogress;
814 size_t datalen;
816 *done = 0;
817 *nobjects_total = 0;
818 *nobjects_indexed = 0;
820 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
821 if (err)
822 return err;
824 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
825 switch (imsg.hdr.type) {
826 case GOT_IMSG_ERROR:
827 if (datalen < sizeof(struct got_imsg_error)) {
828 err = got_error(GOT_ERR_PRIVSEP_LEN);
829 break;
831 err = recv_imsg_error(&imsg, datalen);
832 break;
833 case GOT_IMSG_IDXPACK_PROGRESS:
834 if (datalen < sizeof(*iprogress)) {
835 err = got_error(GOT_ERR_PRIVSEP_LEN);
836 break;
838 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
839 *nobjects_total = iprogress->nobjects_total;
840 *nobjects_indexed = iprogress->nobjects_indexed;
841 break;
842 case GOT_IMSG_IDXPACK_DONE:
843 if (datalen != 0) {
844 err = got_error(GOT_ERR_PRIVSEP_LEN);
845 break;
847 *done = 1;
848 break;
849 default:
850 err = got_error(GOT_ERR_PRIVSEP_MSG);
851 break;
854 imsg_free(&imsg);
855 return err;
858 const struct got_error *
859 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
860 struct imsgbuf *ibuf)
862 const struct got_error *err = NULL;
863 struct got_imsg_object *iobj;
864 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
866 if (datalen != sizeof(*iobj))
867 return got_error(GOT_ERR_PRIVSEP_LEN);
868 iobj = imsg->data;
870 *obj = calloc(1, sizeof(**obj));
871 if (*obj == NULL)
872 return got_error_from_errno("calloc");
874 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
875 (*obj)->type = iobj->type;
876 (*obj)->flags = iobj->flags;
877 (*obj)->hdrlen = iobj->hdrlen;
878 (*obj)->size = iobj->size;
879 /* path_packfile is handled by caller */
880 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
881 (*obj)->pack_offset = iobj->pack_offset;
882 (*obj)->pack_idx = iobj->pack_idx;
885 return err;
888 const struct got_error *
889 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
891 const struct got_error *err = NULL;
892 struct imsg imsg;
893 const size_t min_datalen =
894 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
896 *obj = NULL;
898 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
899 if (err)
900 return err;
902 switch (imsg.hdr.type) {
903 case GOT_IMSG_OBJECT:
904 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
905 break;
906 default:
907 err = got_error(GOT_ERR_PRIVSEP_MSG);
908 break;
911 imsg_free(&imsg);
913 return err;
916 static const struct got_error *
917 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
918 size_t logmsg_len)
920 const struct got_error *err = NULL;
921 size_t offset, remain;
923 offset = 0;
924 remain = logmsg_len;
925 while (remain > 0) {
926 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
928 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
929 commit->logmsg + offset, n) == -1) {
930 err = got_error_from_errno("imsg_compose "
931 "COMMIT_LOGMSG");
932 break;
935 err = flush_imsg(ibuf);
936 if (err)
937 break;
939 offset += n;
940 remain -= n;
943 return err;
946 const struct got_error *
947 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
949 const struct got_error *err = NULL;
950 struct got_imsg_commit_object *icommit;
951 uint8_t *buf;
952 size_t len, total;
953 struct got_object_qid *qid;
954 size_t author_len = strlen(commit->author);
955 size_t committer_len = strlen(commit->committer);
956 size_t logmsg_len = strlen(commit->logmsg);
958 total = sizeof(*icommit) + author_len + committer_len +
959 commit->nparents * SHA1_DIGEST_LENGTH;
961 buf = malloc(total);
962 if (buf == NULL)
963 return got_error_from_errno("malloc");
965 icommit = (struct got_imsg_commit_object *)buf;
966 memcpy(icommit->tree_id, commit->tree_id->sha1,
967 sizeof(icommit->tree_id));
968 icommit->author_len = author_len;
969 icommit->author_time = commit->author_time;
970 icommit->author_gmtoff = commit->author_gmtoff;
971 icommit->committer_len = committer_len;
972 icommit->committer_time = commit->committer_time;
973 icommit->committer_gmtoff = commit->committer_gmtoff;
974 icommit->logmsg_len = logmsg_len;
975 icommit->nparents = commit->nparents;
977 len = sizeof(*icommit);
978 memcpy(buf + len, commit->author, author_len);
979 len += author_len;
980 memcpy(buf + len, commit->committer, committer_len);
981 len += committer_len;
982 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
983 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
984 len += SHA1_DIGEST_LENGTH;
987 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
988 err = got_error_from_errno("imsg_compose COMMIT");
989 goto done;
992 if (logmsg_len == 0 ||
993 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
994 err = flush_imsg(ibuf);
995 if (err)
996 goto done;
998 err = send_commit_logmsg(ibuf, commit, logmsg_len);
999 done:
1000 free(buf);
1001 return err;
1004 static const struct got_error *
1005 get_commit_from_imsg(struct got_commit_object **commit,
1006 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1008 const struct got_error *err = NULL;
1009 struct got_imsg_commit_object *icommit;
1010 size_t len = 0;
1011 int i;
1013 if (datalen < sizeof(*icommit))
1014 return got_error(GOT_ERR_PRIVSEP_LEN);
1016 icommit = imsg->data;
1017 if (datalen != sizeof(*icommit) + icommit->author_len +
1018 icommit->committer_len +
1019 icommit->nparents * SHA1_DIGEST_LENGTH)
1020 return got_error(GOT_ERR_PRIVSEP_LEN);
1022 if (icommit->nparents < 0)
1023 return got_error(GOT_ERR_PRIVSEP_LEN);
1025 len += sizeof(*icommit);
1027 *commit = got_object_commit_alloc_partial();
1028 if (*commit == NULL)
1029 return got_error_from_errno(
1030 "got_object_commit_alloc_partial");
1032 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1033 SHA1_DIGEST_LENGTH);
1034 (*commit)->author_time = icommit->author_time;
1035 (*commit)->author_gmtoff = icommit->author_gmtoff;
1036 (*commit)->committer_time = icommit->committer_time;
1037 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1039 if (icommit->author_len == 0) {
1040 (*commit)->author = strdup("");
1041 if ((*commit)->author == NULL) {
1042 err = got_error_from_errno("strdup");
1043 goto done;
1045 } else {
1046 (*commit)->author = malloc(icommit->author_len + 1);
1047 if ((*commit)->author == NULL) {
1048 err = got_error_from_errno("malloc");
1049 goto done;
1051 memcpy((*commit)->author, imsg->data + len,
1052 icommit->author_len);
1053 (*commit)->author[icommit->author_len] = '\0';
1055 len += icommit->author_len;
1057 if (icommit->committer_len == 0) {
1058 (*commit)->committer = strdup("");
1059 if ((*commit)->committer == NULL) {
1060 err = got_error_from_errno("strdup");
1061 goto done;
1063 } else {
1064 (*commit)->committer =
1065 malloc(icommit->committer_len + 1);
1066 if ((*commit)->committer == NULL) {
1067 err = got_error_from_errno("malloc");
1068 goto done;
1070 memcpy((*commit)->committer, imsg->data + len,
1071 icommit->committer_len);
1072 (*commit)->committer[icommit->committer_len] = '\0';
1074 len += icommit->committer_len;
1076 if (icommit->logmsg_len == 0) {
1077 (*commit)->logmsg = strdup("");
1078 if ((*commit)->logmsg == NULL) {
1079 err = got_error_from_errno("strdup");
1080 goto done;
1082 } else {
1083 size_t offset = 0, remain = icommit->logmsg_len;
1085 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1086 if ((*commit)->logmsg == NULL) {
1087 err = got_error_from_errno("malloc");
1088 goto done;
1090 while (remain > 0) {
1091 struct imsg imsg_log;
1092 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1093 remain);
1095 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1096 if (err)
1097 goto done;
1099 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1100 err = got_error(GOT_ERR_PRIVSEP_MSG);
1101 goto done;
1104 memcpy((*commit)->logmsg + offset,
1105 imsg_log.data, n);
1106 imsg_free(&imsg_log);
1107 offset += n;
1108 remain -= n;
1110 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1113 for (i = 0; i < icommit->nparents; i++) {
1114 struct got_object_qid *qid;
1116 err = got_object_qid_alloc_partial(&qid);
1117 if (err)
1118 break;
1119 memcpy(qid->id, imsg->data + len +
1120 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1121 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1122 (*commit)->nparents++;
1124 done:
1125 if (err) {
1126 got_object_commit_close(*commit);
1127 *commit = NULL;
1129 return err;
1132 const struct got_error *
1133 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1135 const struct got_error *err = NULL;
1136 struct imsg imsg;
1137 size_t datalen;
1138 const size_t min_datalen =
1139 MIN(sizeof(struct got_imsg_error),
1140 sizeof(struct got_imsg_commit_object));
1142 *commit = NULL;
1144 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1145 if (err)
1146 return err;
1148 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1150 switch (imsg.hdr.type) {
1151 case GOT_IMSG_COMMIT:
1152 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1153 break;
1154 default:
1155 err = got_error(GOT_ERR_PRIVSEP_MSG);
1156 break;
1159 imsg_free(&imsg);
1161 return err;
1164 const struct got_error *
1165 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1166 int nentries)
1168 const struct got_error *err = NULL;
1169 struct got_imsg_tree_object itree;
1170 struct got_pathlist_entry *pe;
1171 size_t totlen;
1172 int nimsg; /* number of imsg queued in ibuf */
1174 itree.nentries = nentries;
1175 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1176 == -1)
1177 return got_error_from_errno("imsg_compose TREE");
1179 totlen = sizeof(itree);
1180 nimsg = 1;
1181 TAILQ_FOREACH(pe, entries, entry) {
1182 const char *name = pe->path;
1183 struct got_parsed_tree_entry *pte = pe->data;
1184 struct ibuf *wbuf;
1185 size_t namelen = strlen(name);
1186 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1188 if (len > MAX_IMSGSIZE)
1189 return got_error(GOT_ERR_NO_SPACE);
1191 nimsg++;
1192 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1193 err = flush_imsg(ibuf);
1194 if (err)
1195 return err;
1196 nimsg = 0;
1199 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1200 if (wbuf == NULL)
1201 return got_error_from_errno("imsg_create TREE_ENTRY");
1203 /* Keep in sync with struct got_imsg_tree_object definition! */
1204 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1205 err = got_error_from_errno("imsg_add TREE_ENTRY");
1206 ibuf_free(wbuf);
1207 return err;
1209 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1210 err = got_error_from_errno("imsg_add TREE_ENTRY");
1211 ibuf_free(wbuf);
1212 return err;
1215 if (imsg_add(wbuf, name, namelen) == -1) {
1216 err = got_error_from_errno("imsg_add TREE_ENTRY");
1217 ibuf_free(wbuf);
1218 return err;
1221 wbuf->fd = -1;
1222 imsg_close(ibuf, wbuf);
1224 totlen += len;
1227 return flush_imsg(ibuf);
1230 const struct got_error *
1231 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1233 const struct got_error *err = NULL;
1234 const size_t min_datalen =
1235 MIN(sizeof(struct got_imsg_error),
1236 sizeof(struct got_imsg_tree_object));
1237 struct got_imsg_tree_object *itree;
1238 int nentries = 0;
1240 *tree = NULL;
1241 get_more:
1242 err = read_imsg(ibuf);
1243 if (err)
1244 goto done;
1246 for (;;) {
1247 struct imsg imsg;
1248 size_t n;
1249 size_t datalen;
1250 struct got_imsg_tree_entry *ite;
1251 struct got_tree_entry *te = NULL;
1253 n = imsg_get(ibuf, &imsg);
1254 if (n == 0) {
1255 if (*tree && (*tree)->nentries != nentries)
1256 goto get_more;
1257 break;
1260 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1261 return got_error(GOT_ERR_PRIVSEP_LEN);
1263 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1265 switch (imsg.hdr.type) {
1266 case GOT_IMSG_ERROR:
1267 err = recv_imsg_error(&imsg, datalen);
1268 break;
1269 case GOT_IMSG_TREE:
1270 /* This message should only appear once. */
1271 if (*tree != NULL) {
1272 err = got_error(GOT_ERR_PRIVSEP_MSG);
1273 break;
1275 if (datalen != sizeof(*itree)) {
1276 err = got_error(GOT_ERR_PRIVSEP_LEN);
1277 break;
1279 itree = imsg.data;
1280 *tree = malloc(sizeof(**tree));
1281 if (*tree == NULL) {
1282 err = got_error_from_errno("malloc");
1283 break;
1285 (*tree)->entries = calloc(itree->nentries,
1286 sizeof(struct got_tree_entry));
1287 if ((*tree)->entries == NULL) {
1288 err = got_error_from_errno("malloc");
1289 break;
1291 (*tree)->nentries = itree->nentries;
1292 (*tree)->refcnt = 0;
1293 break;
1294 case GOT_IMSG_TREE_ENTRY:
1295 /* This message should be preceeded by GOT_IMSG_TREE. */
1296 if (*tree == NULL) {
1297 err = got_error(GOT_ERR_PRIVSEP_MSG);
1298 break;
1300 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1301 err = got_error(GOT_ERR_PRIVSEP_LEN);
1302 break;
1305 /* Remaining data contains the entry's name. */
1306 datalen -= sizeof(*ite);
1307 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1308 err = got_error(GOT_ERR_PRIVSEP_LEN);
1309 break;
1311 ite = imsg.data;
1313 if (datalen + 1 > sizeof(te->name)) {
1314 err = got_error(GOT_ERR_NO_SPACE);
1315 break;
1317 te = &(*tree)->entries[nentries];
1318 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1319 te->name[datalen] = '\0';
1321 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1322 te->mode = ite->mode;
1323 te->idx = nentries;
1324 nentries++;
1325 break;
1326 default:
1327 err = got_error(GOT_ERR_PRIVSEP_MSG);
1328 break;
1331 imsg_free(&imsg);
1333 done:
1334 if (*tree && (*tree)->nentries != nentries) {
1335 if (err == NULL)
1336 err = got_error(GOT_ERR_PRIVSEP_LEN);
1337 got_object_tree_close(*tree);
1338 *tree = NULL;
1341 return err;
1344 const struct got_error *
1345 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1346 const uint8_t *data)
1348 struct got_imsg_blob iblob;
1350 iblob.size = size;
1351 iblob.hdrlen = hdrlen;
1353 if (data) {
1354 uint8_t *buf;
1356 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1357 return got_error(GOT_ERR_NO_SPACE);
1359 buf = malloc(sizeof(iblob) + size);
1360 if (buf == NULL)
1361 return got_error_from_errno("malloc");
1363 memcpy(buf, &iblob, sizeof(iblob));
1364 memcpy(buf + sizeof(iblob), data, size);
1365 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1366 sizeof(iblob) + size) == -1) {
1367 free(buf);
1368 return got_error_from_errno("imsg_compose BLOB");
1370 free(buf);
1371 } else {
1372 /* Data has already been written to file descriptor. */
1373 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1374 sizeof(iblob)) == -1)
1375 return got_error_from_errno("imsg_compose BLOB");
1379 return flush_imsg(ibuf);
1382 const struct got_error *
1383 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1384 struct imsgbuf *ibuf)
1386 const struct got_error *err = NULL;
1387 struct imsg imsg;
1388 struct got_imsg_blob *iblob;
1389 size_t datalen;
1391 *outbuf = NULL;
1393 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1394 if (err)
1395 return err;
1397 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1399 switch (imsg.hdr.type) {
1400 case GOT_IMSG_BLOB:
1401 if (datalen < sizeof(*iblob)) {
1402 err = got_error(GOT_ERR_PRIVSEP_LEN);
1403 break;
1405 iblob = imsg.data;
1406 *size = iblob->size;
1407 *hdrlen = iblob->hdrlen;
1409 if (datalen == sizeof(*iblob)) {
1410 /* Data has been written to file descriptor. */
1411 break;
1414 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1415 err = got_error(GOT_ERR_PRIVSEP_LEN);
1416 break;
1419 *outbuf = malloc(*size);
1420 if (*outbuf == NULL) {
1421 err = got_error_from_errno("malloc");
1422 break;
1424 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1425 break;
1426 default:
1427 err = got_error(GOT_ERR_PRIVSEP_MSG);
1428 break;
1431 imsg_free(&imsg);
1433 return err;
1436 static const struct got_error *
1437 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1439 const struct got_error *err = NULL;
1440 size_t offset, remain;
1442 offset = 0;
1443 remain = tagmsg_len;
1444 while (remain > 0) {
1445 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1447 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1448 tag->tagmsg + offset, n) == -1) {
1449 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1450 break;
1453 err = flush_imsg(ibuf);
1454 if (err)
1455 break;
1457 offset += n;
1458 remain -= n;
1461 return err;
1464 const struct got_error *
1465 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1467 const struct got_error *err = NULL;
1468 struct got_imsg_tag_object *itag;
1469 uint8_t *buf;
1470 size_t len, total;
1471 size_t tag_len = strlen(tag->tag);
1472 size_t tagger_len = strlen(tag->tagger);
1473 size_t tagmsg_len = strlen(tag->tagmsg);
1475 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1477 buf = malloc(total);
1478 if (buf == NULL)
1479 return got_error_from_errno("malloc");
1481 itag = (struct got_imsg_tag_object *)buf;
1482 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1483 itag->obj_type = tag->obj_type;
1484 itag->tag_len = tag_len;
1485 itag->tagger_len = tagger_len;
1486 itag->tagger_time = tag->tagger_time;
1487 itag->tagger_gmtoff = tag->tagger_gmtoff;
1488 itag->tagmsg_len = tagmsg_len;
1490 len = sizeof(*itag);
1491 memcpy(buf + len, tag->tag, tag_len);
1492 len += tag_len;
1493 memcpy(buf + len, tag->tagger, tagger_len);
1494 len += tagger_len;
1496 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1497 err = got_error_from_errno("imsg_compose TAG");
1498 goto done;
1501 if (tagmsg_len == 0 ||
1502 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1503 err = flush_imsg(ibuf);
1504 if (err)
1505 goto done;
1507 err = send_tagmsg(ibuf, tag, tagmsg_len);
1508 done:
1509 free(buf);
1510 return err;
1513 const struct got_error *
1514 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1516 const struct got_error *err = NULL;
1517 struct imsg imsg;
1518 struct got_imsg_tag_object *itag;
1519 size_t len, datalen;
1520 const size_t min_datalen =
1521 MIN(sizeof(struct got_imsg_error),
1522 sizeof(struct got_imsg_tag_object));
1524 *tag = NULL;
1526 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1527 if (err)
1528 return err;
1530 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1531 len = 0;
1533 switch (imsg.hdr.type) {
1534 case GOT_IMSG_TAG:
1535 if (datalen < sizeof(*itag)) {
1536 err = got_error(GOT_ERR_PRIVSEP_LEN);
1537 break;
1539 itag = imsg.data;
1540 if (datalen != sizeof(*itag) + itag->tag_len +
1541 itag->tagger_len) {
1542 err = got_error(GOT_ERR_PRIVSEP_LEN);
1543 break;
1545 len += sizeof(*itag);
1547 *tag = calloc(1, sizeof(**tag));
1548 if (*tag == NULL) {
1549 err = got_error_from_errno("calloc");
1550 break;
1553 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1555 if (itag->tag_len == 0) {
1556 (*tag)->tag = strdup("");
1557 if ((*tag)->tag == NULL) {
1558 err = got_error_from_errno("strdup");
1559 break;
1561 } else {
1562 (*tag)->tag = malloc(itag->tag_len + 1);
1563 if ((*tag)->tag == NULL) {
1564 err = got_error_from_errno("malloc");
1565 break;
1567 memcpy((*tag)->tag, imsg.data + len,
1568 itag->tag_len);
1569 (*tag)->tag[itag->tag_len] = '\0';
1571 len += itag->tag_len;
1573 (*tag)->obj_type = itag->obj_type;
1574 (*tag)->tagger_time = itag->tagger_time;
1575 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1577 if (itag->tagger_len == 0) {
1578 (*tag)->tagger = strdup("");
1579 if ((*tag)->tagger == NULL) {
1580 err = got_error_from_errno("strdup");
1581 break;
1583 } else {
1584 (*tag)->tagger = malloc(itag->tagger_len + 1);
1585 if ((*tag)->tagger == NULL) {
1586 err = got_error_from_errno("malloc");
1587 break;
1589 memcpy((*tag)->tagger, imsg.data + len,
1590 itag->tagger_len);
1591 (*tag)->tagger[itag->tagger_len] = '\0';
1593 len += itag->tagger_len;
1595 if (itag->tagmsg_len == 0) {
1596 (*tag)->tagmsg = strdup("");
1597 if ((*tag)->tagmsg == NULL) {
1598 err = got_error_from_errno("strdup");
1599 break;
1601 } else {
1602 size_t offset = 0, remain = itag->tagmsg_len;
1604 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1605 if ((*tag)->tagmsg == NULL) {
1606 err = got_error_from_errno("malloc");
1607 break;
1609 while (remain > 0) {
1610 struct imsg imsg_log;
1611 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1612 remain);
1614 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1615 if (err)
1616 return err;
1618 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1619 return got_error(GOT_ERR_PRIVSEP_MSG);
1621 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1622 n);
1623 imsg_free(&imsg_log);
1624 offset += n;
1625 remain -= n;
1627 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1630 break;
1631 default:
1632 err = got_error(GOT_ERR_PRIVSEP_MSG);
1633 break;
1636 imsg_free(&imsg);
1638 return err;
1641 const struct got_error *
1642 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1643 struct got_packidx *packidx)
1645 const struct got_error *err = NULL;
1646 struct got_imsg_packidx ipackidx;
1647 struct got_imsg_pack ipack;
1648 int fd;
1650 ipackidx.len = packidx->len;
1651 fd = dup(packidx->fd);
1652 if (fd == -1)
1653 return got_error_from_errno("dup");
1655 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1656 sizeof(ipackidx)) == -1) {
1657 err = got_error_from_errno("imsg_compose PACKIDX");
1658 close(fd);
1659 return err;
1662 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1663 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1664 return got_error(GOT_ERR_NO_SPACE);
1665 ipack.filesize = pack->filesize;
1667 fd = dup(pack->fd);
1668 if (fd == -1)
1669 return got_error_from_errno("dup");
1671 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1672 == -1) {
1673 err = got_error_from_errno("imsg_compose PACK");
1674 close(fd);
1675 return err;
1678 return flush_imsg(ibuf);
1681 const struct got_error *
1682 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1683 struct got_object_id *id)
1685 struct got_imsg_packed_object iobj;
1687 iobj.idx = idx;
1688 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1690 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1691 &iobj, sizeof(iobj)) == -1)
1692 return got_error_from_errno("imsg_compose "
1693 "PACKED_OBJECT_REQUEST");
1695 return flush_imsg(ibuf);
1698 const struct got_error *
1699 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1701 const struct got_error *err = NULL;
1703 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1704 NULL, 0) == -1) {
1705 err = got_error_from_errno("imsg_compose "
1706 "GITCONFIG_PARSE_REQUEST");
1707 close(fd);
1708 return err;
1711 return flush_imsg(ibuf);
1714 const struct got_error *
1715 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1717 if (imsg_compose(ibuf,
1718 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1719 NULL, 0) == -1)
1720 return got_error_from_errno("imsg_compose "
1721 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1723 return flush_imsg(ibuf);
1726 const struct got_error *
1727 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1729 if (imsg_compose(ibuf,
1730 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1731 return got_error_from_errno("imsg_compose "
1732 "GITCONFIG_AUTHOR_NAME_REQUEST");
1734 return flush_imsg(ibuf);
1737 const struct got_error *
1738 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1740 if (imsg_compose(ibuf,
1741 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1742 return got_error_from_errno("imsg_compose "
1743 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1745 return flush_imsg(ibuf);
1748 const struct got_error *
1749 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1751 if (imsg_compose(ibuf,
1752 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1753 return got_error_from_errno("imsg_compose "
1754 "GITCONFIG_REMOTE_REQUEST");
1756 return flush_imsg(ibuf);
1759 const struct got_error *
1760 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1762 if (imsg_compose(ibuf,
1763 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1764 return got_error_from_errno("imsg_compose "
1765 "GITCONFIG_OWNER_REQUEST");
1767 return flush_imsg(ibuf);
1770 const struct got_error *
1771 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1773 size_t len = value ? strlen(value) + 1 : 0;
1775 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1776 value, len) == -1)
1777 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1779 return flush_imsg(ibuf);
1782 const struct got_error *
1783 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1785 const struct got_error *err = NULL;
1786 struct imsg imsg;
1787 size_t datalen;
1788 const size_t min_datalen = 0;
1790 *str = NULL;
1792 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1793 if (err)
1794 return err;
1795 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1797 switch (imsg.hdr.type) {
1798 case GOT_IMSG_GITCONFIG_STR_VAL:
1799 if (datalen == 0)
1800 break;
1801 *str = malloc(datalen);
1802 if (*str == NULL) {
1803 err = got_error_from_errno("malloc");
1804 break;
1806 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1807 err = got_error(GOT_ERR_NO_SPACE);
1808 break;
1809 default:
1810 err = got_error(GOT_ERR_PRIVSEP_MSG);
1811 break;
1814 imsg_free(&imsg);
1815 return err;
1818 const struct got_error *
1819 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1821 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1822 &value, sizeof(value)) == -1)
1823 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1825 return flush_imsg(ibuf);
1828 const struct got_error *
1829 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1831 const struct got_error *err = NULL;
1832 struct imsg imsg;
1833 size_t datalen;
1834 const size_t min_datalen =
1835 MIN(sizeof(struct got_imsg_error), sizeof(int));
1837 *val = 0;
1839 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1840 if (err)
1841 return err;
1842 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1844 switch (imsg.hdr.type) {
1845 case GOT_IMSG_GITCONFIG_INT_VAL:
1846 if (datalen != sizeof(*val)) {
1847 err = got_error(GOT_ERR_PRIVSEP_LEN);
1848 break;
1850 memcpy(val, imsg.data, sizeof(*val));
1851 break;
1852 default:
1853 err = got_error(GOT_ERR_PRIVSEP_MSG);
1854 break;
1857 imsg_free(&imsg);
1858 return err;
1861 const struct got_error *
1862 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1863 struct got_remote_repo *remotes, int nremotes)
1865 const struct got_error *err = NULL;
1866 struct got_imsg_remotes iremotes;
1867 int i;
1869 iremotes.nremotes = nremotes;
1870 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1871 &iremotes, sizeof(iremotes)) == -1)
1872 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1874 err = flush_imsg(ibuf);
1875 imsg_clear(ibuf);
1876 if (err)
1877 return err;
1879 for (i = 0; i < nremotes; i++) {
1880 struct got_imsg_remote iremote;
1881 size_t len = sizeof(iremote);
1882 struct ibuf *wbuf;
1884 iremote.name_len = strlen(remotes[i].name);
1885 len += iremote.name_len;
1886 iremote.url_len = strlen(remotes[i].url);
1887 len += iremote.url_len;
1889 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1890 if (wbuf == NULL)
1891 return got_error_from_errno(
1892 "imsg_create GITCONFIG_REMOTE");
1894 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1895 err = got_error_from_errno(
1896 "imsg_add GIITCONFIG_REMOTE");
1897 ibuf_free(wbuf);
1898 return err;
1901 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1902 err = got_error_from_errno(
1903 "imsg_add GIITCONFIG_REMOTE");
1904 ibuf_free(wbuf);
1905 return err;
1907 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1908 err = got_error_from_errno(
1909 "imsg_add GIITCONFIG_REMOTE");
1910 ibuf_free(wbuf);
1911 return err;
1914 wbuf->fd = -1;
1915 imsg_close(ibuf, wbuf);
1916 err = flush_imsg(ibuf);
1917 if (err)
1918 return err;
1921 return NULL;
1924 const struct got_error *
1925 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1926 int *nremotes, struct imsgbuf *ibuf)
1928 const struct got_error *err = NULL;
1929 struct imsg imsg;
1930 size_t datalen;
1931 struct got_imsg_remotes iremotes;
1932 struct got_imsg_remote iremote;
1934 *remotes = NULL;
1935 *nremotes = 0;
1936 iremotes.nremotes = 0;
1938 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1939 if (err)
1940 return err;
1941 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1943 switch (imsg.hdr.type) {
1944 case GOT_IMSG_GITCONFIG_REMOTES:
1945 if (datalen != sizeof(iremotes)) {
1946 err = got_error(GOT_ERR_PRIVSEP_LEN);
1947 break;
1949 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1950 if (iremotes.nremotes == 0) {
1951 imsg_free(&imsg);
1952 return NULL;
1954 break;
1955 default:
1956 imsg_free(&imsg);
1957 return got_error(GOT_ERR_PRIVSEP_MSG);
1960 imsg_free(&imsg);
1962 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1963 if (*remotes == NULL)
1964 return got_error_from_errno("recallocarray");
1966 while (*nremotes < iremotes.nremotes) {
1967 struct got_remote_repo *remote;
1969 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1970 if (err)
1971 break;
1972 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1974 switch (imsg.hdr.type) {
1975 case GOT_IMSG_GITCONFIG_REMOTE:
1976 remote = &(*remotes)[*nremotes];
1977 if (datalen < sizeof(iremote)) {
1978 err = got_error(GOT_ERR_PRIVSEP_LEN);
1979 break;
1981 memcpy(&iremote, imsg.data, sizeof(iremote));
1982 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1983 (sizeof(iremote) + iremote.name_len +
1984 iremote.url_len) > datalen) {
1985 err = got_error(GOT_ERR_PRIVSEP_LEN);
1986 break;
1988 remote->name = strndup(imsg.data + sizeof(iremote),
1989 iremote.name_len);
1990 if (remote->name == NULL) {
1991 err = got_error_from_errno("strndup");
1992 break;
1994 remote->url = strndup(imsg.data + sizeof(iremote) +
1995 iremote.name_len, iremote.url_len);
1996 if (remote->url == NULL) {
1997 err = got_error_from_errno("strndup");
1998 free(remote->name);
1999 break;
2001 (*nremotes)++;
2002 break;
2003 default:
2004 err = got_error(GOT_ERR_PRIVSEP_MSG);
2005 break;
2008 imsg_free(&imsg);
2009 if (err)
2010 break;
2013 if (err) {
2014 int i;
2015 for (i = 0; i < *nremotes; i++) {
2016 free((*remotes)[i].name);
2017 free((*remotes)[i].url);
2019 free(*remotes);
2020 *remotes = NULL;
2021 *nremotes = 0;
2023 return err;
2026 const struct got_error *
2027 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2028 struct got_object_id *id, int idx, const char *path)
2030 const struct got_error *err = NULL;
2031 struct ibuf *wbuf;
2032 size_t path_len = strlen(path) + 1;
2034 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2035 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2036 if (wbuf == NULL)
2037 return got_error_from_errno(
2038 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2039 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2040 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2041 ibuf_free(wbuf);
2042 return err;
2044 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2045 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2046 ibuf_free(wbuf);
2047 return err;
2049 if (imsg_add(wbuf, path, path_len) == -1) {
2050 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2051 ibuf_free(wbuf);
2052 return err;
2055 wbuf->fd = -1;
2056 imsg_close(ibuf, wbuf);
2058 return flush_imsg(ibuf);
2061 const struct got_error *
2062 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2063 size_t ncommits, struct imsgbuf *ibuf)
2065 const struct got_error *err;
2066 struct ibuf *wbuf;
2067 int i;
2069 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2070 sizeof(struct got_imsg_traversed_commits) +
2071 ncommits * SHA1_DIGEST_LENGTH);
2072 if (wbuf == NULL)
2073 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2075 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2076 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2077 ibuf_free(wbuf);
2078 return err;
2080 for (i = 0; i < ncommits; i++) {
2081 struct got_object_id *id = &commit_ids[i];
2082 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2083 err = got_error_from_errno(
2084 "imsg_add TRAVERSED_COMMITS");
2085 ibuf_free(wbuf);
2086 return err;
2090 wbuf->fd = -1;
2091 imsg_close(ibuf, wbuf);
2093 return flush_imsg(ibuf);
2096 const struct got_error *
2097 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2098 struct got_object_id **changed_commit_id,
2099 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2101 const struct got_error *err = NULL;
2102 struct imsg imsg;
2103 struct got_imsg_traversed_commits *icommits;
2104 size_t datalen;
2105 int i, done = 0;
2107 *changed_commit = NULL;
2108 *changed_commit_id = NULL;
2110 while (!done) {
2111 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2112 if (err)
2113 return err;
2115 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2116 switch (imsg.hdr.type) {
2117 case GOT_IMSG_TRAVERSED_COMMITS:
2118 icommits = imsg.data;
2119 if (datalen != sizeof(*icommits) +
2120 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2121 err = got_error(GOT_ERR_PRIVSEP_LEN);
2122 break;
2124 for (i = 0; i < icommits->ncommits; i++) {
2125 struct got_object_qid *qid;
2126 uint8_t *sha1 = (uint8_t *)imsg.data +
2127 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2128 err = got_object_qid_alloc_partial(&qid);
2129 if (err)
2130 break;
2131 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2132 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2134 /* The last commit may contain a change. */
2135 if (i == icommits->ncommits - 1) {
2136 *changed_commit_id =
2137 got_object_id_dup(qid->id);
2138 if (*changed_commit_id == NULL) {
2139 err = got_error_from_errno(
2140 "got_object_id_dup");
2141 break;
2145 break;
2146 case GOT_IMSG_COMMIT:
2147 if (*changed_commit_id == NULL) {
2148 err = got_error(GOT_ERR_PRIVSEP_MSG);
2149 break;
2151 err = get_commit_from_imsg(changed_commit, &imsg,
2152 datalen, ibuf);
2153 break;
2154 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2155 done = 1;
2156 break;
2157 default:
2158 err = got_error(GOT_ERR_PRIVSEP_MSG);
2159 break;
2162 imsg_free(&imsg);
2163 if (err)
2164 break;
2167 if (err)
2168 got_object_id_queue_free(commit_ids);
2169 return err;
2172 const struct got_error *
2173 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2175 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2176 NULL, 0) == -1)
2177 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2179 return flush_imsg(ibuf);
2182 const struct got_error *
2183 got_privsep_unveil_exec_helpers(void)
2185 const char *helpers[] = {
2186 GOT_PATH_PROG_READ_PACK,
2187 GOT_PATH_PROG_READ_OBJECT,
2188 GOT_PATH_PROG_READ_COMMIT,
2189 GOT_PATH_PROG_READ_TREE,
2190 GOT_PATH_PROG_READ_BLOB,
2191 GOT_PATH_PROG_READ_TAG,
2192 GOT_PATH_PROG_READ_GITCONFIG,
2194 int i;
2196 for (i = 0; i < nitems(helpers); i++) {
2197 if (unveil(helpers[i], "x") == 0)
2198 continue;
2199 return got_error_from_errno2("unveil", helpers[i]);
2202 return NULL;
2205 void
2206 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2208 if (close(imsg_fds[0]) != 0) {
2209 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2210 _exit(1);
2213 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2214 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2215 _exit(1);
2217 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2218 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2219 _exit(1);
2222 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2223 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2224 strerror(errno));
2225 _exit(1);