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)
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_symrefs);
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_have_refs definition! */
439 if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
440 err = got_error_from_errno("imsg_add FETCH_REQUEST");
441 ibuf_free(wbuf);
442 close(fd);
443 return err;
446 TAILQ_FOREACH(pe, have_refs, entry) {
447 const char *name = pe->path;
448 size_t name_len = pe->path_len;
449 struct got_object_id *id = pe->data;
451 /* Keep in sync with struct got_imsg_fetch_have_ref! */
452 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
453 err = got_error_from_errno("imsg_add FETCH_REQUEST");
454 ibuf_free(wbuf);
455 close(fd);
456 return err;
458 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
459 err = got_error_from_errno("imsg_add FETCH_REQUEST");
460 ibuf_free(wbuf);
461 close(fd);
462 return err;
464 if (imsg_add(wbuf, name, name_len) == -1) {
465 err = got_error_from_errno("imsg_add FETCH_REQUEST");
466 ibuf_free(wbuf);
467 close(fd);
468 return err;
472 wbuf->fd = fd;
473 imsg_close(ibuf, wbuf);
474 return flush_imsg(ibuf);
477 const struct got_error *
478 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
480 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
484 const struct got_error *
485 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
486 struct got_pathlist_head *symrefs)
488 const struct got_error *err = NULL;
489 struct ibuf *wbuf;
490 size_t len, nsymrefs = 0;
491 struct got_pathlist_entry *pe;
493 len = sizeof(struct got_imsg_fetch_symrefs);
494 TAILQ_FOREACH(pe, symrefs, entry) {
495 const char *target = pe->data;
496 len += sizeof(struct got_imsg_fetch_symref) +
497 pe->path_len + strlen(target);
498 nsymrefs++;
501 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
502 return got_error(GOT_ERR_NO_SPACE);
504 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
505 if (wbuf == NULL)
506 return got_error_from_errno("imsg_create FETCH_SYMREFS");
508 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
509 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
510 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
511 ibuf_free(wbuf);
512 return err;
515 TAILQ_FOREACH(pe, symrefs, entry) {
516 const char *name = pe->path;
517 size_t name_len = pe->path_len;
518 const char *target = pe->data;
519 size_t target_len = strlen(target);
521 /* Keep in sync with struct got_imsg_fetch_symref definition! */
522 if (imsg_add(wbuf, &name_len, sizeof(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_len, sizeof(target_len)) == -1) {
528 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
529 ibuf_free(wbuf);
530 return err;
532 if (imsg_add(wbuf, name, name_len) == -1) {
533 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
534 ibuf_free(wbuf);
535 return err;
537 if (imsg_add(wbuf, target, target_len) == -1) {
538 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
539 ibuf_free(wbuf);
540 return err;
544 wbuf->fd = -1;
545 imsg_close(ibuf, wbuf);
546 return flush_imsg(ibuf);
549 const struct got_error *
550 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
551 struct got_object_id *refid, const char *refname)
553 const struct got_error *err = NULL;
554 struct ibuf *wbuf;
555 size_t len, reflen = strlen(refname);
557 len = sizeof(struct got_imsg_fetch_ref) + reflen;
558 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
559 return got_error(GOT_ERR_NO_SPACE);
561 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
562 if (wbuf == NULL)
563 return got_error_from_errno("imsg_create FETCH_REF");
565 /* Keep in sync with struct got_imsg_fetch_ref definition! */
566 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
567 err = got_error_from_errno("imsg_add FETCH_REF");
568 ibuf_free(wbuf);
569 return err;
571 if (imsg_add(wbuf, refname, reflen) == -1) {
572 err = got_error_from_errno("imsg_add FETCH_REF");
573 ibuf_free(wbuf);
574 return err;
577 wbuf->fd = -1;
578 imsg_close(ibuf, wbuf);
579 return flush_imsg(ibuf);
582 const struct got_error *
583 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
584 size_t msglen)
586 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
587 return got_error(GOT_ERR_NO_SPACE);
589 if (msglen == 0)
590 return NULL;
592 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
593 msg, msglen) == -1)
594 return got_error_from_errno(
595 "imsg_compose FETCH_SERVER_PROGRESS");
597 return flush_imsg(ibuf);
600 const struct got_error *
601 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
603 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
604 &bytes, sizeof(bytes)) == -1)
605 return got_error_from_errno(
606 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
608 return flush_imsg(ibuf);
611 const struct got_error *
612 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
614 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
615 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
616 return got_error_from_errno("imsg_compose FETCH");
617 return flush_imsg(ibuf);
621 const struct got_error *
622 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
623 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
624 off_t *packfile_size, struct imsgbuf *ibuf)
626 const struct got_error *err = NULL;
627 struct imsg imsg;
628 size_t datalen;
629 struct got_imsg_fetch_symrefs *isymrefs = NULL;
630 size_t n, remain;
631 off_t off;
632 int i;
634 *done = 0;
635 *id = NULL;
636 *refname = NULL;
637 *server_progress = NULL;
638 *packfile_size = 0;
640 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
641 if (err)
642 return err;
644 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
645 switch (imsg.hdr.type) {
646 case GOT_IMSG_ERROR:
647 if (datalen < sizeof(struct got_imsg_error)) {
648 err = got_error(GOT_ERR_PRIVSEP_LEN);
649 break;
651 err = recv_imsg_error(&imsg, datalen);
652 break;
653 case GOT_IMSG_FETCH_SYMREFS:
654 if (datalen < sizeof(*isymrefs)) {
655 err = got_error(GOT_ERR_PRIVSEP_LEN);
656 break;
658 if (isymrefs != NULL) {
659 err = got_error(GOT_ERR_PRIVSEP_MSG);
660 break;
662 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
663 off = sizeof(*isymrefs);
664 remain = datalen - off;
665 for (n = 0; n < isymrefs->nsymrefs; n++) {
666 struct got_imsg_fetch_symref *s;
667 char *name, *target;
668 if (remain < sizeof(struct got_imsg_fetch_symref)) {
669 err = got_error(GOT_ERR_PRIVSEP_LEN);
670 goto done;
672 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
673 off += sizeof(*s);
674 remain -= sizeof(*s);
675 if (remain < s->name_len) {
676 err = got_error(GOT_ERR_PRIVSEP_LEN);
677 goto done;
679 name = strndup(imsg.data + off, s->name_len);
680 if (name == NULL) {
681 err = got_error_from_errno("strndup");
682 goto done;
684 off += s->name_len;
685 remain -= s->name_len;
686 if (remain < s->target_len) {
687 err = got_error(GOT_ERR_PRIVSEP_LEN);
688 free(name);
689 goto done;
691 target = strndup(imsg.data + off, s->target_len);
692 if (target == NULL) {
693 err = got_error_from_errno("strndup");
694 free(name);
695 goto done;
697 off += s->target_len;
698 remain -= s->target_len;
699 err = got_pathlist_append(symrefs, name, target);
700 if (err) {
701 free(name);
702 free(target);
703 goto done;
706 break;
707 case GOT_IMSG_FETCH_REF:
708 if (datalen <= SHA1_DIGEST_LENGTH) {
709 err = got_error(GOT_ERR_PRIVSEP_MSG);
710 break;
712 *id = malloc(sizeof(**id));
713 if (*id == NULL) {
714 err = got_error_from_errno("malloc");
715 break;
717 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
718 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
719 datalen - SHA1_DIGEST_LENGTH);
720 if (*refname == NULL) {
721 err = got_error_from_errno("strndup");
722 break;
724 break;
725 case GOT_IMSG_FETCH_SERVER_PROGRESS:
726 if (datalen == 0) {
727 err = got_error(GOT_ERR_PRIVSEP_LEN);
728 break;
730 *server_progress = strndup(imsg.data, datalen);
731 if (*server_progress == NULL) {
732 err = got_error_from_errno("strndup");
733 break;
735 for (i = 0; i < datalen; i++) {
736 if (!isprint((unsigned char)(*server_progress)[i]) &&
737 !isspace((unsigned char)(*server_progress)[i])) {
738 err = got_error(GOT_ERR_PRIVSEP_MSG);
739 free(*server_progress);
740 *server_progress = NULL;
741 goto done;
744 break;
745 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
746 if (datalen < sizeof(*packfile_size)) {
747 err = got_error(GOT_ERR_PRIVSEP_MSG);
748 break;
750 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
751 break;
752 case GOT_IMSG_FETCH_DONE:
753 *id = malloc(sizeof(**id));
754 if (*id == NULL) {
755 err = got_error_from_errno("malloc");
756 break;
758 if (datalen != SHA1_DIGEST_LENGTH) {
759 err = got_error(GOT_ERR_PRIVSEP_MSG);
760 break;
762 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
763 *done = 1;
764 break;
765 default:
766 err = got_error(GOT_ERR_PRIVSEP_MSG);
767 break;
769 done:
770 if (err) {
771 free(*id);
772 *id = NULL;
773 free(*refname);
774 *refname = NULL;
776 imsg_free(&imsg);
777 return err;
780 const struct got_error *
781 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
782 int fd)
784 const struct got_error *err = NULL;
786 /* Keep in sync with struct got_imsg_index_pack_request */
787 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
788 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
789 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
790 close(fd);
791 return err;
793 return flush_imsg(ibuf);
796 const struct got_error *
797 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
799 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
802 const struct got_error *
803 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
804 int nobj_indexed, int nobj_loose, int nobj_resolved)
806 struct got_imsg_index_pack_progress iprogress;
808 iprogress.nobj_total = nobj_total;
809 iprogress.nobj_indexed = nobj_indexed;
810 iprogress.nobj_loose = nobj_loose;
811 iprogress.nobj_resolved = nobj_resolved;
813 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
814 &iprogress, sizeof(iprogress)) == -1)
815 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
817 return flush_imsg(ibuf);
820 const struct got_error *
821 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
823 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
824 return got_error_from_errno("imsg_compose FETCH");
825 return flush_imsg(ibuf);
828 const struct got_error *
829 got_privsep_recv_index_progress(int *done, int *nobj_total,
830 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
831 struct imsgbuf *ibuf)
833 const struct got_error *err = NULL;
834 struct imsg imsg;
835 struct got_imsg_index_pack_progress *iprogress;
836 size_t datalen;
838 *done = 0;
839 *nobj_total = 0;
840 *nobj_indexed = 0;
841 *nobj_resolved = 0;
843 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
844 if (err)
845 return err;
847 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
848 switch (imsg.hdr.type) {
849 case GOT_IMSG_ERROR:
850 if (datalen < sizeof(struct got_imsg_error)) {
851 err = got_error(GOT_ERR_PRIVSEP_LEN);
852 break;
854 err = recv_imsg_error(&imsg, datalen);
855 break;
856 case GOT_IMSG_IDXPACK_PROGRESS:
857 if (datalen < sizeof(*iprogress)) {
858 err = got_error(GOT_ERR_PRIVSEP_LEN);
859 break;
861 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
862 *nobj_total = iprogress->nobj_total;
863 *nobj_indexed = iprogress->nobj_indexed;
864 *nobj_loose = iprogress->nobj_loose;
865 *nobj_resolved = iprogress->nobj_resolved;
866 break;
867 case GOT_IMSG_IDXPACK_DONE:
868 if (datalen != 0) {
869 err = got_error(GOT_ERR_PRIVSEP_LEN);
870 break;
872 *done = 1;
873 break;
874 default:
875 err = got_error(GOT_ERR_PRIVSEP_MSG);
876 break;
879 imsg_free(&imsg);
880 return err;
883 const struct got_error *
884 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
885 struct imsgbuf *ibuf)
887 const struct got_error *err = NULL;
888 struct got_imsg_object *iobj;
889 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
891 if (datalen != sizeof(*iobj))
892 return got_error(GOT_ERR_PRIVSEP_LEN);
893 iobj = imsg->data;
895 *obj = calloc(1, sizeof(**obj));
896 if (*obj == NULL)
897 return got_error_from_errno("calloc");
899 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
900 (*obj)->type = iobj->type;
901 (*obj)->flags = iobj->flags;
902 (*obj)->hdrlen = iobj->hdrlen;
903 (*obj)->size = iobj->size;
904 /* path_packfile is handled by caller */
905 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
906 (*obj)->pack_offset = iobj->pack_offset;
907 (*obj)->pack_idx = iobj->pack_idx;
910 return err;
913 const struct got_error *
914 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
916 const struct got_error *err = NULL;
917 struct imsg imsg;
918 const size_t min_datalen =
919 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
921 *obj = NULL;
923 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
924 if (err)
925 return err;
927 switch (imsg.hdr.type) {
928 case GOT_IMSG_OBJECT:
929 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
930 break;
931 default:
932 err = got_error(GOT_ERR_PRIVSEP_MSG);
933 break;
936 imsg_free(&imsg);
938 return err;
941 static const struct got_error *
942 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
943 size_t logmsg_len)
945 const struct got_error *err = NULL;
946 size_t offset, remain;
948 offset = 0;
949 remain = logmsg_len;
950 while (remain > 0) {
951 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
953 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
954 commit->logmsg + offset, n) == -1) {
955 err = got_error_from_errno("imsg_compose "
956 "COMMIT_LOGMSG");
957 break;
960 err = flush_imsg(ibuf);
961 if (err)
962 break;
964 offset += n;
965 remain -= n;
968 return err;
971 const struct got_error *
972 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
974 const struct got_error *err = NULL;
975 struct got_imsg_commit_object *icommit;
976 uint8_t *buf;
977 size_t len, total;
978 struct got_object_qid *qid;
979 size_t author_len = strlen(commit->author);
980 size_t committer_len = strlen(commit->committer);
981 size_t logmsg_len = strlen(commit->logmsg);
983 total = sizeof(*icommit) + author_len + committer_len +
984 commit->nparents * SHA1_DIGEST_LENGTH;
986 buf = malloc(total);
987 if (buf == NULL)
988 return got_error_from_errno("malloc");
990 icommit = (struct got_imsg_commit_object *)buf;
991 memcpy(icommit->tree_id, commit->tree_id->sha1,
992 sizeof(icommit->tree_id));
993 icommit->author_len = author_len;
994 icommit->author_time = commit->author_time;
995 icommit->author_gmtoff = commit->author_gmtoff;
996 icommit->committer_len = committer_len;
997 icommit->committer_time = commit->committer_time;
998 icommit->committer_gmtoff = commit->committer_gmtoff;
999 icommit->logmsg_len = logmsg_len;
1000 icommit->nparents = commit->nparents;
1002 len = sizeof(*icommit);
1003 memcpy(buf + len, commit->author, author_len);
1004 len += author_len;
1005 memcpy(buf + len, commit->committer, committer_len);
1006 len += committer_len;
1007 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1008 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1009 len += SHA1_DIGEST_LENGTH;
1012 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1013 err = got_error_from_errno("imsg_compose COMMIT");
1014 goto done;
1017 if (logmsg_len == 0 ||
1018 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1019 err = flush_imsg(ibuf);
1020 if (err)
1021 goto done;
1023 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1024 done:
1025 free(buf);
1026 return err;
1029 static const struct got_error *
1030 get_commit_from_imsg(struct got_commit_object **commit,
1031 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1033 const struct got_error *err = NULL;
1034 struct got_imsg_commit_object *icommit;
1035 size_t len = 0;
1036 int i;
1038 if (datalen < sizeof(*icommit))
1039 return got_error(GOT_ERR_PRIVSEP_LEN);
1041 icommit = imsg->data;
1042 if (datalen != sizeof(*icommit) + icommit->author_len +
1043 icommit->committer_len +
1044 icommit->nparents * SHA1_DIGEST_LENGTH)
1045 return got_error(GOT_ERR_PRIVSEP_LEN);
1047 if (icommit->nparents < 0)
1048 return got_error(GOT_ERR_PRIVSEP_LEN);
1050 len += sizeof(*icommit);
1052 *commit = got_object_commit_alloc_partial();
1053 if (*commit == NULL)
1054 return got_error_from_errno(
1055 "got_object_commit_alloc_partial");
1057 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1058 SHA1_DIGEST_LENGTH);
1059 (*commit)->author_time = icommit->author_time;
1060 (*commit)->author_gmtoff = icommit->author_gmtoff;
1061 (*commit)->committer_time = icommit->committer_time;
1062 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1064 if (icommit->author_len == 0) {
1065 (*commit)->author = strdup("");
1066 if ((*commit)->author == NULL) {
1067 err = got_error_from_errno("strdup");
1068 goto done;
1070 } else {
1071 (*commit)->author = malloc(icommit->author_len + 1);
1072 if ((*commit)->author == NULL) {
1073 err = got_error_from_errno("malloc");
1074 goto done;
1076 memcpy((*commit)->author, imsg->data + len,
1077 icommit->author_len);
1078 (*commit)->author[icommit->author_len] = '\0';
1080 len += icommit->author_len;
1082 if (icommit->committer_len == 0) {
1083 (*commit)->committer = strdup("");
1084 if ((*commit)->committer == NULL) {
1085 err = got_error_from_errno("strdup");
1086 goto done;
1088 } else {
1089 (*commit)->committer =
1090 malloc(icommit->committer_len + 1);
1091 if ((*commit)->committer == NULL) {
1092 err = got_error_from_errno("malloc");
1093 goto done;
1095 memcpy((*commit)->committer, imsg->data + len,
1096 icommit->committer_len);
1097 (*commit)->committer[icommit->committer_len] = '\0';
1099 len += icommit->committer_len;
1101 if (icommit->logmsg_len == 0) {
1102 (*commit)->logmsg = strdup("");
1103 if ((*commit)->logmsg == NULL) {
1104 err = got_error_from_errno("strdup");
1105 goto done;
1107 } else {
1108 size_t offset = 0, remain = icommit->logmsg_len;
1110 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1111 if ((*commit)->logmsg == NULL) {
1112 err = got_error_from_errno("malloc");
1113 goto done;
1115 while (remain > 0) {
1116 struct imsg imsg_log;
1117 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1118 remain);
1120 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1121 if (err)
1122 goto done;
1124 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1125 err = got_error(GOT_ERR_PRIVSEP_MSG);
1126 goto done;
1129 memcpy((*commit)->logmsg + offset,
1130 imsg_log.data, n);
1131 imsg_free(&imsg_log);
1132 offset += n;
1133 remain -= n;
1135 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1138 for (i = 0; i < icommit->nparents; i++) {
1139 struct got_object_qid *qid;
1141 err = got_object_qid_alloc_partial(&qid);
1142 if (err)
1143 break;
1144 memcpy(qid->id, imsg->data + len +
1145 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1146 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1147 (*commit)->nparents++;
1149 done:
1150 if (err) {
1151 got_object_commit_close(*commit);
1152 *commit = NULL;
1154 return err;
1157 const struct got_error *
1158 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1160 const struct got_error *err = NULL;
1161 struct imsg imsg;
1162 size_t datalen;
1163 const size_t min_datalen =
1164 MIN(sizeof(struct got_imsg_error),
1165 sizeof(struct got_imsg_commit_object));
1167 *commit = NULL;
1169 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1170 if (err)
1171 return err;
1173 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1175 switch (imsg.hdr.type) {
1176 case GOT_IMSG_COMMIT:
1177 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1178 break;
1179 default:
1180 err = got_error(GOT_ERR_PRIVSEP_MSG);
1181 break;
1184 imsg_free(&imsg);
1186 return err;
1189 const struct got_error *
1190 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1191 int nentries)
1193 const struct got_error *err = NULL;
1194 struct got_imsg_tree_object itree;
1195 struct got_pathlist_entry *pe;
1196 size_t totlen;
1197 int nimsg; /* number of imsg queued in ibuf */
1199 itree.nentries = nentries;
1200 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1201 == -1)
1202 return got_error_from_errno("imsg_compose TREE");
1204 totlen = sizeof(itree);
1205 nimsg = 1;
1206 TAILQ_FOREACH(pe, entries, entry) {
1207 const char *name = pe->path;
1208 struct got_parsed_tree_entry *pte = pe->data;
1209 struct ibuf *wbuf;
1210 size_t namelen = strlen(name);
1211 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1213 if (len > MAX_IMSGSIZE)
1214 return got_error(GOT_ERR_NO_SPACE);
1216 nimsg++;
1217 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1218 err = flush_imsg(ibuf);
1219 if (err)
1220 return err;
1221 nimsg = 0;
1224 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1225 if (wbuf == NULL)
1226 return got_error_from_errno("imsg_create TREE_ENTRY");
1228 /* Keep in sync with struct got_imsg_tree_object definition! */
1229 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1230 err = got_error_from_errno("imsg_add TREE_ENTRY");
1231 ibuf_free(wbuf);
1232 return err;
1234 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1235 err = got_error_from_errno("imsg_add TREE_ENTRY");
1236 ibuf_free(wbuf);
1237 return err;
1240 if (imsg_add(wbuf, name, namelen) == -1) {
1241 err = got_error_from_errno("imsg_add TREE_ENTRY");
1242 ibuf_free(wbuf);
1243 return err;
1246 wbuf->fd = -1;
1247 imsg_close(ibuf, wbuf);
1249 totlen += len;
1252 return flush_imsg(ibuf);
1255 const struct got_error *
1256 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1258 const struct got_error *err = NULL;
1259 const size_t min_datalen =
1260 MIN(sizeof(struct got_imsg_error),
1261 sizeof(struct got_imsg_tree_object));
1262 struct got_imsg_tree_object *itree;
1263 int nentries = 0;
1265 *tree = NULL;
1266 get_more:
1267 err = read_imsg(ibuf);
1268 if (err)
1269 goto done;
1271 for (;;) {
1272 struct imsg imsg;
1273 size_t n;
1274 size_t datalen;
1275 struct got_imsg_tree_entry *ite;
1276 struct got_tree_entry *te = NULL;
1278 n = imsg_get(ibuf, &imsg);
1279 if (n == 0) {
1280 if (*tree && (*tree)->nentries != nentries)
1281 goto get_more;
1282 break;
1285 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1286 return got_error(GOT_ERR_PRIVSEP_LEN);
1288 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1290 switch (imsg.hdr.type) {
1291 case GOT_IMSG_ERROR:
1292 err = recv_imsg_error(&imsg, datalen);
1293 break;
1294 case GOT_IMSG_TREE:
1295 /* This message should only appear once. */
1296 if (*tree != NULL) {
1297 err = got_error(GOT_ERR_PRIVSEP_MSG);
1298 break;
1300 if (datalen != sizeof(*itree)) {
1301 err = got_error(GOT_ERR_PRIVSEP_LEN);
1302 break;
1304 itree = imsg.data;
1305 *tree = malloc(sizeof(**tree));
1306 if (*tree == NULL) {
1307 err = got_error_from_errno("malloc");
1308 break;
1310 (*tree)->entries = calloc(itree->nentries,
1311 sizeof(struct got_tree_entry));
1312 if ((*tree)->entries == NULL) {
1313 err = got_error_from_errno("malloc");
1314 break;
1316 (*tree)->nentries = itree->nentries;
1317 (*tree)->refcnt = 0;
1318 break;
1319 case GOT_IMSG_TREE_ENTRY:
1320 /* This message should be preceeded by GOT_IMSG_TREE. */
1321 if (*tree == NULL) {
1322 err = got_error(GOT_ERR_PRIVSEP_MSG);
1323 break;
1325 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1326 err = got_error(GOT_ERR_PRIVSEP_LEN);
1327 break;
1330 /* Remaining data contains the entry's name. */
1331 datalen -= sizeof(*ite);
1332 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1333 err = got_error(GOT_ERR_PRIVSEP_LEN);
1334 break;
1336 ite = imsg.data;
1338 if (datalen + 1 > sizeof(te->name)) {
1339 err = got_error(GOT_ERR_NO_SPACE);
1340 break;
1342 te = &(*tree)->entries[nentries];
1343 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1344 te->name[datalen] = '\0';
1346 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1347 te->mode = ite->mode;
1348 te->idx = nentries;
1349 nentries++;
1350 break;
1351 default:
1352 err = got_error(GOT_ERR_PRIVSEP_MSG);
1353 break;
1356 imsg_free(&imsg);
1358 done:
1359 if (*tree && (*tree)->nentries != nentries) {
1360 if (err == NULL)
1361 err = got_error(GOT_ERR_PRIVSEP_LEN);
1362 got_object_tree_close(*tree);
1363 *tree = NULL;
1366 return err;
1369 const struct got_error *
1370 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1371 const uint8_t *data)
1373 struct got_imsg_blob iblob;
1375 iblob.size = size;
1376 iblob.hdrlen = hdrlen;
1378 if (data) {
1379 uint8_t *buf;
1381 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1382 return got_error(GOT_ERR_NO_SPACE);
1384 buf = malloc(sizeof(iblob) + size);
1385 if (buf == NULL)
1386 return got_error_from_errno("malloc");
1388 memcpy(buf, &iblob, sizeof(iblob));
1389 memcpy(buf + sizeof(iblob), data, size);
1390 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1391 sizeof(iblob) + size) == -1) {
1392 free(buf);
1393 return got_error_from_errno("imsg_compose BLOB");
1395 free(buf);
1396 } else {
1397 /* Data has already been written to file descriptor. */
1398 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1399 sizeof(iblob)) == -1)
1400 return got_error_from_errno("imsg_compose BLOB");
1404 return flush_imsg(ibuf);
1407 const struct got_error *
1408 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1409 struct imsgbuf *ibuf)
1411 const struct got_error *err = NULL;
1412 struct imsg imsg;
1413 struct got_imsg_blob *iblob;
1414 size_t datalen;
1416 *outbuf = NULL;
1418 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1419 if (err)
1420 return err;
1422 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1424 switch (imsg.hdr.type) {
1425 case GOT_IMSG_BLOB:
1426 if (datalen < sizeof(*iblob)) {
1427 err = got_error(GOT_ERR_PRIVSEP_LEN);
1428 break;
1430 iblob = imsg.data;
1431 *size = iblob->size;
1432 *hdrlen = iblob->hdrlen;
1434 if (datalen == sizeof(*iblob)) {
1435 /* Data has been written to file descriptor. */
1436 break;
1439 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1440 err = got_error(GOT_ERR_PRIVSEP_LEN);
1441 break;
1444 *outbuf = malloc(*size);
1445 if (*outbuf == NULL) {
1446 err = got_error_from_errno("malloc");
1447 break;
1449 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1450 break;
1451 default:
1452 err = got_error(GOT_ERR_PRIVSEP_MSG);
1453 break;
1456 imsg_free(&imsg);
1458 return err;
1461 static const struct got_error *
1462 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1464 const struct got_error *err = NULL;
1465 size_t offset, remain;
1467 offset = 0;
1468 remain = tagmsg_len;
1469 while (remain > 0) {
1470 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1472 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1473 tag->tagmsg + offset, n) == -1) {
1474 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1475 break;
1478 err = flush_imsg(ibuf);
1479 if (err)
1480 break;
1482 offset += n;
1483 remain -= n;
1486 return err;
1489 const struct got_error *
1490 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1492 const struct got_error *err = NULL;
1493 struct got_imsg_tag_object *itag;
1494 uint8_t *buf;
1495 size_t len, total;
1496 size_t tag_len = strlen(tag->tag);
1497 size_t tagger_len = strlen(tag->tagger);
1498 size_t tagmsg_len = strlen(tag->tagmsg);
1500 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1502 buf = malloc(total);
1503 if (buf == NULL)
1504 return got_error_from_errno("malloc");
1506 itag = (struct got_imsg_tag_object *)buf;
1507 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1508 itag->obj_type = tag->obj_type;
1509 itag->tag_len = tag_len;
1510 itag->tagger_len = tagger_len;
1511 itag->tagger_time = tag->tagger_time;
1512 itag->tagger_gmtoff = tag->tagger_gmtoff;
1513 itag->tagmsg_len = tagmsg_len;
1515 len = sizeof(*itag);
1516 memcpy(buf + len, tag->tag, tag_len);
1517 len += tag_len;
1518 memcpy(buf + len, tag->tagger, tagger_len);
1519 len += tagger_len;
1521 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1522 err = got_error_from_errno("imsg_compose TAG");
1523 goto done;
1526 if (tagmsg_len == 0 ||
1527 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1528 err = flush_imsg(ibuf);
1529 if (err)
1530 goto done;
1532 err = send_tagmsg(ibuf, tag, tagmsg_len);
1533 done:
1534 free(buf);
1535 return err;
1538 const struct got_error *
1539 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1541 const struct got_error *err = NULL;
1542 struct imsg imsg;
1543 struct got_imsg_tag_object *itag;
1544 size_t len, datalen;
1545 const size_t min_datalen =
1546 MIN(sizeof(struct got_imsg_error),
1547 sizeof(struct got_imsg_tag_object));
1549 *tag = NULL;
1551 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1552 if (err)
1553 return err;
1555 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1556 len = 0;
1558 switch (imsg.hdr.type) {
1559 case GOT_IMSG_TAG:
1560 if (datalen < sizeof(*itag)) {
1561 err = got_error(GOT_ERR_PRIVSEP_LEN);
1562 break;
1564 itag = imsg.data;
1565 if (datalen != sizeof(*itag) + itag->tag_len +
1566 itag->tagger_len) {
1567 err = got_error(GOT_ERR_PRIVSEP_LEN);
1568 break;
1570 len += sizeof(*itag);
1572 *tag = calloc(1, sizeof(**tag));
1573 if (*tag == NULL) {
1574 err = got_error_from_errno("calloc");
1575 break;
1578 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1580 if (itag->tag_len == 0) {
1581 (*tag)->tag = strdup("");
1582 if ((*tag)->tag == NULL) {
1583 err = got_error_from_errno("strdup");
1584 break;
1586 } else {
1587 (*tag)->tag = malloc(itag->tag_len + 1);
1588 if ((*tag)->tag == NULL) {
1589 err = got_error_from_errno("malloc");
1590 break;
1592 memcpy((*tag)->tag, imsg.data + len,
1593 itag->tag_len);
1594 (*tag)->tag[itag->tag_len] = '\0';
1596 len += itag->tag_len;
1598 (*tag)->obj_type = itag->obj_type;
1599 (*tag)->tagger_time = itag->tagger_time;
1600 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1602 if (itag->tagger_len == 0) {
1603 (*tag)->tagger = strdup("");
1604 if ((*tag)->tagger == NULL) {
1605 err = got_error_from_errno("strdup");
1606 break;
1608 } else {
1609 (*tag)->tagger = malloc(itag->tagger_len + 1);
1610 if ((*tag)->tagger == NULL) {
1611 err = got_error_from_errno("malloc");
1612 break;
1614 memcpy((*tag)->tagger, imsg.data + len,
1615 itag->tagger_len);
1616 (*tag)->tagger[itag->tagger_len] = '\0';
1618 len += itag->tagger_len;
1620 if (itag->tagmsg_len == 0) {
1621 (*tag)->tagmsg = strdup("");
1622 if ((*tag)->tagmsg == NULL) {
1623 err = got_error_from_errno("strdup");
1624 break;
1626 } else {
1627 size_t offset = 0, remain = itag->tagmsg_len;
1629 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1630 if ((*tag)->tagmsg == NULL) {
1631 err = got_error_from_errno("malloc");
1632 break;
1634 while (remain > 0) {
1635 struct imsg imsg_log;
1636 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1637 remain);
1639 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1640 if (err)
1641 return err;
1643 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1644 return got_error(GOT_ERR_PRIVSEP_MSG);
1646 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1647 n);
1648 imsg_free(&imsg_log);
1649 offset += n;
1650 remain -= n;
1652 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1655 break;
1656 default:
1657 err = got_error(GOT_ERR_PRIVSEP_MSG);
1658 break;
1661 imsg_free(&imsg);
1663 return err;
1666 const struct got_error *
1667 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1668 struct got_packidx *packidx)
1670 const struct got_error *err = NULL;
1671 struct got_imsg_packidx ipackidx;
1672 struct got_imsg_pack ipack;
1673 int fd;
1675 ipackidx.len = packidx->len;
1676 fd = dup(packidx->fd);
1677 if (fd == -1)
1678 return got_error_from_errno("dup");
1680 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1681 sizeof(ipackidx)) == -1) {
1682 err = got_error_from_errno("imsg_compose PACKIDX");
1683 close(fd);
1684 return err;
1687 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1688 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1689 return got_error(GOT_ERR_NO_SPACE);
1690 ipack.filesize = pack->filesize;
1692 fd = dup(pack->fd);
1693 if (fd == -1)
1694 return got_error_from_errno("dup");
1696 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1697 == -1) {
1698 err = got_error_from_errno("imsg_compose PACK");
1699 close(fd);
1700 return err;
1703 return flush_imsg(ibuf);
1706 const struct got_error *
1707 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1708 struct got_object_id *id)
1710 struct got_imsg_packed_object iobj;
1712 iobj.idx = idx;
1713 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1715 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1716 &iobj, sizeof(iobj)) == -1)
1717 return got_error_from_errno("imsg_compose "
1718 "PACKED_OBJECT_REQUEST");
1720 return flush_imsg(ibuf);
1723 const struct got_error *
1724 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1726 const struct got_error *err = NULL;
1728 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1729 NULL, 0) == -1) {
1730 err = got_error_from_errno("imsg_compose "
1731 "GITCONFIG_PARSE_REQUEST");
1732 close(fd);
1733 return err;
1736 return flush_imsg(ibuf);
1739 const struct got_error *
1740 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1742 if (imsg_compose(ibuf,
1743 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1744 NULL, 0) == -1)
1745 return got_error_from_errno("imsg_compose "
1746 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1748 return flush_imsg(ibuf);
1751 const struct got_error *
1752 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1754 if (imsg_compose(ibuf,
1755 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1756 return got_error_from_errno("imsg_compose "
1757 "GITCONFIG_AUTHOR_NAME_REQUEST");
1759 return flush_imsg(ibuf);
1762 const struct got_error *
1763 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1765 if (imsg_compose(ibuf,
1766 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1767 return got_error_from_errno("imsg_compose "
1768 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1770 return flush_imsg(ibuf);
1773 const struct got_error *
1774 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1776 if (imsg_compose(ibuf,
1777 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1778 return got_error_from_errno("imsg_compose "
1779 "GITCONFIG_REMOTE_REQUEST");
1781 return flush_imsg(ibuf);
1784 const struct got_error *
1785 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1787 if (imsg_compose(ibuf,
1788 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1789 return got_error_from_errno("imsg_compose "
1790 "GITCONFIG_OWNER_REQUEST");
1792 return flush_imsg(ibuf);
1795 const struct got_error *
1796 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1798 size_t len = value ? strlen(value) + 1 : 0;
1800 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1801 value, len) == -1)
1802 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1804 return flush_imsg(ibuf);
1807 const struct got_error *
1808 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1810 const struct got_error *err = NULL;
1811 struct imsg imsg;
1812 size_t datalen;
1813 const size_t min_datalen = 0;
1815 *str = NULL;
1817 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1818 if (err)
1819 return err;
1820 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1822 switch (imsg.hdr.type) {
1823 case GOT_IMSG_GITCONFIG_STR_VAL:
1824 if (datalen == 0)
1825 break;
1826 *str = malloc(datalen);
1827 if (*str == NULL) {
1828 err = got_error_from_errno("malloc");
1829 break;
1831 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1832 err = got_error(GOT_ERR_NO_SPACE);
1833 break;
1834 default:
1835 err = got_error(GOT_ERR_PRIVSEP_MSG);
1836 break;
1839 imsg_free(&imsg);
1840 return err;
1843 const struct got_error *
1844 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1846 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1847 &value, sizeof(value)) == -1)
1848 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1850 return flush_imsg(ibuf);
1853 const struct got_error *
1854 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1856 const struct got_error *err = NULL;
1857 struct imsg imsg;
1858 size_t datalen;
1859 const size_t min_datalen =
1860 MIN(sizeof(struct got_imsg_error), sizeof(int));
1862 *val = 0;
1864 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1865 if (err)
1866 return err;
1867 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1869 switch (imsg.hdr.type) {
1870 case GOT_IMSG_GITCONFIG_INT_VAL:
1871 if (datalen != sizeof(*val)) {
1872 err = got_error(GOT_ERR_PRIVSEP_LEN);
1873 break;
1875 memcpy(val, imsg.data, sizeof(*val));
1876 break;
1877 default:
1878 err = got_error(GOT_ERR_PRIVSEP_MSG);
1879 break;
1882 imsg_free(&imsg);
1883 return err;
1886 const struct got_error *
1887 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1888 struct got_remote_repo *remotes, int nremotes)
1890 const struct got_error *err = NULL;
1891 struct got_imsg_remotes iremotes;
1892 int i;
1894 iremotes.nremotes = nremotes;
1895 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1896 &iremotes, sizeof(iremotes)) == -1)
1897 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1899 err = flush_imsg(ibuf);
1900 imsg_clear(ibuf);
1901 if (err)
1902 return err;
1904 for (i = 0; i < nremotes; i++) {
1905 struct got_imsg_remote iremote;
1906 size_t len = sizeof(iremote);
1907 struct ibuf *wbuf;
1909 iremote.name_len = strlen(remotes[i].name);
1910 len += iremote.name_len;
1911 iremote.url_len = strlen(remotes[i].url);
1912 len += iremote.url_len;
1914 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1915 if (wbuf == NULL)
1916 return got_error_from_errno(
1917 "imsg_create GITCONFIG_REMOTE");
1919 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1920 err = got_error_from_errno(
1921 "imsg_add GITCONFIG_REMOTE");
1922 ibuf_free(wbuf);
1923 return err;
1926 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1927 err = got_error_from_errno(
1928 "imsg_add GITCONFIG_REMOTE");
1929 ibuf_free(wbuf);
1930 return err;
1932 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1933 err = got_error_from_errno(
1934 "imsg_add GITCONFIG_REMOTE");
1935 ibuf_free(wbuf);
1936 return err;
1938 if (imsg_add(wbuf, &remotes[i].mirror_references,
1939 sizeof(iremote.mirror_references)) == -1) {
1940 err = got_error_from_errno(
1941 "imsg_add GITCONFIG_REMOTE");
1942 ibuf_free(wbuf);
1943 return err;
1946 wbuf->fd = -1;
1947 imsg_close(ibuf, wbuf);
1948 err = flush_imsg(ibuf);
1949 if (err)
1950 return err;
1953 return NULL;
1956 const struct got_error *
1957 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1958 int *nremotes, struct imsgbuf *ibuf)
1960 const struct got_error *err = NULL;
1961 struct imsg imsg;
1962 size_t datalen;
1963 struct got_imsg_remotes iremotes;
1964 struct got_imsg_remote iremote;
1966 *remotes = NULL;
1967 *nremotes = 0;
1968 iremotes.nremotes = 0;
1970 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1971 if (err)
1972 return err;
1973 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1975 switch (imsg.hdr.type) {
1976 case GOT_IMSG_GITCONFIG_REMOTES:
1977 if (datalen != sizeof(iremotes)) {
1978 err = got_error(GOT_ERR_PRIVSEP_LEN);
1979 break;
1981 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1982 if (iremotes.nremotes == 0) {
1983 imsg_free(&imsg);
1984 return NULL;
1986 break;
1987 default:
1988 imsg_free(&imsg);
1989 return got_error(GOT_ERR_PRIVSEP_MSG);
1992 imsg_free(&imsg);
1994 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1995 if (*remotes == NULL)
1996 return got_error_from_errno("recallocarray");
1998 while (*nremotes < iremotes.nremotes) {
1999 struct got_remote_repo *remote;
2001 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2002 if (err)
2003 break;
2004 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2006 switch (imsg.hdr.type) {
2007 case GOT_IMSG_GITCONFIG_REMOTE:
2008 remote = &(*remotes)[*nremotes];
2009 if (datalen < sizeof(iremote)) {
2010 err = got_error(GOT_ERR_PRIVSEP_LEN);
2011 break;
2013 memcpy(&iremote, imsg.data, sizeof(iremote));
2014 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2015 (sizeof(iremote) + iremote.name_len +
2016 iremote.url_len) > datalen) {
2017 err = got_error(GOT_ERR_PRIVSEP_LEN);
2018 break;
2020 remote->name = strndup(imsg.data + sizeof(iremote),
2021 iremote.name_len);
2022 if (remote->name == NULL) {
2023 err = got_error_from_errno("strndup");
2024 break;
2026 remote->url = strndup(imsg.data + sizeof(iremote) +
2027 iremote.name_len, iremote.url_len);
2028 if (remote->url == NULL) {
2029 err = got_error_from_errno("strndup");
2030 free(remote->name);
2031 break;
2033 remote->mirror_references = iremote.mirror_references;
2034 (*nremotes)++;
2035 break;
2036 default:
2037 err = got_error(GOT_ERR_PRIVSEP_MSG);
2038 break;
2041 imsg_free(&imsg);
2042 if (err)
2043 break;
2046 if (err) {
2047 int i;
2048 for (i = 0; i < *nremotes; i++) {
2049 free((*remotes)[i].name);
2050 free((*remotes)[i].url);
2052 free(*remotes);
2053 *remotes = NULL;
2054 *nremotes = 0;
2056 return err;
2059 const struct got_error *
2060 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2061 struct got_object_id *id, int idx, const char *path)
2063 const struct got_error *err = NULL;
2064 struct ibuf *wbuf;
2065 size_t path_len = strlen(path) + 1;
2067 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2068 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2069 if (wbuf == NULL)
2070 return got_error_from_errno(
2071 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2072 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2073 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2074 ibuf_free(wbuf);
2075 return err;
2077 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2078 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2079 ibuf_free(wbuf);
2080 return err;
2082 if (imsg_add(wbuf, path, path_len) == -1) {
2083 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2084 ibuf_free(wbuf);
2085 return err;
2088 wbuf->fd = -1;
2089 imsg_close(ibuf, wbuf);
2091 return flush_imsg(ibuf);
2094 const struct got_error *
2095 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2096 size_t ncommits, struct imsgbuf *ibuf)
2098 const struct got_error *err;
2099 struct ibuf *wbuf;
2100 int i;
2102 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2103 sizeof(struct got_imsg_traversed_commits) +
2104 ncommits * SHA1_DIGEST_LENGTH);
2105 if (wbuf == NULL)
2106 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2108 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2109 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2110 ibuf_free(wbuf);
2111 return err;
2113 for (i = 0; i < ncommits; i++) {
2114 struct got_object_id *id = &commit_ids[i];
2115 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2116 err = got_error_from_errno(
2117 "imsg_add TRAVERSED_COMMITS");
2118 ibuf_free(wbuf);
2119 return err;
2123 wbuf->fd = -1;
2124 imsg_close(ibuf, wbuf);
2126 return flush_imsg(ibuf);
2129 const struct got_error *
2130 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2131 struct got_object_id **changed_commit_id,
2132 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2134 const struct got_error *err = NULL;
2135 struct imsg imsg;
2136 struct got_imsg_traversed_commits *icommits;
2137 size_t datalen;
2138 int i, done = 0;
2140 *changed_commit = NULL;
2141 *changed_commit_id = NULL;
2143 while (!done) {
2144 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2145 if (err)
2146 return err;
2148 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2149 switch (imsg.hdr.type) {
2150 case GOT_IMSG_TRAVERSED_COMMITS:
2151 icommits = imsg.data;
2152 if (datalen != sizeof(*icommits) +
2153 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2154 err = got_error(GOT_ERR_PRIVSEP_LEN);
2155 break;
2157 for (i = 0; i < icommits->ncommits; i++) {
2158 struct got_object_qid *qid;
2159 uint8_t *sha1 = (uint8_t *)imsg.data +
2160 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2161 err = got_object_qid_alloc_partial(&qid);
2162 if (err)
2163 break;
2164 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2165 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2167 /* The last commit may contain a change. */
2168 if (i == icommits->ncommits - 1) {
2169 *changed_commit_id =
2170 got_object_id_dup(qid->id);
2171 if (*changed_commit_id == NULL) {
2172 err = got_error_from_errno(
2173 "got_object_id_dup");
2174 break;
2178 break;
2179 case GOT_IMSG_COMMIT:
2180 if (*changed_commit_id == NULL) {
2181 err = got_error(GOT_ERR_PRIVSEP_MSG);
2182 break;
2184 err = get_commit_from_imsg(changed_commit, &imsg,
2185 datalen, ibuf);
2186 break;
2187 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2188 done = 1;
2189 break;
2190 default:
2191 err = got_error(GOT_ERR_PRIVSEP_MSG);
2192 break;
2195 imsg_free(&imsg);
2196 if (err)
2197 break;
2200 if (err)
2201 got_object_id_queue_free(commit_ids);
2202 return err;
2205 const struct got_error *
2206 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2208 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2209 NULL, 0) == -1)
2210 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2212 return flush_imsg(ibuf);
2215 const struct got_error *
2216 got_privsep_unveil_exec_helpers(void)
2218 const char *helpers[] = {
2219 GOT_PATH_PROG_READ_PACK,
2220 GOT_PATH_PROG_READ_OBJECT,
2221 GOT_PATH_PROG_READ_COMMIT,
2222 GOT_PATH_PROG_READ_TREE,
2223 GOT_PATH_PROG_READ_BLOB,
2224 GOT_PATH_PROG_READ_TAG,
2225 GOT_PATH_PROG_READ_GITCONFIG,
2226 GOT_PATH_PROG_FETCH_PACK,
2227 GOT_PATH_PROG_INDEX_PACK,
2229 int i;
2231 for (i = 0; i < nitems(helpers); i++) {
2232 if (unveil(helpers[i], "x") == 0)
2233 continue;
2234 return got_error_from_errno2("unveil", helpers[i]);
2237 return NULL;
2240 void
2241 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2243 if (close(imsg_fds[0]) != 0) {
2244 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2245 _exit(1);
2248 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2249 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2250 _exit(1);
2252 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2253 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2254 _exit(1);
2257 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2258 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2259 strerror(errno));
2260 _exit(1);