Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/syslimits.h>
22 #include <sys/wait.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <stdint.h>
30 #include <poll.h>
31 #include <imsg.h>
32 #include <sha1.h>
33 #include <zlib.h>
34 #include <time.h>
36 #include "got_object.h"
37 #include "got_error.h"
38 #include "got_path.h"
39 #include "got_repository.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_pack.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 static const struct got_error *
58 poll_fd(int fd, int events, int timeout)
59 {
60 struct pollfd pfd[1];
61 int n;
63 pfd[0].fd = fd;
64 pfd[0].events = events;
66 n = poll(pfd, 1, timeout);
67 if (n == -1)
68 return got_error_from_errno("poll");
69 if (n == 0)
70 return got_error(GOT_ERR_TIMEOUT);
71 if (pfd[0].revents & (POLLERR | POLLNVAL))
72 return got_error_from_errno("poll error");
73 if (pfd[0].revents & (events | POLLHUP))
74 return NULL;
76 return got_error(GOT_ERR_INTERRUPT);
77 }
79 static const struct got_error *
80 read_imsg(struct imsgbuf *ibuf)
81 {
82 const struct got_error *err;
83 size_t n;
85 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
86 if (err)
87 return err;
89 n = imsg_read(ibuf);
90 if (n == -1) {
91 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
92 return got_error(GOT_ERR_PRIVSEP_NO_FD);
93 return got_error(GOT_ERR_PRIVSEP_READ);
94 }
95 if (n == 0)
96 return got_error(GOT_ERR_PRIVSEP_PIPE);
98 return NULL;
99 }
101 const struct got_error *
102 got_privsep_wait_for_child(pid_t pid)
104 int child_status;
106 if (waitpid(pid, &child_status, 0) == -1)
107 return got_error_from_errno("waitpid");
109 if (!WIFEXITED(child_status))
110 return got_error(GOT_ERR_PRIVSEP_DIED);
112 if (WEXITSTATUS(child_status) != 0)
113 return got_error(GOT_ERR_PRIVSEP_EXIT);
115 return NULL;
118 static const struct got_error *
119 recv_imsg_error(struct imsg *imsg, size_t datalen)
121 struct got_imsg_error *ierr;
123 if (datalen != sizeof(*ierr))
124 return got_error(GOT_ERR_PRIVSEP_LEN);
126 ierr = imsg->data;
127 if (ierr->code == GOT_ERR_ERRNO) {
128 static struct got_error serr;
129 serr.code = GOT_ERR_ERRNO;
130 serr.msg = strerror(ierr->errno_code);
131 return &serr;
134 return got_error(ierr->code);
137 const struct got_error *
138 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
139 size_t min_datalen)
141 const struct got_error *err;
142 ssize_t n;
144 n = imsg_get(ibuf, imsg);
145 if (n == -1)
146 return got_error_from_errno("imsg_get");
148 while (n == 0) {
149 err = read_imsg(ibuf);
150 if (err)
151 return err;
152 n = imsg_get(ibuf, imsg);
153 if (n == -1)
154 return got_error_from_errno("imsg_get");
157 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
158 return got_error(GOT_ERR_PRIVSEP_LEN);
160 if (imsg->hdr.type == GOT_IMSG_ERROR) {
161 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 return recv_imsg_error(imsg, datalen);
165 return NULL;
168 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
169 void
170 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
172 const struct got_error *poll_err;
173 struct got_imsg_error ierr;
174 int ret;
176 ierr.code = err->code;
177 if (err->code == GOT_ERR_ERRNO)
178 ierr.errno_code = errno;
179 else
180 ierr.errno_code = 0;
181 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
182 if (ret == -1) {
183 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
184 getprogname(), err->code, err->msg, strerror(errno));
185 return;
188 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
189 if (poll_err) {
190 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
191 getprogname(), err->code, err->msg, poll_err->msg);
192 return;
195 ret = imsg_flush(ibuf);
196 if (ret == -1) {
197 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
198 getprogname(), err->code, err->msg, strerror(errno));
199 return;
203 static const struct got_error *
204 flush_imsg(struct imsgbuf *ibuf)
206 const struct got_error *err;
208 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
209 if (err)
210 return err;
212 if (imsg_flush(ibuf) == -1)
213 return got_error_from_errno("imsg_flush");
215 return NULL;
218 const struct got_error *
219 got_privsep_send_stop(int fd)
221 const struct got_error *err = NULL;
222 struct imsgbuf ibuf;
224 imsg_init(&ibuf, fd);
226 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
227 return got_error_from_errno("imsg_compose STOP");
229 err = flush_imsg(&ibuf);
230 imsg_clear(&ibuf);
231 return err;
234 const struct got_error *
235 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
237 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
238 == -1)
239 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
241 return flush_imsg(ibuf);
244 const struct got_error *
245 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
246 struct got_object_id *id, int pack_idx)
248 const struct got_error *err = NULL;
249 struct got_imsg_packed_object iobj, *iobjp;
250 size_t len;
252 if (id) { /* commit is packed */
253 iobj.idx = pack_idx;
254 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
255 iobjp = &iobj;
256 len = sizeof(iobj);
257 } else {
258 iobjp = NULL;
259 len = 0;
262 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
263 == -1) {
264 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
265 close(fd);
266 return err;
269 return flush_imsg(ibuf);
272 const struct got_error *
273 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
274 struct got_object_id *id, int pack_idx)
276 const struct got_error *err = NULL;
277 struct ibuf *wbuf;
278 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
280 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
281 if (wbuf == NULL)
282 return got_error_from_errno("imsg_create TREE_REQUEST");
284 if (id) { /* tree is packed */
285 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
286 err = got_error_from_errno("imsg_add TREE_ENTRY");
287 ibuf_free(wbuf);
288 return err;
291 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
292 err = got_error_from_errno("imsg_add TREE_ENTRY");
293 ibuf_free(wbuf);
294 return err;
298 wbuf->fd = fd;
299 imsg_close(ibuf, wbuf);
301 return flush_imsg(ibuf);
304 const struct got_error *
305 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
306 struct got_object_id *id, int pack_idx)
308 struct got_imsg_packed_object iobj, *iobjp;
309 size_t len;
311 if (id) { /* tag is packed */
312 iobj.idx = pack_idx;
313 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
314 iobjp = &iobj;
315 len = sizeof(iobj);
316 } else {
317 iobjp = NULL;
318 len = 0;
321 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
322 == -1)
323 return got_error_from_errno("imsg_compose TAG_REQUEST");
325 return flush_imsg(ibuf);
328 const struct got_error *
329 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
330 struct got_object_id *id, int pack_idx)
332 const struct got_error *err = NULL;
333 struct got_imsg_packed_object iobj, *iobjp;
334 size_t len;
336 if (id) { /* blob is packed */
337 iobj.idx = pack_idx;
338 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
339 iobjp = &iobj;
340 len = sizeof(iobj);
341 } else {
342 iobjp = NULL;
343 len = 0;
346 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
347 == -1) {
348 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
349 close(infd);
350 return err;
353 return flush_imsg(ibuf);
356 const struct got_error *
357 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
359 const struct got_error *err = NULL;
361 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
362 == -1) {
363 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
364 close(outfd);
365 return err;
368 return flush_imsg(ibuf);
371 static const struct got_error *
372 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
374 const struct got_error *err = NULL;
376 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
377 err = got_error_from_errno("imsg_compose TMPFD");
378 close(fd);
379 return err;
382 return flush_imsg(ibuf);
385 const struct got_error *
386 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
388 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
391 const struct got_error *
392 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
394 struct got_imsg_object iobj;
396 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
397 iobj.type = obj->type;
398 iobj.flags = obj->flags;
399 iobj.hdrlen = obj->hdrlen;
400 iobj.size = obj->size;
401 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
402 iobj.pack_offset = obj->pack_offset;
403 iobj.pack_idx = obj->pack_idx;
406 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
407 == -1)
408 return got_error_from_errno("imsg_compose OBJECT");
410 return flush_imsg(ibuf);
413 const struct got_error *
414 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
415 struct got_pathlist_head *have_refs, int fetch_all_branches)
417 const struct got_error *err = NULL;
418 struct ibuf *wbuf;
419 size_t len, n_have_refs = 0;
420 struct got_pathlist_entry *pe;
422 len = sizeof(struct got_imsg_fetch_request);
423 TAILQ_FOREACH(pe, have_refs, entry) {
424 len += sizeof(struct got_imsg_fetch_have_ref) + pe->path_len;
425 n_have_refs++;
427 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
428 close(fd);
429 return got_error(GOT_ERR_NO_SPACE);
432 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
433 if (wbuf == NULL) {
434 close(fd);
435 return got_error_from_errno("imsg_create FETCH_REQUEST");
438 /* Keep in sync with struct got_imsg_fetch_request definition! */
439 if (imsg_add(wbuf, &fetch_all_branches, sizeof(fetch_all_branches))
440 == -1) {
441 err = got_error_from_errno("imsg_add FETCH_REQUEST");
442 ibuf_free(wbuf);
443 close(fd);
444 return err;
446 if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
447 err = got_error_from_errno("imsg_add FETCH_REQUEST");
448 ibuf_free(wbuf);
449 close(fd);
450 return err;
453 TAILQ_FOREACH(pe, have_refs, entry) {
454 const char *name = pe->path;
455 size_t name_len = pe->path_len;
456 struct got_object_id *id = pe->data;
458 /* Keep in sync with struct got_imsg_fetch_have_ref! */
459 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
460 err = got_error_from_errno("imsg_add FETCH_REQUEST");
461 ibuf_free(wbuf);
462 close(fd);
463 return err;
465 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
466 err = got_error_from_errno("imsg_add FETCH_REQUEST");
467 ibuf_free(wbuf);
468 close(fd);
469 return err;
471 if (imsg_add(wbuf, name, name_len) == -1) {
472 err = got_error_from_errno("imsg_add FETCH_REQUEST");
473 ibuf_free(wbuf);
474 close(fd);
475 return err;
479 wbuf->fd = fd;
480 imsg_close(ibuf, wbuf);
481 return flush_imsg(ibuf);
484 const struct got_error *
485 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
487 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
491 const struct got_error *
492 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
493 struct got_pathlist_head *symrefs)
495 const struct got_error *err = NULL;
496 struct ibuf *wbuf;
497 size_t len, nsymrefs = 0;
498 struct got_pathlist_entry *pe;
500 len = sizeof(struct got_imsg_fetch_symrefs);
501 TAILQ_FOREACH(pe, symrefs, entry) {
502 const char *target = pe->data;
503 len += sizeof(struct got_imsg_fetch_symref) +
504 pe->path_len + strlen(target);
505 nsymrefs++;
508 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
509 return got_error(GOT_ERR_NO_SPACE);
511 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
512 if (wbuf == NULL)
513 return got_error_from_errno("imsg_create FETCH_SYMREFS");
515 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
516 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
517 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
518 ibuf_free(wbuf);
519 return err;
522 TAILQ_FOREACH(pe, symrefs, entry) {
523 const char *name = pe->path;
524 size_t name_len = pe->path_len;
525 const char *target = pe->data;
526 size_t target_len = strlen(target);
528 /* Keep in sync with struct got_imsg_fetch_symref definition! */
529 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
530 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
531 ibuf_free(wbuf);
532 return err;
534 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
535 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
536 ibuf_free(wbuf);
537 return err;
539 if (imsg_add(wbuf, name, name_len) == -1) {
540 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
541 ibuf_free(wbuf);
542 return err;
544 if (imsg_add(wbuf, target, target_len) == -1) {
545 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
546 ibuf_free(wbuf);
547 return err;
551 wbuf->fd = -1;
552 imsg_close(ibuf, wbuf);
553 return flush_imsg(ibuf);
556 const struct got_error *
557 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
558 struct got_object_id *refid, const char *refname)
560 const struct got_error *err = NULL;
561 struct ibuf *wbuf;
562 size_t len, reflen = strlen(refname);
564 len = sizeof(struct got_imsg_fetch_ref) + reflen;
565 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
566 return got_error(GOT_ERR_NO_SPACE);
568 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
569 if (wbuf == NULL)
570 return got_error_from_errno("imsg_create FETCH_REF");
572 /* Keep in sync with struct got_imsg_fetch_ref definition! */
573 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
574 err = got_error_from_errno("imsg_add FETCH_REF");
575 ibuf_free(wbuf);
576 return err;
578 if (imsg_add(wbuf, refname, reflen) == -1) {
579 err = got_error_from_errno("imsg_add FETCH_REF");
580 ibuf_free(wbuf);
581 return err;
584 wbuf->fd = -1;
585 imsg_close(ibuf, wbuf);
586 return flush_imsg(ibuf);
589 const struct got_error *
590 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
591 size_t msglen)
593 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
594 return got_error(GOT_ERR_NO_SPACE);
596 if (msglen == 0)
597 return NULL;
599 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
600 msg, msglen) == -1)
601 return got_error_from_errno(
602 "imsg_compose FETCH_SERVER_PROGRESS");
604 return flush_imsg(ibuf);
607 const struct got_error *
608 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
610 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
611 &bytes, sizeof(bytes)) == -1)
612 return got_error_from_errno(
613 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
615 return flush_imsg(ibuf);
618 const struct got_error *
619 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
621 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
622 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
623 return got_error_from_errno("imsg_compose FETCH");
624 return flush_imsg(ibuf);
628 const struct got_error *
629 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
630 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
631 off_t *packfile_size, struct imsgbuf *ibuf)
633 const struct got_error *err = NULL;
634 struct imsg imsg;
635 size_t datalen;
636 struct got_imsg_fetch_symrefs *isymrefs = NULL;
637 size_t n, remain;
638 off_t off;
639 int i;
641 *done = 0;
642 *id = NULL;
643 *refname = NULL;
644 *server_progress = NULL;
645 *packfile_size = 0;
647 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
648 if (err)
649 return err;
651 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
652 switch (imsg.hdr.type) {
653 case GOT_IMSG_ERROR:
654 if (datalen < sizeof(struct got_imsg_error)) {
655 err = got_error(GOT_ERR_PRIVSEP_LEN);
656 break;
658 err = recv_imsg_error(&imsg, datalen);
659 break;
660 case GOT_IMSG_FETCH_SYMREFS:
661 if (datalen < sizeof(*isymrefs)) {
662 err = got_error(GOT_ERR_PRIVSEP_LEN);
663 break;
665 if (isymrefs != NULL) {
666 err = got_error(GOT_ERR_PRIVSEP_MSG);
667 break;
669 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
670 off = sizeof(*isymrefs);
671 remain = datalen - off;
672 for (n = 0; n < isymrefs->nsymrefs; n++) {
673 struct got_imsg_fetch_symref *s;
674 char *name, *target;
675 if (remain < sizeof(struct got_imsg_fetch_symref)) {
676 err = got_error(GOT_ERR_PRIVSEP_LEN);
677 goto done;
679 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
680 off += sizeof(*s);
681 remain -= sizeof(*s);
682 if (remain < s->name_len) {
683 err = got_error(GOT_ERR_PRIVSEP_LEN);
684 goto done;
686 name = strndup(imsg.data + off, s->name_len);
687 if (name == NULL) {
688 err = got_error_from_errno("strndup");
689 goto done;
691 off += s->name_len;
692 remain -= s->name_len;
693 if (remain < s->target_len) {
694 err = got_error(GOT_ERR_PRIVSEP_LEN);
695 free(name);
696 goto done;
698 target = strndup(imsg.data + off, s->target_len);
699 if (target == NULL) {
700 err = got_error_from_errno("strndup");
701 free(name);
702 goto done;
704 off += s->target_len;
705 remain -= s->target_len;
706 err = got_pathlist_append(symrefs, name, target);
707 if (err) {
708 free(name);
709 free(target);
710 goto done;
713 break;
714 case GOT_IMSG_FETCH_REF:
715 if (datalen <= SHA1_DIGEST_LENGTH) {
716 err = got_error(GOT_ERR_PRIVSEP_MSG);
717 break;
719 *id = malloc(sizeof(**id));
720 if (*id == NULL) {
721 err = got_error_from_errno("malloc");
722 break;
724 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
725 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
726 datalen - SHA1_DIGEST_LENGTH);
727 if (*refname == NULL) {
728 err = got_error_from_errno("strndup");
729 break;
731 break;
732 case GOT_IMSG_FETCH_SERVER_PROGRESS:
733 if (datalen == 0) {
734 err = got_error(GOT_ERR_PRIVSEP_LEN);
735 break;
737 *server_progress = strndup(imsg.data, datalen);
738 if (*server_progress == NULL) {
739 err = got_error_from_errno("strndup");
740 break;
742 for (i = 0; i < datalen; i++) {
743 if (!isprint((unsigned char)(*server_progress)[i]) &&
744 !isspace((unsigned char)(*server_progress)[i])) {
745 err = got_error(GOT_ERR_PRIVSEP_MSG);
746 free(*server_progress);
747 *server_progress = NULL;
748 goto done;
751 break;
752 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
753 if (datalen < sizeof(*packfile_size)) {
754 err = got_error(GOT_ERR_PRIVSEP_MSG);
755 break;
757 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
758 break;
759 case GOT_IMSG_FETCH_DONE:
760 *id = malloc(sizeof(**id));
761 if (*id == NULL) {
762 err = got_error_from_errno("malloc");
763 break;
765 if (datalen != SHA1_DIGEST_LENGTH) {
766 err = got_error(GOT_ERR_PRIVSEP_MSG);
767 break;
769 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
770 *done = 1;
771 break;
772 default:
773 err = got_error(GOT_ERR_PRIVSEP_MSG);
774 break;
776 done:
777 if (err) {
778 free(*id);
779 *id = NULL;
780 free(*refname);
781 *refname = NULL;
783 imsg_free(&imsg);
784 return err;
787 const struct got_error *
788 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
789 int fd)
791 const struct got_error *err = NULL;
793 /* Keep in sync with struct got_imsg_index_pack_request */
794 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
795 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
796 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
797 close(fd);
798 return err;
800 return flush_imsg(ibuf);
803 const struct got_error *
804 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
806 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
809 const struct got_error *
810 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
811 int nobj_indexed, int nobj_loose, int nobj_resolved)
813 struct got_imsg_index_pack_progress iprogress;
815 iprogress.nobj_total = nobj_total;
816 iprogress.nobj_indexed = nobj_indexed;
817 iprogress.nobj_loose = nobj_loose;
818 iprogress.nobj_resolved = nobj_resolved;
820 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
821 &iprogress, sizeof(iprogress)) == -1)
822 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
824 return flush_imsg(ibuf);
827 const struct got_error *
828 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
830 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
831 return got_error_from_errno("imsg_compose FETCH");
832 return flush_imsg(ibuf);
835 const struct got_error *
836 got_privsep_recv_index_progress(int *done, int *nobj_total,
837 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
838 struct imsgbuf *ibuf)
840 const struct got_error *err = NULL;
841 struct imsg imsg;
842 struct got_imsg_index_pack_progress *iprogress;
843 size_t datalen;
845 *done = 0;
846 *nobj_total = 0;
847 *nobj_indexed = 0;
848 *nobj_resolved = 0;
850 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
851 if (err)
852 return err;
854 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
855 switch (imsg.hdr.type) {
856 case GOT_IMSG_ERROR:
857 if (datalen < sizeof(struct got_imsg_error)) {
858 err = got_error(GOT_ERR_PRIVSEP_LEN);
859 break;
861 err = recv_imsg_error(&imsg, datalen);
862 break;
863 case GOT_IMSG_IDXPACK_PROGRESS:
864 if (datalen < sizeof(*iprogress)) {
865 err = got_error(GOT_ERR_PRIVSEP_LEN);
866 break;
868 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
869 *nobj_total = iprogress->nobj_total;
870 *nobj_indexed = iprogress->nobj_indexed;
871 *nobj_loose = iprogress->nobj_loose;
872 *nobj_resolved = iprogress->nobj_resolved;
873 break;
874 case GOT_IMSG_IDXPACK_DONE:
875 if (datalen != 0) {
876 err = got_error(GOT_ERR_PRIVSEP_LEN);
877 break;
879 *done = 1;
880 break;
881 default:
882 err = got_error(GOT_ERR_PRIVSEP_MSG);
883 break;
886 imsg_free(&imsg);
887 return err;
890 const struct got_error *
891 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
892 struct imsgbuf *ibuf)
894 const struct got_error *err = NULL;
895 struct got_imsg_object *iobj;
896 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
898 if (datalen != sizeof(*iobj))
899 return got_error(GOT_ERR_PRIVSEP_LEN);
900 iobj = imsg->data;
902 *obj = calloc(1, sizeof(**obj));
903 if (*obj == NULL)
904 return got_error_from_errno("calloc");
906 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
907 (*obj)->type = iobj->type;
908 (*obj)->flags = iobj->flags;
909 (*obj)->hdrlen = iobj->hdrlen;
910 (*obj)->size = iobj->size;
911 /* path_packfile is handled by caller */
912 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
913 (*obj)->pack_offset = iobj->pack_offset;
914 (*obj)->pack_idx = iobj->pack_idx;
917 return err;
920 const struct got_error *
921 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
923 const struct got_error *err = NULL;
924 struct imsg imsg;
925 const size_t min_datalen =
926 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
928 *obj = NULL;
930 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
931 if (err)
932 return err;
934 switch (imsg.hdr.type) {
935 case GOT_IMSG_OBJECT:
936 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
937 break;
938 default:
939 err = got_error(GOT_ERR_PRIVSEP_MSG);
940 break;
943 imsg_free(&imsg);
945 return err;
948 static const struct got_error *
949 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
950 size_t logmsg_len)
952 const struct got_error *err = NULL;
953 size_t offset, remain;
955 offset = 0;
956 remain = logmsg_len;
957 while (remain > 0) {
958 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
960 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
961 commit->logmsg + offset, n) == -1) {
962 err = got_error_from_errno("imsg_compose "
963 "COMMIT_LOGMSG");
964 break;
967 err = flush_imsg(ibuf);
968 if (err)
969 break;
971 offset += n;
972 remain -= n;
975 return err;
978 const struct got_error *
979 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
981 const struct got_error *err = NULL;
982 struct got_imsg_commit_object *icommit;
983 uint8_t *buf;
984 size_t len, total;
985 struct got_object_qid *qid;
986 size_t author_len = strlen(commit->author);
987 size_t committer_len = strlen(commit->committer);
988 size_t logmsg_len = strlen(commit->logmsg);
990 total = sizeof(*icommit) + author_len + committer_len +
991 commit->nparents * SHA1_DIGEST_LENGTH;
993 buf = malloc(total);
994 if (buf == NULL)
995 return got_error_from_errno("malloc");
997 icommit = (struct got_imsg_commit_object *)buf;
998 memcpy(icommit->tree_id, commit->tree_id->sha1,
999 sizeof(icommit->tree_id));
1000 icommit->author_len = author_len;
1001 icommit->author_time = commit->author_time;
1002 icommit->author_gmtoff = commit->author_gmtoff;
1003 icommit->committer_len = committer_len;
1004 icommit->committer_time = commit->committer_time;
1005 icommit->committer_gmtoff = commit->committer_gmtoff;
1006 icommit->logmsg_len = logmsg_len;
1007 icommit->nparents = commit->nparents;
1009 len = sizeof(*icommit);
1010 memcpy(buf + len, commit->author, author_len);
1011 len += author_len;
1012 memcpy(buf + len, commit->committer, committer_len);
1013 len += committer_len;
1014 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1015 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1016 len += SHA1_DIGEST_LENGTH;
1019 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1020 err = got_error_from_errno("imsg_compose COMMIT");
1021 goto done;
1024 if (logmsg_len == 0 ||
1025 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1026 err = flush_imsg(ibuf);
1027 if (err)
1028 goto done;
1030 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1031 done:
1032 free(buf);
1033 return err;
1036 static const struct got_error *
1037 get_commit_from_imsg(struct got_commit_object **commit,
1038 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1040 const struct got_error *err = NULL;
1041 struct got_imsg_commit_object *icommit;
1042 size_t len = 0;
1043 int i;
1045 if (datalen < sizeof(*icommit))
1046 return got_error(GOT_ERR_PRIVSEP_LEN);
1048 icommit = imsg->data;
1049 if (datalen != sizeof(*icommit) + icommit->author_len +
1050 icommit->committer_len +
1051 icommit->nparents * SHA1_DIGEST_LENGTH)
1052 return got_error(GOT_ERR_PRIVSEP_LEN);
1054 if (icommit->nparents < 0)
1055 return got_error(GOT_ERR_PRIVSEP_LEN);
1057 len += sizeof(*icommit);
1059 *commit = got_object_commit_alloc_partial();
1060 if (*commit == NULL)
1061 return got_error_from_errno(
1062 "got_object_commit_alloc_partial");
1064 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1065 SHA1_DIGEST_LENGTH);
1066 (*commit)->author_time = icommit->author_time;
1067 (*commit)->author_gmtoff = icommit->author_gmtoff;
1068 (*commit)->committer_time = icommit->committer_time;
1069 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1071 if (icommit->author_len == 0) {
1072 (*commit)->author = strdup("");
1073 if ((*commit)->author == NULL) {
1074 err = got_error_from_errno("strdup");
1075 goto done;
1077 } else {
1078 (*commit)->author = malloc(icommit->author_len + 1);
1079 if ((*commit)->author == NULL) {
1080 err = got_error_from_errno("malloc");
1081 goto done;
1083 memcpy((*commit)->author, imsg->data + len,
1084 icommit->author_len);
1085 (*commit)->author[icommit->author_len] = '\0';
1087 len += icommit->author_len;
1089 if (icommit->committer_len == 0) {
1090 (*commit)->committer = strdup("");
1091 if ((*commit)->committer == NULL) {
1092 err = got_error_from_errno("strdup");
1093 goto done;
1095 } else {
1096 (*commit)->committer =
1097 malloc(icommit->committer_len + 1);
1098 if ((*commit)->committer == NULL) {
1099 err = got_error_from_errno("malloc");
1100 goto done;
1102 memcpy((*commit)->committer, imsg->data + len,
1103 icommit->committer_len);
1104 (*commit)->committer[icommit->committer_len] = '\0';
1106 len += icommit->committer_len;
1108 if (icommit->logmsg_len == 0) {
1109 (*commit)->logmsg = strdup("");
1110 if ((*commit)->logmsg == NULL) {
1111 err = got_error_from_errno("strdup");
1112 goto done;
1114 } else {
1115 size_t offset = 0, remain = icommit->logmsg_len;
1117 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1118 if ((*commit)->logmsg == NULL) {
1119 err = got_error_from_errno("malloc");
1120 goto done;
1122 while (remain > 0) {
1123 struct imsg imsg_log;
1124 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1125 remain);
1127 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1128 if (err)
1129 goto done;
1131 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1132 err = got_error(GOT_ERR_PRIVSEP_MSG);
1133 goto done;
1136 memcpy((*commit)->logmsg + offset,
1137 imsg_log.data, n);
1138 imsg_free(&imsg_log);
1139 offset += n;
1140 remain -= n;
1142 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1145 for (i = 0; i < icommit->nparents; i++) {
1146 struct got_object_qid *qid;
1148 err = got_object_qid_alloc_partial(&qid);
1149 if (err)
1150 break;
1151 memcpy(qid->id, imsg->data + len +
1152 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1153 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1154 (*commit)->nparents++;
1156 done:
1157 if (err) {
1158 got_object_commit_close(*commit);
1159 *commit = NULL;
1161 return err;
1164 const struct got_error *
1165 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1167 const struct got_error *err = NULL;
1168 struct imsg imsg;
1169 size_t datalen;
1170 const size_t min_datalen =
1171 MIN(sizeof(struct got_imsg_error),
1172 sizeof(struct got_imsg_commit_object));
1174 *commit = NULL;
1176 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1177 if (err)
1178 return err;
1180 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1182 switch (imsg.hdr.type) {
1183 case GOT_IMSG_COMMIT:
1184 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1185 break;
1186 default:
1187 err = got_error(GOT_ERR_PRIVSEP_MSG);
1188 break;
1191 imsg_free(&imsg);
1193 return err;
1196 const struct got_error *
1197 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1198 int nentries)
1200 const struct got_error *err = NULL;
1201 struct got_imsg_tree_object itree;
1202 struct got_pathlist_entry *pe;
1203 size_t totlen;
1204 int nimsg; /* number of imsg queued in ibuf */
1206 itree.nentries = nentries;
1207 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1208 == -1)
1209 return got_error_from_errno("imsg_compose TREE");
1211 totlen = sizeof(itree);
1212 nimsg = 1;
1213 TAILQ_FOREACH(pe, entries, entry) {
1214 const char *name = pe->path;
1215 struct got_parsed_tree_entry *pte = pe->data;
1216 struct ibuf *wbuf;
1217 size_t namelen = strlen(name);
1218 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1220 if (len > MAX_IMSGSIZE)
1221 return got_error(GOT_ERR_NO_SPACE);
1223 nimsg++;
1224 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1225 err = flush_imsg(ibuf);
1226 if (err)
1227 return err;
1228 nimsg = 0;
1231 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1232 if (wbuf == NULL)
1233 return got_error_from_errno("imsg_create TREE_ENTRY");
1235 /* Keep in sync with struct got_imsg_tree_object definition! */
1236 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1237 err = got_error_from_errno("imsg_add TREE_ENTRY");
1238 ibuf_free(wbuf);
1239 return err;
1241 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1242 err = got_error_from_errno("imsg_add TREE_ENTRY");
1243 ibuf_free(wbuf);
1244 return err;
1247 if (imsg_add(wbuf, name, namelen) == -1) {
1248 err = got_error_from_errno("imsg_add TREE_ENTRY");
1249 ibuf_free(wbuf);
1250 return err;
1253 wbuf->fd = -1;
1254 imsg_close(ibuf, wbuf);
1256 totlen += len;
1259 return flush_imsg(ibuf);
1262 const struct got_error *
1263 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1265 const struct got_error *err = NULL;
1266 const size_t min_datalen =
1267 MIN(sizeof(struct got_imsg_error),
1268 sizeof(struct got_imsg_tree_object));
1269 struct got_imsg_tree_object *itree;
1270 int nentries = 0;
1272 *tree = NULL;
1273 get_more:
1274 err = read_imsg(ibuf);
1275 if (err)
1276 goto done;
1278 for (;;) {
1279 struct imsg imsg;
1280 size_t n;
1281 size_t datalen;
1282 struct got_imsg_tree_entry *ite;
1283 struct got_tree_entry *te = NULL;
1285 n = imsg_get(ibuf, &imsg);
1286 if (n == 0) {
1287 if (*tree && (*tree)->nentries != nentries)
1288 goto get_more;
1289 break;
1292 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1293 return got_error(GOT_ERR_PRIVSEP_LEN);
1295 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1297 switch (imsg.hdr.type) {
1298 case GOT_IMSG_ERROR:
1299 err = recv_imsg_error(&imsg, datalen);
1300 break;
1301 case GOT_IMSG_TREE:
1302 /* This message should only appear once. */
1303 if (*tree != NULL) {
1304 err = got_error(GOT_ERR_PRIVSEP_MSG);
1305 break;
1307 if (datalen != sizeof(*itree)) {
1308 err = got_error(GOT_ERR_PRIVSEP_LEN);
1309 break;
1311 itree = imsg.data;
1312 *tree = malloc(sizeof(**tree));
1313 if (*tree == NULL) {
1314 err = got_error_from_errno("malloc");
1315 break;
1317 (*tree)->entries = calloc(itree->nentries,
1318 sizeof(struct got_tree_entry));
1319 if ((*tree)->entries == NULL) {
1320 err = got_error_from_errno("malloc");
1321 break;
1323 (*tree)->nentries = itree->nentries;
1324 (*tree)->refcnt = 0;
1325 break;
1326 case GOT_IMSG_TREE_ENTRY:
1327 /* This message should be preceeded by GOT_IMSG_TREE. */
1328 if (*tree == NULL) {
1329 err = got_error(GOT_ERR_PRIVSEP_MSG);
1330 break;
1332 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1333 err = got_error(GOT_ERR_PRIVSEP_LEN);
1334 break;
1337 /* Remaining data contains the entry's name. */
1338 datalen -= sizeof(*ite);
1339 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1340 err = got_error(GOT_ERR_PRIVSEP_LEN);
1341 break;
1343 ite = imsg.data;
1345 if (datalen + 1 > sizeof(te->name)) {
1346 err = got_error(GOT_ERR_NO_SPACE);
1347 break;
1349 te = &(*tree)->entries[nentries];
1350 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1351 te->name[datalen] = '\0';
1353 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1354 te->mode = ite->mode;
1355 te->idx = nentries;
1356 nentries++;
1357 break;
1358 default:
1359 err = got_error(GOT_ERR_PRIVSEP_MSG);
1360 break;
1363 imsg_free(&imsg);
1365 done:
1366 if (*tree && (*tree)->nentries != nentries) {
1367 if (err == NULL)
1368 err = got_error(GOT_ERR_PRIVSEP_LEN);
1369 got_object_tree_close(*tree);
1370 *tree = NULL;
1373 return err;
1376 const struct got_error *
1377 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1378 const uint8_t *data)
1380 struct got_imsg_blob iblob;
1382 iblob.size = size;
1383 iblob.hdrlen = hdrlen;
1385 if (data) {
1386 uint8_t *buf;
1388 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1389 return got_error(GOT_ERR_NO_SPACE);
1391 buf = malloc(sizeof(iblob) + size);
1392 if (buf == NULL)
1393 return got_error_from_errno("malloc");
1395 memcpy(buf, &iblob, sizeof(iblob));
1396 memcpy(buf + sizeof(iblob), data, size);
1397 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1398 sizeof(iblob) + size) == -1) {
1399 free(buf);
1400 return got_error_from_errno("imsg_compose BLOB");
1402 free(buf);
1403 } else {
1404 /* Data has already been written to file descriptor. */
1405 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1406 sizeof(iblob)) == -1)
1407 return got_error_from_errno("imsg_compose BLOB");
1411 return flush_imsg(ibuf);
1414 const struct got_error *
1415 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1416 struct imsgbuf *ibuf)
1418 const struct got_error *err = NULL;
1419 struct imsg imsg;
1420 struct got_imsg_blob *iblob;
1421 size_t datalen;
1423 *outbuf = NULL;
1425 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1426 if (err)
1427 return err;
1429 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1431 switch (imsg.hdr.type) {
1432 case GOT_IMSG_BLOB:
1433 if (datalen < sizeof(*iblob)) {
1434 err = got_error(GOT_ERR_PRIVSEP_LEN);
1435 break;
1437 iblob = imsg.data;
1438 *size = iblob->size;
1439 *hdrlen = iblob->hdrlen;
1441 if (datalen == sizeof(*iblob)) {
1442 /* Data has been written to file descriptor. */
1443 break;
1446 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1447 err = got_error(GOT_ERR_PRIVSEP_LEN);
1448 break;
1451 *outbuf = malloc(*size);
1452 if (*outbuf == NULL) {
1453 err = got_error_from_errno("malloc");
1454 break;
1456 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1457 break;
1458 default:
1459 err = got_error(GOT_ERR_PRIVSEP_MSG);
1460 break;
1463 imsg_free(&imsg);
1465 return err;
1468 static const struct got_error *
1469 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1471 const struct got_error *err = NULL;
1472 size_t offset, remain;
1474 offset = 0;
1475 remain = tagmsg_len;
1476 while (remain > 0) {
1477 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1479 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1480 tag->tagmsg + offset, n) == -1) {
1481 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1482 break;
1485 err = flush_imsg(ibuf);
1486 if (err)
1487 break;
1489 offset += n;
1490 remain -= n;
1493 return err;
1496 const struct got_error *
1497 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1499 const struct got_error *err = NULL;
1500 struct got_imsg_tag_object *itag;
1501 uint8_t *buf;
1502 size_t len, total;
1503 size_t tag_len = strlen(tag->tag);
1504 size_t tagger_len = strlen(tag->tagger);
1505 size_t tagmsg_len = strlen(tag->tagmsg);
1507 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1509 buf = malloc(total);
1510 if (buf == NULL)
1511 return got_error_from_errno("malloc");
1513 itag = (struct got_imsg_tag_object *)buf;
1514 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1515 itag->obj_type = tag->obj_type;
1516 itag->tag_len = tag_len;
1517 itag->tagger_len = tagger_len;
1518 itag->tagger_time = tag->tagger_time;
1519 itag->tagger_gmtoff = tag->tagger_gmtoff;
1520 itag->tagmsg_len = tagmsg_len;
1522 len = sizeof(*itag);
1523 memcpy(buf + len, tag->tag, tag_len);
1524 len += tag_len;
1525 memcpy(buf + len, tag->tagger, tagger_len);
1526 len += tagger_len;
1528 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1529 err = got_error_from_errno("imsg_compose TAG");
1530 goto done;
1533 if (tagmsg_len == 0 ||
1534 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1535 err = flush_imsg(ibuf);
1536 if (err)
1537 goto done;
1539 err = send_tagmsg(ibuf, tag, tagmsg_len);
1540 done:
1541 free(buf);
1542 return err;
1545 const struct got_error *
1546 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1548 const struct got_error *err = NULL;
1549 struct imsg imsg;
1550 struct got_imsg_tag_object *itag;
1551 size_t len, datalen;
1552 const size_t min_datalen =
1553 MIN(sizeof(struct got_imsg_error),
1554 sizeof(struct got_imsg_tag_object));
1556 *tag = NULL;
1558 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1559 if (err)
1560 return err;
1562 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1563 len = 0;
1565 switch (imsg.hdr.type) {
1566 case GOT_IMSG_TAG:
1567 if (datalen < sizeof(*itag)) {
1568 err = got_error(GOT_ERR_PRIVSEP_LEN);
1569 break;
1571 itag = imsg.data;
1572 if (datalen != sizeof(*itag) + itag->tag_len +
1573 itag->tagger_len) {
1574 err = got_error(GOT_ERR_PRIVSEP_LEN);
1575 break;
1577 len += sizeof(*itag);
1579 *tag = calloc(1, sizeof(**tag));
1580 if (*tag == NULL) {
1581 err = got_error_from_errno("calloc");
1582 break;
1585 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1587 if (itag->tag_len == 0) {
1588 (*tag)->tag = strdup("");
1589 if ((*tag)->tag == NULL) {
1590 err = got_error_from_errno("strdup");
1591 break;
1593 } else {
1594 (*tag)->tag = malloc(itag->tag_len + 1);
1595 if ((*tag)->tag == NULL) {
1596 err = got_error_from_errno("malloc");
1597 break;
1599 memcpy((*tag)->tag, imsg.data + len,
1600 itag->tag_len);
1601 (*tag)->tag[itag->tag_len] = '\0';
1603 len += itag->tag_len;
1605 (*tag)->obj_type = itag->obj_type;
1606 (*tag)->tagger_time = itag->tagger_time;
1607 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1609 if (itag->tagger_len == 0) {
1610 (*tag)->tagger = strdup("");
1611 if ((*tag)->tagger == NULL) {
1612 err = got_error_from_errno("strdup");
1613 break;
1615 } else {
1616 (*tag)->tagger = malloc(itag->tagger_len + 1);
1617 if ((*tag)->tagger == NULL) {
1618 err = got_error_from_errno("malloc");
1619 break;
1621 memcpy((*tag)->tagger, imsg.data + len,
1622 itag->tagger_len);
1623 (*tag)->tagger[itag->tagger_len] = '\0';
1625 len += itag->tagger_len;
1627 if (itag->tagmsg_len == 0) {
1628 (*tag)->tagmsg = strdup("");
1629 if ((*tag)->tagmsg == NULL) {
1630 err = got_error_from_errno("strdup");
1631 break;
1633 } else {
1634 size_t offset = 0, remain = itag->tagmsg_len;
1636 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1637 if ((*tag)->tagmsg == NULL) {
1638 err = got_error_from_errno("malloc");
1639 break;
1641 while (remain > 0) {
1642 struct imsg imsg_log;
1643 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1644 remain);
1646 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1647 if (err)
1648 return err;
1650 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1651 return got_error(GOT_ERR_PRIVSEP_MSG);
1653 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1654 n);
1655 imsg_free(&imsg_log);
1656 offset += n;
1657 remain -= n;
1659 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1662 break;
1663 default:
1664 err = got_error(GOT_ERR_PRIVSEP_MSG);
1665 break;
1668 imsg_free(&imsg);
1670 return err;
1673 const struct got_error *
1674 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1675 struct got_packidx *packidx)
1677 const struct got_error *err = NULL;
1678 struct got_imsg_packidx ipackidx;
1679 struct got_imsg_pack ipack;
1680 int fd;
1682 ipackidx.len = packidx->len;
1683 fd = dup(packidx->fd);
1684 if (fd == -1)
1685 return got_error_from_errno("dup");
1687 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1688 sizeof(ipackidx)) == -1) {
1689 err = got_error_from_errno("imsg_compose PACKIDX");
1690 close(fd);
1691 return err;
1694 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1695 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1696 return got_error(GOT_ERR_NO_SPACE);
1697 ipack.filesize = pack->filesize;
1699 fd = dup(pack->fd);
1700 if (fd == -1)
1701 return got_error_from_errno("dup");
1703 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1704 == -1) {
1705 err = got_error_from_errno("imsg_compose PACK");
1706 close(fd);
1707 return err;
1710 return flush_imsg(ibuf);
1713 const struct got_error *
1714 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1715 struct got_object_id *id)
1717 struct got_imsg_packed_object iobj;
1719 iobj.idx = idx;
1720 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1722 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1723 &iobj, sizeof(iobj)) == -1)
1724 return got_error_from_errno("imsg_compose "
1725 "PACKED_OBJECT_REQUEST");
1727 return flush_imsg(ibuf);
1730 const struct got_error *
1731 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1733 const struct got_error *err = NULL;
1735 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1736 NULL, 0) == -1) {
1737 err = got_error_from_errno("imsg_compose "
1738 "GITCONFIG_PARSE_REQUEST");
1739 close(fd);
1740 return err;
1743 return flush_imsg(ibuf);
1746 const struct got_error *
1747 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1749 if (imsg_compose(ibuf,
1750 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1751 NULL, 0) == -1)
1752 return got_error_from_errno("imsg_compose "
1753 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1755 return flush_imsg(ibuf);
1758 const struct got_error *
1759 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1761 if (imsg_compose(ibuf,
1762 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1763 return got_error_from_errno("imsg_compose "
1764 "GITCONFIG_AUTHOR_NAME_REQUEST");
1766 return flush_imsg(ibuf);
1769 const struct got_error *
1770 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1772 if (imsg_compose(ibuf,
1773 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1774 return got_error_from_errno("imsg_compose "
1775 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1777 return flush_imsg(ibuf);
1780 const struct got_error *
1781 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1783 if (imsg_compose(ibuf,
1784 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1785 return got_error_from_errno("imsg_compose "
1786 "GITCONFIG_REMOTE_REQUEST");
1788 return flush_imsg(ibuf);
1791 const struct got_error *
1792 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1794 if (imsg_compose(ibuf,
1795 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1796 return got_error_from_errno("imsg_compose "
1797 "GITCONFIG_OWNER_REQUEST");
1799 return flush_imsg(ibuf);
1802 const struct got_error *
1803 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1805 size_t len = value ? strlen(value) + 1 : 0;
1807 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1808 value, len) == -1)
1809 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1811 return flush_imsg(ibuf);
1814 const struct got_error *
1815 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1817 const struct got_error *err = NULL;
1818 struct imsg imsg;
1819 size_t datalen;
1820 const size_t min_datalen = 0;
1822 *str = NULL;
1824 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1825 if (err)
1826 return err;
1827 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1829 switch (imsg.hdr.type) {
1830 case GOT_IMSG_GITCONFIG_STR_VAL:
1831 if (datalen == 0)
1832 break;
1833 *str = malloc(datalen);
1834 if (*str == NULL) {
1835 err = got_error_from_errno("malloc");
1836 break;
1838 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1839 err = got_error(GOT_ERR_NO_SPACE);
1840 break;
1841 default:
1842 err = got_error(GOT_ERR_PRIVSEP_MSG);
1843 break;
1846 imsg_free(&imsg);
1847 return err;
1850 const struct got_error *
1851 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1853 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1854 &value, sizeof(value)) == -1)
1855 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1857 return flush_imsg(ibuf);
1860 const struct got_error *
1861 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1863 const struct got_error *err = NULL;
1864 struct imsg imsg;
1865 size_t datalen;
1866 const size_t min_datalen =
1867 MIN(sizeof(struct got_imsg_error), sizeof(int));
1869 *val = 0;
1871 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1872 if (err)
1873 return err;
1874 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1876 switch (imsg.hdr.type) {
1877 case GOT_IMSG_GITCONFIG_INT_VAL:
1878 if (datalen != sizeof(*val)) {
1879 err = got_error(GOT_ERR_PRIVSEP_LEN);
1880 break;
1882 memcpy(val, imsg.data, sizeof(*val));
1883 break;
1884 default:
1885 err = got_error(GOT_ERR_PRIVSEP_MSG);
1886 break;
1889 imsg_free(&imsg);
1890 return err;
1893 const struct got_error *
1894 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1895 struct got_remote_repo *remotes, int nremotes)
1897 const struct got_error *err = NULL;
1898 struct got_imsg_remotes iremotes;
1899 int i;
1901 iremotes.nremotes = nremotes;
1902 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1903 &iremotes, sizeof(iremotes)) == -1)
1904 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1906 err = flush_imsg(ibuf);
1907 imsg_clear(ibuf);
1908 if (err)
1909 return err;
1911 for (i = 0; i < nremotes; i++) {
1912 struct got_imsg_remote iremote;
1913 size_t len = sizeof(iremote);
1914 struct ibuf *wbuf;
1916 iremote.mirror_references = remotes[i].mirror_references;
1917 iremote.name_len = strlen(remotes[i].name);
1918 len += iremote.name_len;
1919 iremote.url_len = strlen(remotes[i].url);
1920 len += iremote.url_len;
1922 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1923 if (wbuf == NULL)
1924 return got_error_from_errno(
1925 "imsg_create GITCONFIG_REMOTE");
1927 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1928 err = got_error_from_errno(
1929 "imsg_add GITCONFIG_REMOTE");
1930 ibuf_free(wbuf);
1931 return err;
1934 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1935 err = got_error_from_errno(
1936 "imsg_add GITCONFIG_REMOTE");
1937 ibuf_free(wbuf);
1938 return err;
1940 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1941 err = got_error_from_errno(
1942 "imsg_add GITCONFIG_REMOTE");
1943 ibuf_free(wbuf);
1944 return err;
1947 wbuf->fd = -1;
1948 imsg_close(ibuf, wbuf);
1949 err = flush_imsg(ibuf);
1950 if (err)
1951 return err;
1954 return NULL;
1957 const struct got_error *
1958 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1959 int *nremotes, struct imsgbuf *ibuf)
1961 const struct got_error *err = NULL;
1962 struct imsg imsg;
1963 size_t datalen;
1964 struct got_imsg_remotes iremotes;
1965 struct got_imsg_remote iremote;
1967 *remotes = NULL;
1968 *nremotes = 0;
1969 iremotes.nremotes = 0;
1971 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1972 if (err)
1973 return err;
1974 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1976 switch (imsg.hdr.type) {
1977 case GOT_IMSG_GITCONFIG_REMOTES:
1978 if (datalen != sizeof(iremotes)) {
1979 err = got_error(GOT_ERR_PRIVSEP_LEN);
1980 break;
1982 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1983 if (iremotes.nremotes == 0) {
1984 imsg_free(&imsg);
1985 return NULL;
1987 break;
1988 default:
1989 imsg_free(&imsg);
1990 return got_error(GOT_ERR_PRIVSEP_MSG);
1993 imsg_free(&imsg);
1995 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
1996 if (*remotes == NULL)
1997 return got_error_from_errno("recallocarray");
1999 while (*nremotes < iremotes.nremotes) {
2000 struct got_remote_repo *remote;
2002 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2003 if (err)
2004 break;
2005 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2007 switch (imsg.hdr.type) {
2008 case GOT_IMSG_GITCONFIG_REMOTE:
2009 remote = &(*remotes)[*nremotes];
2010 if (datalen < sizeof(iremote)) {
2011 err = got_error(GOT_ERR_PRIVSEP_LEN);
2012 break;
2014 memcpy(&iremote, imsg.data, sizeof(iremote));
2015 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2016 (sizeof(iremote) + iremote.name_len +
2017 iremote.url_len) > datalen) {
2018 err = got_error(GOT_ERR_PRIVSEP_LEN);
2019 break;
2021 remote->name = strndup(imsg.data + sizeof(iremote),
2022 iremote.name_len);
2023 if (remote->name == NULL) {
2024 err = got_error_from_errno("strndup");
2025 break;
2027 remote->url = strndup(imsg.data + sizeof(iremote) +
2028 iremote.name_len, iremote.url_len);
2029 if (remote->url == NULL) {
2030 err = got_error_from_errno("strndup");
2031 free(remote->name);
2032 break;
2034 remote->mirror_references = iremote.mirror_references;
2035 (*nremotes)++;
2036 break;
2037 default:
2038 err = got_error(GOT_ERR_PRIVSEP_MSG);
2039 break;
2042 imsg_free(&imsg);
2043 if (err)
2044 break;
2047 if (err) {
2048 int i;
2049 for (i = 0; i < *nremotes; i++) {
2050 free((*remotes)[i].name);
2051 free((*remotes)[i].url);
2053 free(*remotes);
2054 *remotes = NULL;
2055 *nremotes = 0;
2057 return err;
2060 const struct got_error *
2061 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2062 struct got_object_id *id, int idx, const char *path)
2064 const struct got_error *err = NULL;
2065 struct ibuf *wbuf;
2066 size_t path_len = strlen(path) + 1;
2068 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2069 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2070 if (wbuf == NULL)
2071 return got_error_from_errno(
2072 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2073 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2074 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2075 ibuf_free(wbuf);
2076 return err;
2078 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2079 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2080 ibuf_free(wbuf);
2081 return err;
2083 if (imsg_add(wbuf, path, path_len) == -1) {
2084 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2085 ibuf_free(wbuf);
2086 return err;
2089 wbuf->fd = -1;
2090 imsg_close(ibuf, wbuf);
2092 return flush_imsg(ibuf);
2095 const struct got_error *
2096 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2097 size_t ncommits, struct imsgbuf *ibuf)
2099 const struct got_error *err;
2100 struct ibuf *wbuf;
2101 int i;
2103 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2104 sizeof(struct got_imsg_traversed_commits) +
2105 ncommits * SHA1_DIGEST_LENGTH);
2106 if (wbuf == NULL)
2107 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2109 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2110 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2111 ibuf_free(wbuf);
2112 return err;
2114 for (i = 0; i < ncommits; i++) {
2115 struct got_object_id *id = &commit_ids[i];
2116 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2117 err = got_error_from_errno(
2118 "imsg_add TRAVERSED_COMMITS");
2119 ibuf_free(wbuf);
2120 return err;
2124 wbuf->fd = -1;
2125 imsg_close(ibuf, wbuf);
2127 return flush_imsg(ibuf);
2130 const struct got_error *
2131 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2132 struct got_object_id **changed_commit_id,
2133 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2135 const struct got_error *err = NULL;
2136 struct imsg imsg;
2137 struct got_imsg_traversed_commits *icommits;
2138 size_t datalen;
2139 int i, done = 0;
2141 *changed_commit = NULL;
2142 *changed_commit_id = NULL;
2144 while (!done) {
2145 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2146 if (err)
2147 return err;
2149 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2150 switch (imsg.hdr.type) {
2151 case GOT_IMSG_TRAVERSED_COMMITS:
2152 icommits = imsg.data;
2153 if (datalen != sizeof(*icommits) +
2154 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2155 err = got_error(GOT_ERR_PRIVSEP_LEN);
2156 break;
2158 for (i = 0; i < icommits->ncommits; i++) {
2159 struct got_object_qid *qid;
2160 uint8_t *sha1 = (uint8_t *)imsg.data +
2161 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2162 err = got_object_qid_alloc_partial(&qid);
2163 if (err)
2164 break;
2165 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2166 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2168 /* The last commit may contain a change. */
2169 if (i == icommits->ncommits - 1) {
2170 *changed_commit_id =
2171 got_object_id_dup(qid->id);
2172 if (*changed_commit_id == NULL) {
2173 err = got_error_from_errno(
2174 "got_object_id_dup");
2175 break;
2179 break;
2180 case GOT_IMSG_COMMIT:
2181 if (*changed_commit_id == NULL) {
2182 err = got_error(GOT_ERR_PRIVSEP_MSG);
2183 break;
2185 err = get_commit_from_imsg(changed_commit, &imsg,
2186 datalen, ibuf);
2187 break;
2188 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2189 done = 1;
2190 break;
2191 default:
2192 err = got_error(GOT_ERR_PRIVSEP_MSG);
2193 break;
2196 imsg_free(&imsg);
2197 if (err)
2198 break;
2201 if (err)
2202 got_object_id_queue_free(commit_ids);
2203 return err;
2206 const struct got_error *
2207 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2209 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2210 NULL, 0) == -1)
2211 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2213 return flush_imsg(ibuf);
2216 const struct got_error *
2217 got_privsep_unveil_exec_helpers(void)
2219 const char *helpers[] = {
2220 GOT_PATH_PROG_READ_PACK,
2221 GOT_PATH_PROG_READ_OBJECT,
2222 GOT_PATH_PROG_READ_COMMIT,
2223 GOT_PATH_PROG_READ_TREE,
2224 GOT_PATH_PROG_READ_BLOB,
2225 GOT_PATH_PROG_READ_TAG,
2226 GOT_PATH_PROG_READ_GITCONFIG,
2227 GOT_PATH_PROG_FETCH_PACK,
2228 GOT_PATH_PROG_INDEX_PACK,
2230 int i;
2232 for (i = 0; i < nitems(helpers); i++) {
2233 if (unveil(helpers[i], "x") == 0)
2234 continue;
2235 return got_error_from_errno2("unveil", helpers[i]);
2238 return NULL;
2241 void
2242 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2244 if (close(imsg_fds[0]) != 0) {
2245 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2246 _exit(1);
2249 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2250 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2251 _exit(1);
2253 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2254 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2255 _exit(1);
2258 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2259 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2260 strerror(errno));
2261 _exit(1);