Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/syslimits.h>
22 #include <sys/wait.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <stdint.h>
30 #include <poll.h>
31 #include <imsg.h>
32 #include <sha1.h>
33 #include <zlib.h>
34 #include <time.h>
36 #include "got_object.h"
37 #include "got_error.h"
38 #include "got_path.h"
39 #include "got_repository.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_pack.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 static const struct got_error *
58 poll_fd(int fd, int events, int timeout)
59 {
60 struct pollfd pfd[1];
61 int n;
63 pfd[0].fd = fd;
64 pfd[0].events = events;
66 n = poll(pfd, 1, timeout);
67 if (n == -1)
68 return got_error_from_errno("poll");
69 if (n == 0)
70 return got_error(GOT_ERR_TIMEOUT);
71 if (pfd[0].revents & (POLLERR | POLLNVAL))
72 return got_error_from_errno("poll error");
73 if (pfd[0].revents & (events | POLLHUP))
74 return NULL;
76 return got_error(GOT_ERR_INTERRUPT);
77 }
79 static const struct got_error *
80 read_imsg(struct imsgbuf *ibuf)
81 {
82 const struct got_error *err;
83 size_t n;
85 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
86 if (err)
87 return err;
89 n = imsg_read(ibuf);
90 if (n == -1) {
91 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
92 return got_error(GOT_ERR_PRIVSEP_NO_FD);
93 return got_error(GOT_ERR_PRIVSEP_READ);
94 }
95 if (n == 0)
96 return got_error(GOT_ERR_PRIVSEP_PIPE);
98 return NULL;
99 }
101 const struct got_error *
102 got_privsep_wait_for_child(pid_t pid)
104 int child_status;
106 if (waitpid(pid, &child_status, 0) == -1)
107 return got_error_from_errno("waitpid");
109 if (!WIFEXITED(child_status))
110 return got_error(GOT_ERR_PRIVSEP_DIED);
112 if (WEXITSTATUS(child_status) != 0)
113 return got_error(GOT_ERR_PRIVSEP_EXIT);
115 return NULL;
118 static const struct got_error *
119 recv_imsg_error(struct imsg *imsg, size_t datalen)
121 struct got_imsg_error *ierr;
123 if (datalen != sizeof(*ierr))
124 return got_error(GOT_ERR_PRIVSEP_LEN);
126 ierr = imsg->data;
127 if (ierr->code == GOT_ERR_ERRNO) {
128 static struct got_error serr;
129 serr.code = GOT_ERR_ERRNO;
130 serr.msg = strerror(ierr->errno_code);
131 return &serr;
134 return got_error(ierr->code);
137 const struct got_error *
138 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
139 size_t min_datalen)
141 const struct got_error *err;
142 ssize_t n;
144 n = imsg_get(ibuf, imsg);
145 if (n == -1)
146 return got_error_from_errno("imsg_get");
148 while (n == 0) {
149 err = read_imsg(ibuf);
150 if (err)
151 return err;
152 n = imsg_get(ibuf, imsg);
153 if (n == -1)
154 return got_error_from_errno("imsg_get");
157 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
158 return got_error(GOT_ERR_PRIVSEP_LEN);
160 if (imsg->hdr.type == GOT_IMSG_ERROR) {
161 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 return recv_imsg_error(imsg, datalen);
165 return NULL;
168 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
169 void
170 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
172 const struct got_error *poll_err;
173 struct got_imsg_error ierr;
174 int ret;
176 ierr.code = err->code;
177 if (err->code == GOT_ERR_ERRNO)
178 ierr.errno_code = errno;
179 else
180 ierr.errno_code = 0;
181 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
182 if (ret == -1) {
183 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
184 getprogname(), err->code, err->msg, strerror(errno));
185 return;
188 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
189 if (poll_err) {
190 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
191 getprogname(), err->code, err->msg, poll_err->msg);
192 return;
195 ret = imsg_flush(ibuf);
196 if (ret == -1) {
197 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
198 getprogname(), err->code, err->msg, strerror(errno));
199 return;
203 static const struct got_error *
204 flush_imsg(struct imsgbuf *ibuf)
206 const struct got_error *err;
208 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
209 if (err)
210 return err;
212 if (imsg_flush(ibuf) == -1)
213 return got_error_from_errno("imsg_flush");
215 return NULL;
218 const struct got_error *
219 got_privsep_send_stop(int fd)
221 const struct got_error *err = NULL;
222 struct imsgbuf ibuf;
224 imsg_init(&ibuf, fd);
226 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
227 return got_error_from_errno("imsg_compose STOP");
229 err = flush_imsg(&ibuf);
230 imsg_clear(&ibuf);
231 return err;
234 const struct got_error *
235 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
237 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
238 == -1)
239 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
241 return flush_imsg(ibuf);
244 const struct got_error *
245 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
246 struct got_object_id *id, int pack_idx)
248 const struct got_error *err = NULL;
249 struct got_imsg_packed_object iobj, *iobjp;
250 size_t len;
252 if (id) { /* commit is packed */
253 iobj.idx = pack_idx;
254 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
255 iobjp = &iobj;
256 len = sizeof(iobj);
257 } else {
258 iobjp = NULL;
259 len = 0;
262 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
263 == -1) {
264 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
265 close(fd);
266 return err;
269 return flush_imsg(ibuf);
272 const struct got_error *
273 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
274 struct got_object_id *id, int pack_idx)
276 const struct got_error *err = NULL;
277 struct ibuf *wbuf;
278 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
280 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
281 if (wbuf == NULL)
282 return got_error_from_errno("imsg_create TREE_REQUEST");
284 if (id) { /* tree is packed */
285 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
286 err = got_error_from_errno("imsg_add TREE_ENTRY");
287 ibuf_free(wbuf);
288 return err;
291 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
292 err = got_error_from_errno("imsg_add TREE_ENTRY");
293 ibuf_free(wbuf);
294 return err;
298 wbuf->fd = fd;
299 imsg_close(ibuf, wbuf);
301 return flush_imsg(ibuf);
304 const struct got_error *
305 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
306 struct got_object_id *id, int pack_idx)
308 struct got_imsg_packed_object iobj, *iobjp;
309 size_t len;
311 if (id) { /* tag is packed */
312 iobj.idx = pack_idx;
313 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
314 iobjp = &iobj;
315 len = sizeof(iobj);
316 } else {
317 iobjp = NULL;
318 len = 0;
321 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
322 == -1)
323 return got_error_from_errno("imsg_compose TAG_REQUEST");
325 return flush_imsg(ibuf);
328 const struct got_error *
329 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
330 struct got_object_id *id, int pack_idx)
332 const struct got_error *err = NULL;
333 struct got_imsg_packed_object iobj, *iobjp;
334 size_t len;
336 if (id) { /* blob is packed */
337 iobj.idx = pack_idx;
338 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
339 iobjp = &iobj;
340 len = sizeof(iobj);
341 } else {
342 iobjp = NULL;
343 len = 0;
346 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
347 == -1) {
348 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
349 close(infd);
350 return err;
353 return flush_imsg(ibuf);
356 const struct got_error *
357 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
359 const struct got_error *err = NULL;
361 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
362 == -1) {
363 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
364 close(outfd);
365 return err;
368 return flush_imsg(ibuf);
371 static const struct got_error *
372 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
374 const struct got_error *err = NULL;
376 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
377 err = got_error_from_errno("imsg_compose TMPFD");
378 close(fd);
379 return err;
382 return flush_imsg(ibuf);
385 const struct got_error *
386 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
388 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
391 const struct got_error *
392 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
394 struct got_imsg_object iobj;
396 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
397 iobj.type = obj->type;
398 iobj.flags = obj->flags;
399 iobj.hdrlen = obj->hdrlen;
400 iobj.size = obj->size;
401 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
402 iobj.pack_offset = obj->pack_offset;
403 iobj.pack_idx = obj->pack_idx;
406 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
407 == -1)
408 return got_error_from_errno("imsg_compose OBJECT");
410 return flush_imsg(ibuf);
413 const struct got_error *
414 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
415 struct got_pathlist_head *have_refs, int fetch_all_branches,
416 struct got_pathlist_head *wanted_branches)
418 const struct got_error *err = NULL;
419 struct ibuf *wbuf;
420 size_t len;
421 struct got_pathlist_entry *pe;
422 struct got_imsg_fetch_request fetchreq;
424 memset(&fetchreq, 0, sizeof(fetchreq));
425 fetchreq.fetch_all_branches = fetch_all_branches;
426 TAILQ_FOREACH(pe, have_refs, entry)
427 fetchreq.n_have_refs++;
428 TAILQ_FOREACH(pe, wanted_branches, entry)
429 fetchreq.n_wanted_branches++;
430 len = sizeof(struct got_imsg_fetch_request);
431 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
432 close(fd);
433 return got_error(GOT_ERR_NO_SPACE);
436 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
437 &fetchreq, sizeof(fetchreq)) == -1)
438 return got_error_from_errno(
439 "imsg_compose FETCH_SERVER_PROGRESS");
441 err = flush_imsg(ibuf);
442 if (err) {
443 close(fd);
444 return err;
446 fd = -1;
448 TAILQ_FOREACH(pe, have_refs, entry) {
449 const char *name = pe->path;
450 size_t name_len = pe->path_len;
451 struct got_object_id *id = pe->data;
453 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
454 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
455 if (wbuf == NULL)
456 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
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_HAVE_REF");
461 ibuf_free(wbuf);
462 return err;
464 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
465 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
466 ibuf_free(wbuf);
467 return err;
469 if (imsg_add(wbuf, name, name_len) == -1) {
470 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
471 ibuf_free(wbuf);
472 return err;
475 wbuf->fd = -1;
476 imsg_close(ibuf, wbuf);
477 err = flush_imsg(ibuf);
478 if (err)
479 return err;
482 TAILQ_FOREACH(pe, wanted_branches, entry) {
483 const char *name = pe->path;
484 size_t name_len = pe->path_len;
486 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
487 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
488 len);
489 if (wbuf == NULL)
490 return got_error_from_errno(
491 "imsg_create FETCH_WANTED_BRANCH");
493 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
494 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
495 err = got_error_from_errno(
496 "imsg_add FETCH_WANTED_BRANCH");
497 ibuf_free(wbuf);
498 return err;
500 if (imsg_add(wbuf, name, name_len) == -1) {
501 err = got_error_from_errno(
502 "imsg_add FETCH_WANTED_BRANCH");
503 ibuf_free(wbuf);
504 return err;
507 wbuf->fd = -1;
508 imsg_close(ibuf, wbuf);
509 err = flush_imsg(ibuf);
510 if (err)
511 return err;
514 return NULL;
518 const struct got_error *
519 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
521 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
525 const struct got_error *
526 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
527 struct got_pathlist_head *symrefs)
529 const struct got_error *err = NULL;
530 struct ibuf *wbuf;
531 size_t len, nsymrefs = 0;
532 struct got_pathlist_entry *pe;
534 len = sizeof(struct got_imsg_fetch_symrefs);
535 TAILQ_FOREACH(pe, symrefs, entry) {
536 const char *target = pe->data;
537 len += sizeof(struct got_imsg_fetch_symref) +
538 pe->path_len + strlen(target);
539 nsymrefs++;
542 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
543 return got_error(GOT_ERR_NO_SPACE);
545 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
546 if (wbuf == NULL)
547 return got_error_from_errno("imsg_create FETCH_SYMREFS");
549 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
550 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
551 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
552 ibuf_free(wbuf);
553 return err;
556 TAILQ_FOREACH(pe, symrefs, entry) {
557 const char *name = pe->path;
558 size_t name_len = pe->path_len;
559 const char *target = pe->data;
560 size_t target_len = strlen(target);
562 /* Keep in sync with struct got_imsg_fetch_symref definition! */
563 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
564 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
565 ibuf_free(wbuf);
566 return err;
568 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
569 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
570 ibuf_free(wbuf);
571 return err;
573 if (imsg_add(wbuf, name, name_len) == -1) {
574 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
575 ibuf_free(wbuf);
576 return err;
578 if (imsg_add(wbuf, target, target_len) == -1) {
579 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
580 ibuf_free(wbuf);
581 return err;
585 wbuf->fd = -1;
586 imsg_close(ibuf, wbuf);
587 return flush_imsg(ibuf);
590 const struct got_error *
591 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
592 struct got_object_id *refid, const char *refname)
594 const struct got_error *err = NULL;
595 struct ibuf *wbuf;
596 size_t len, reflen = strlen(refname);
598 len = sizeof(struct got_imsg_fetch_ref) + reflen;
599 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
600 return got_error(GOT_ERR_NO_SPACE);
602 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
603 if (wbuf == NULL)
604 return got_error_from_errno("imsg_create FETCH_REF");
606 /* Keep in sync with struct got_imsg_fetch_ref definition! */
607 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
608 err = got_error_from_errno("imsg_add FETCH_REF");
609 ibuf_free(wbuf);
610 return err;
612 if (imsg_add(wbuf, refname, reflen) == -1) {
613 err = got_error_from_errno("imsg_add FETCH_REF");
614 ibuf_free(wbuf);
615 return err;
618 wbuf->fd = -1;
619 imsg_close(ibuf, wbuf);
620 return flush_imsg(ibuf);
623 const struct got_error *
624 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
625 size_t msglen)
627 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
628 return got_error(GOT_ERR_NO_SPACE);
630 if (msglen == 0)
631 return NULL;
633 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
634 msg, msglen) == -1)
635 return got_error_from_errno(
636 "imsg_compose FETCH_SERVER_PROGRESS");
638 return flush_imsg(ibuf);
641 const struct got_error *
642 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
644 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
645 &bytes, sizeof(bytes)) == -1)
646 return got_error_from_errno(
647 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
649 return flush_imsg(ibuf);
652 const struct got_error *
653 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
655 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
656 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
657 return got_error_from_errno("imsg_compose FETCH");
658 return flush_imsg(ibuf);
662 const struct got_error *
663 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
664 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
665 off_t *packfile_size, struct imsgbuf *ibuf)
667 const struct got_error *err = NULL;
668 struct imsg imsg;
669 size_t datalen;
670 struct got_imsg_fetch_symrefs *isymrefs = NULL;
671 size_t n, remain;
672 off_t off;
673 int i;
675 *done = 0;
676 *id = NULL;
677 *refname = NULL;
678 *server_progress = NULL;
679 *packfile_size = 0;
681 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
682 if (err)
683 return err;
685 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
686 switch (imsg.hdr.type) {
687 case GOT_IMSG_ERROR:
688 if (datalen < sizeof(struct got_imsg_error)) {
689 err = got_error(GOT_ERR_PRIVSEP_LEN);
690 break;
692 err = recv_imsg_error(&imsg, datalen);
693 break;
694 case GOT_IMSG_FETCH_SYMREFS:
695 if (datalen < sizeof(*isymrefs)) {
696 err = got_error(GOT_ERR_PRIVSEP_LEN);
697 break;
699 if (isymrefs != NULL) {
700 err = got_error(GOT_ERR_PRIVSEP_MSG);
701 break;
703 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
704 off = sizeof(*isymrefs);
705 remain = datalen - off;
706 for (n = 0; n < isymrefs->nsymrefs; n++) {
707 struct got_imsg_fetch_symref *s;
708 char *name, *target;
709 if (remain < sizeof(struct got_imsg_fetch_symref)) {
710 err = got_error(GOT_ERR_PRIVSEP_LEN);
711 goto done;
713 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
714 off += sizeof(*s);
715 remain -= sizeof(*s);
716 if (remain < s->name_len) {
717 err = got_error(GOT_ERR_PRIVSEP_LEN);
718 goto done;
720 name = strndup(imsg.data + off, s->name_len);
721 if (name == NULL) {
722 err = got_error_from_errno("strndup");
723 goto done;
725 off += s->name_len;
726 remain -= s->name_len;
727 if (remain < s->target_len) {
728 err = got_error(GOT_ERR_PRIVSEP_LEN);
729 free(name);
730 goto done;
732 target = strndup(imsg.data + off, s->target_len);
733 if (target == NULL) {
734 err = got_error_from_errno("strndup");
735 free(name);
736 goto done;
738 off += s->target_len;
739 remain -= s->target_len;
740 err = got_pathlist_append(symrefs, name, target);
741 if (err) {
742 free(name);
743 free(target);
744 goto done;
747 break;
748 case GOT_IMSG_FETCH_REF:
749 if (datalen <= SHA1_DIGEST_LENGTH) {
750 err = got_error(GOT_ERR_PRIVSEP_MSG);
751 break;
753 *id = malloc(sizeof(**id));
754 if (*id == NULL) {
755 err = got_error_from_errno("malloc");
756 break;
758 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
759 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
760 datalen - SHA1_DIGEST_LENGTH);
761 if (*refname == NULL) {
762 err = got_error_from_errno("strndup");
763 break;
765 break;
766 case GOT_IMSG_FETCH_SERVER_PROGRESS:
767 if (datalen == 0) {
768 err = got_error(GOT_ERR_PRIVSEP_LEN);
769 break;
771 *server_progress = strndup(imsg.data, datalen);
772 if (*server_progress == NULL) {
773 err = got_error_from_errno("strndup");
774 break;
776 for (i = 0; i < datalen; i++) {
777 if (!isprint((unsigned char)(*server_progress)[i]) &&
778 !isspace((unsigned char)(*server_progress)[i])) {
779 err = got_error(GOT_ERR_PRIVSEP_MSG);
780 free(*server_progress);
781 *server_progress = NULL;
782 goto done;
785 break;
786 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
787 if (datalen < sizeof(*packfile_size)) {
788 err = got_error(GOT_ERR_PRIVSEP_MSG);
789 break;
791 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
792 break;
793 case GOT_IMSG_FETCH_DONE:
794 *id = malloc(sizeof(**id));
795 if (*id == NULL) {
796 err = got_error_from_errno("malloc");
797 break;
799 if (datalen != SHA1_DIGEST_LENGTH) {
800 err = got_error(GOT_ERR_PRIVSEP_MSG);
801 break;
803 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
804 *done = 1;
805 break;
806 default:
807 err = got_error(GOT_ERR_PRIVSEP_MSG);
808 break;
810 done:
811 if (err) {
812 free(*id);
813 *id = NULL;
814 free(*refname);
815 *refname = NULL;
817 imsg_free(&imsg);
818 return err;
821 const struct got_error *
822 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
823 int fd)
825 const struct got_error *err = NULL;
827 /* Keep in sync with struct got_imsg_index_pack_request */
828 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
829 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
830 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
831 close(fd);
832 return err;
834 return flush_imsg(ibuf);
837 const struct got_error *
838 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
840 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
843 const struct got_error *
844 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
845 int nobj_indexed, int nobj_loose, int nobj_resolved)
847 struct got_imsg_index_pack_progress iprogress;
849 iprogress.nobj_total = nobj_total;
850 iprogress.nobj_indexed = nobj_indexed;
851 iprogress.nobj_loose = nobj_loose;
852 iprogress.nobj_resolved = nobj_resolved;
854 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
855 &iprogress, sizeof(iprogress)) == -1)
856 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
858 return flush_imsg(ibuf);
861 const struct got_error *
862 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
864 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
865 return got_error_from_errno("imsg_compose FETCH");
866 return flush_imsg(ibuf);
869 const struct got_error *
870 got_privsep_recv_index_progress(int *done, int *nobj_total,
871 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
872 struct imsgbuf *ibuf)
874 const struct got_error *err = NULL;
875 struct imsg imsg;
876 struct got_imsg_index_pack_progress *iprogress;
877 size_t datalen;
879 *done = 0;
880 *nobj_total = 0;
881 *nobj_indexed = 0;
882 *nobj_resolved = 0;
884 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
885 if (err)
886 return err;
888 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
889 switch (imsg.hdr.type) {
890 case GOT_IMSG_ERROR:
891 if (datalen < sizeof(struct got_imsg_error)) {
892 err = got_error(GOT_ERR_PRIVSEP_LEN);
893 break;
895 err = recv_imsg_error(&imsg, datalen);
896 break;
897 case GOT_IMSG_IDXPACK_PROGRESS:
898 if (datalen < sizeof(*iprogress)) {
899 err = got_error(GOT_ERR_PRIVSEP_LEN);
900 break;
902 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
903 *nobj_total = iprogress->nobj_total;
904 *nobj_indexed = iprogress->nobj_indexed;
905 *nobj_loose = iprogress->nobj_loose;
906 *nobj_resolved = iprogress->nobj_resolved;
907 break;
908 case GOT_IMSG_IDXPACK_DONE:
909 if (datalen != 0) {
910 err = got_error(GOT_ERR_PRIVSEP_LEN);
911 break;
913 *done = 1;
914 break;
915 default:
916 err = got_error(GOT_ERR_PRIVSEP_MSG);
917 break;
920 imsg_free(&imsg);
921 return err;
924 const struct got_error *
925 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
926 struct imsgbuf *ibuf)
928 const struct got_error *err = NULL;
929 struct got_imsg_object *iobj;
930 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
932 if (datalen != sizeof(*iobj))
933 return got_error(GOT_ERR_PRIVSEP_LEN);
934 iobj = imsg->data;
936 *obj = calloc(1, sizeof(**obj));
937 if (*obj == NULL)
938 return got_error_from_errno("calloc");
940 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
941 (*obj)->type = iobj->type;
942 (*obj)->flags = iobj->flags;
943 (*obj)->hdrlen = iobj->hdrlen;
944 (*obj)->size = iobj->size;
945 /* path_packfile is handled by caller */
946 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
947 (*obj)->pack_offset = iobj->pack_offset;
948 (*obj)->pack_idx = iobj->pack_idx;
951 return err;
954 const struct got_error *
955 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
957 const struct got_error *err = NULL;
958 struct imsg imsg;
959 const size_t min_datalen =
960 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
962 *obj = NULL;
964 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
965 if (err)
966 return err;
968 switch (imsg.hdr.type) {
969 case GOT_IMSG_OBJECT:
970 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
971 break;
972 default:
973 err = got_error(GOT_ERR_PRIVSEP_MSG);
974 break;
977 imsg_free(&imsg);
979 return err;
982 static const struct got_error *
983 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
984 size_t logmsg_len)
986 const struct got_error *err = NULL;
987 size_t offset, remain;
989 offset = 0;
990 remain = logmsg_len;
991 while (remain > 0) {
992 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
994 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
995 commit->logmsg + offset, n) == -1) {
996 err = got_error_from_errno("imsg_compose "
997 "COMMIT_LOGMSG");
998 break;
1001 err = flush_imsg(ibuf);
1002 if (err)
1003 break;
1005 offset += n;
1006 remain -= n;
1009 return err;
1012 const struct got_error *
1013 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1015 const struct got_error *err = NULL;
1016 struct got_imsg_commit_object *icommit;
1017 uint8_t *buf;
1018 size_t len, total;
1019 struct got_object_qid *qid;
1020 size_t author_len = strlen(commit->author);
1021 size_t committer_len = strlen(commit->committer);
1022 size_t logmsg_len = strlen(commit->logmsg);
1024 total = sizeof(*icommit) + author_len + committer_len +
1025 commit->nparents * SHA1_DIGEST_LENGTH;
1027 buf = malloc(total);
1028 if (buf == NULL)
1029 return got_error_from_errno("malloc");
1031 icommit = (struct got_imsg_commit_object *)buf;
1032 memcpy(icommit->tree_id, commit->tree_id->sha1,
1033 sizeof(icommit->tree_id));
1034 icommit->author_len = author_len;
1035 icommit->author_time = commit->author_time;
1036 icommit->author_gmtoff = commit->author_gmtoff;
1037 icommit->committer_len = committer_len;
1038 icommit->committer_time = commit->committer_time;
1039 icommit->committer_gmtoff = commit->committer_gmtoff;
1040 icommit->logmsg_len = logmsg_len;
1041 icommit->nparents = commit->nparents;
1043 len = sizeof(*icommit);
1044 memcpy(buf + len, commit->author, author_len);
1045 len += author_len;
1046 memcpy(buf + len, commit->committer, committer_len);
1047 len += committer_len;
1048 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1049 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1050 len += SHA1_DIGEST_LENGTH;
1053 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1054 err = got_error_from_errno("imsg_compose COMMIT");
1055 goto done;
1058 if (logmsg_len == 0 ||
1059 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1060 err = flush_imsg(ibuf);
1061 if (err)
1062 goto done;
1064 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1065 done:
1066 free(buf);
1067 return err;
1070 static const struct got_error *
1071 get_commit_from_imsg(struct got_commit_object **commit,
1072 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1074 const struct got_error *err = NULL;
1075 struct got_imsg_commit_object *icommit;
1076 size_t len = 0;
1077 int i;
1079 if (datalen < sizeof(*icommit))
1080 return got_error(GOT_ERR_PRIVSEP_LEN);
1082 icommit = imsg->data;
1083 if (datalen != sizeof(*icommit) + icommit->author_len +
1084 icommit->committer_len +
1085 icommit->nparents * SHA1_DIGEST_LENGTH)
1086 return got_error(GOT_ERR_PRIVSEP_LEN);
1088 if (icommit->nparents < 0)
1089 return got_error(GOT_ERR_PRIVSEP_LEN);
1091 len += sizeof(*icommit);
1093 *commit = got_object_commit_alloc_partial();
1094 if (*commit == NULL)
1095 return got_error_from_errno(
1096 "got_object_commit_alloc_partial");
1098 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1099 SHA1_DIGEST_LENGTH);
1100 (*commit)->author_time = icommit->author_time;
1101 (*commit)->author_gmtoff = icommit->author_gmtoff;
1102 (*commit)->committer_time = icommit->committer_time;
1103 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1105 if (icommit->author_len == 0) {
1106 (*commit)->author = strdup("");
1107 if ((*commit)->author == NULL) {
1108 err = got_error_from_errno("strdup");
1109 goto done;
1111 } else {
1112 (*commit)->author = malloc(icommit->author_len + 1);
1113 if ((*commit)->author == NULL) {
1114 err = got_error_from_errno("malloc");
1115 goto done;
1117 memcpy((*commit)->author, imsg->data + len,
1118 icommit->author_len);
1119 (*commit)->author[icommit->author_len] = '\0';
1121 len += icommit->author_len;
1123 if (icommit->committer_len == 0) {
1124 (*commit)->committer = strdup("");
1125 if ((*commit)->committer == NULL) {
1126 err = got_error_from_errno("strdup");
1127 goto done;
1129 } else {
1130 (*commit)->committer =
1131 malloc(icommit->committer_len + 1);
1132 if ((*commit)->committer == NULL) {
1133 err = got_error_from_errno("malloc");
1134 goto done;
1136 memcpy((*commit)->committer, imsg->data + len,
1137 icommit->committer_len);
1138 (*commit)->committer[icommit->committer_len] = '\0';
1140 len += icommit->committer_len;
1142 if (icommit->logmsg_len == 0) {
1143 (*commit)->logmsg = strdup("");
1144 if ((*commit)->logmsg == NULL) {
1145 err = got_error_from_errno("strdup");
1146 goto done;
1148 } else {
1149 size_t offset = 0, remain = icommit->logmsg_len;
1151 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1152 if ((*commit)->logmsg == NULL) {
1153 err = got_error_from_errno("malloc");
1154 goto done;
1156 while (remain > 0) {
1157 struct imsg imsg_log;
1158 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1159 remain);
1161 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1162 if (err)
1163 goto done;
1165 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1166 err = got_error(GOT_ERR_PRIVSEP_MSG);
1167 goto done;
1170 memcpy((*commit)->logmsg + offset,
1171 imsg_log.data, n);
1172 imsg_free(&imsg_log);
1173 offset += n;
1174 remain -= n;
1176 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1179 for (i = 0; i < icommit->nparents; i++) {
1180 struct got_object_qid *qid;
1182 err = got_object_qid_alloc_partial(&qid);
1183 if (err)
1184 break;
1185 memcpy(qid->id, imsg->data + len +
1186 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1187 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1188 (*commit)->nparents++;
1190 done:
1191 if (err) {
1192 got_object_commit_close(*commit);
1193 *commit = NULL;
1195 return err;
1198 const struct got_error *
1199 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1201 const struct got_error *err = NULL;
1202 struct imsg imsg;
1203 size_t datalen;
1204 const size_t min_datalen =
1205 MIN(sizeof(struct got_imsg_error),
1206 sizeof(struct got_imsg_commit_object));
1208 *commit = NULL;
1210 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1211 if (err)
1212 return err;
1214 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1216 switch (imsg.hdr.type) {
1217 case GOT_IMSG_COMMIT:
1218 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1219 break;
1220 default:
1221 err = got_error(GOT_ERR_PRIVSEP_MSG);
1222 break;
1225 imsg_free(&imsg);
1227 return err;
1230 const struct got_error *
1231 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1232 int nentries)
1234 const struct got_error *err = NULL;
1235 struct got_imsg_tree_object itree;
1236 struct got_pathlist_entry *pe;
1237 size_t totlen;
1238 int nimsg; /* number of imsg queued in ibuf */
1240 itree.nentries = nentries;
1241 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1242 == -1)
1243 return got_error_from_errno("imsg_compose TREE");
1245 totlen = sizeof(itree);
1246 nimsg = 1;
1247 TAILQ_FOREACH(pe, entries, entry) {
1248 const char *name = pe->path;
1249 struct got_parsed_tree_entry *pte = pe->data;
1250 struct ibuf *wbuf;
1251 size_t namelen = strlen(name);
1252 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1254 if (len > MAX_IMSGSIZE)
1255 return got_error(GOT_ERR_NO_SPACE);
1257 nimsg++;
1258 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1259 err = flush_imsg(ibuf);
1260 if (err)
1261 return err;
1262 nimsg = 0;
1265 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1266 if (wbuf == NULL)
1267 return got_error_from_errno("imsg_create TREE_ENTRY");
1269 /* Keep in sync with struct got_imsg_tree_object definition! */
1270 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1271 err = got_error_from_errno("imsg_add TREE_ENTRY");
1272 ibuf_free(wbuf);
1273 return err;
1275 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1276 err = got_error_from_errno("imsg_add TREE_ENTRY");
1277 ibuf_free(wbuf);
1278 return err;
1281 if (imsg_add(wbuf, name, namelen) == -1) {
1282 err = got_error_from_errno("imsg_add TREE_ENTRY");
1283 ibuf_free(wbuf);
1284 return err;
1287 wbuf->fd = -1;
1288 imsg_close(ibuf, wbuf);
1290 totlen += len;
1293 return flush_imsg(ibuf);
1296 const struct got_error *
1297 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1299 const struct got_error *err = NULL;
1300 const size_t min_datalen =
1301 MIN(sizeof(struct got_imsg_error),
1302 sizeof(struct got_imsg_tree_object));
1303 struct got_imsg_tree_object *itree;
1304 int nentries = 0;
1306 *tree = NULL;
1307 get_more:
1308 err = read_imsg(ibuf);
1309 if (err)
1310 goto done;
1312 for (;;) {
1313 struct imsg imsg;
1314 size_t n;
1315 size_t datalen;
1316 struct got_imsg_tree_entry *ite;
1317 struct got_tree_entry *te = NULL;
1319 n = imsg_get(ibuf, &imsg);
1320 if (n == 0) {
1321 if (*tree && (*tree)->nentries != nentries)
1322 goto get_more;
1323 break;
1326 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1327 return got_error(GOT_ERR_PRIVSEP_LEN);
1329 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1331 switch (imsg.hdr.type) {
1332 case GOT_IMSG_ERROR:
1333 err = recv_imsg_error(&imsg, datalen);
1334 break;
1335 case GOT_IMSG_TREE:
1336 /* This message should only appear once. */
1337 if (*tree != NULL) {
1338 err = got_error(GOT_ERR_PRIVSEP_MSG);
1339 break;
1341 if (datalen != sizeof(*itree)) {
1342 err = got_error(GOT_ERR_PRIVSEP_LEN);
1343 break;
1345 itree = imsg.data;
1346 *tree = malloc(sizeof(**tree));
1347 if (*tree == NULL) {
1348 err = got_error_from_errno("malloc");
1349 break;
1351 (*tree)->entries = calloc(itree->nentries,
1352 sizeof(struct got_tree_entry));
1353 if ((*tree)->entries == NULL) {
1354 err = got_error_from_errno("malloc");
1355 break;
1357 (*tree)->nentries = itree->nentries;
1358 (*tree)->refcnt = 0;
1359 break;
1360 case GOT_IMSG_TREE_ENTRY:
1361 /* This message should be preceeded by GOT_IMSG_TREE. */
1362 if (*tree == NULL) {
1363 err = got_error(GOT_ERR_PRIVSEP_MSG);
1364 break;
1366 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1367 err = got_error(GOT_ERR_PRIVSEP_LEN);
1368 break;
1371 /* Remaining data contains the entry's name. */
1372 datalen -= sizeof(*ite);
1373 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1374 err = got_error(GOT_ERR_PRIVSEP_LEN);
1375 break;
1377 ite = imsg.data;
1379 if (datalen + 1 > sizeof(te->name)) {
1380 err = got_error(GOT_ERR_NO_SPACE);
1381 break;
1383 te = &(*tree)->entries[nentries];
1384 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1385 te->name[datalen] = '\0';
1387 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1388 te->mode = ite->mode;
1389 te->idx = nentries;
1390 nentries++;
1391 break;
1392 default:
1393 err = got_error(GOT_ERR_PRIVSEP_MSG);
1394 break;
1397 imsg_free(&imsg);
1399 done:
1400 if (*tree && (*tree)->nentries != nentries) {
1401 if (err == NULL)
1402 err = got_error(GOT_ERR_PRIVSEP_LEN);
1403 got_object_tree_close(*tree);
1404 *tree = NULL;
1407 return err;
1410 const struct got_error *
1411 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1412 const uint8_t *data)
1414 struct got_imsg_blob iblob;
1416 iblob.size = size;
1417 iblob.hdrlen = hdrlen;
1419 if (data) {
1420 uint8_t *buf;
1422 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1423 return got_error(GOT_ERR_NO_SPACE);
1425 buf = malloc(sizeof(iblob) + size);
1426 if (buf == NULL)
1427 return got_error_from_errno("malloc");
1429 memcpy(buf, &iblob, sizeof(iblob));
1430 memcpy(buf + sizeof(iblob), data, size);
1431 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1432 sizeof(iblob) + size) == -1) {
1433 free(buf);
1434 return got_error_from_errno("imsg_compose BLOB");
1436 free(buf);
1437 } else {
1438 /* Data has already been written to file descriptor. */
1439 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1440 sizeof(iblob)) == -1)
1441 return got_error_from_errno("imsg_compose BLOB");
1445 return flush_imsg(ibuf);
1448 const struct got_error *
1449 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1450 struct imsgbuf *ibuf)
1452 const struct got_error *err = NULL;
1453 struct imsg imsg;
1454 struct got_imsg_blob *iblob;
1455 size_t datalen;
1457 *outbuf = NULL;
1459 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1460 if (err)
1461 return err;
1463 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1465 switch (imsg.hdr.type) {
1466 case GOT_IMSG_BLOB:
1467 if (datalen < sizeof(*iblob)) {
1468 err = got_error(GOT_ERR_PRIVSEP_LEN);
1469 break;
1471 iblob = imsg.data;
1472 *size = iblob->size;
1473 *hdrlen = iblob->hdrlen;
1475 if (datalen == sizeof(*iblob)) {
1476 /* Data has been written to file descriptor. */
1477 break;
1480 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1481 err = got_error(GOT_ERR_PRIVSEP_LEN);
1482 break;
1485 *outbuf = malloc(*size);
1486 if (*outbuf == NULL) {
1487 err = got_error_from_errno("malloc");
1488 break;
1490 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1491 break;
1492 default:
1493 err = got_error(GOT_ERR_PRIVSEP_MSG);
1494 break;
1497 imsg_free(&imsg);
1499 return err;
1502 static const struct got_error *
1503 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1505 const struct got_error *err = NULL;
1506 size_t offset, remain;
1508 offset = 0;
1509 remain = tagmsg_len;
1510 while (remain > 0) {
1511 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1513 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1514 tag->tagmsg + offset, n) == -1) {
1515 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1516 break;
1519 err = flush_imsg(ibuf);
1520 if (err)
1521 break;
1523 offset += n;
1524 remain -= n;
1527 return err;
1530 const struct got_error *
1531 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1533 const struct got_error *err = NULL;
1534 struct got_imsg_tag_object *itag;
1535 uint8_t *buf;
1536 size_t len, total;
1537 size_t tag_len = strlen(tag->tag);
1538 size_t tagger_len = strlen(tag->tagger);
1539 size_t tagmsg_len = strlen(tag->tagmsg);
1541 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1543 buf = malloc(total);
1544 if (buf == NULL)
1545 return got_error_from_errno("malloc");
1547 itag = (struct got_imsg_tag_object *)buf;
1548 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1549 itag->obj_type = tag->obj_type;
1550 itag->tag_len = tag_len;
1551 itag->tagger_len = tagger_len;
1552 itag->tagger_time = tag->tagger_time;
1553 itag->tagger_gmtoff = tag->tagger_gmtoff;
1554 itag->tagmsg_len = tagmsg_len;
1556 len = sizeof(*itag);
1557 memcpy(buf + len, tag->tag, tag_len);
1558 len += tag_len;
1559 memcpy(buf + len, tag->tagger, tagger_len);
1560 len += tagger_len;
1562 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1563 err = got_error_from_errno("imsg_compose TAG");
1564 goto done;
1567 if (tagmsg_len == 0 ||
1568 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1569 err = flush_imsg(ibuf);
1570 if (err)
1571 goto done;
1573 err = send_tagmsg(ibuf, tag, tagmsg_len);
1574 done:
1575 free(buf);
1576 return err;
1579 const struct got_error *
1580 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1582 const struct got_error *err = NULL;
1583 struct imsg imsg;
1584 struct got_imsg_tag_object *itag;
1585 size_t len, datalen;
1586 const size_t min_datalen =
1587 MIN(sizeof(struct got_imsg_error),
1588 sizeof(struct got_imsg_tag_object));
1590 *tag = NULL;
1592 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1593 if (err)
1594 return err;
1596 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1597 len = 0;
1599 switch (imsg.hdr.type) {
1600 case GOT_IMSG_TAG:
1601 if (datalen < sizeof(*itag)) {
1602 err = got_error(GOT_ERR_PRIVSEP_LEN);
1603 break;
1605 itag = imsg.data;
1606 if (datalen != sizeof(*itag) + itag->tag_len +
1607 itag->tagger_len) {
1608 err = got_error(GOT_ERR_PRIVSEP_LEN);
1609 break;
1611 len += sizeof(*itag);
1613 *tag = calloc(1, sizeof(**tag));
1614 if (*tag == NULL) {
1615 err = got_error_from_errno("calloc");
1616 break;
1619 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1621 if (itag->tag_len == 0) {
1622 (*tag)->tag = strdup("");
1623 if ((*tag)->tag == NULL) {
1624 err = got_error_from_errno("strdup");
1625 break;
1627 } else {
1628 (*tag)->tag = malloc(itag->tag_len + 1);
1629 if ((*tag)->tag == NULL) {
1630 err = got_error_from_errno("malloc");
1631 break;
1633 memcpy((*tag)->tag, imsg.data + len,
1634 itag->tag_len);
1635 (*tag)->tag[itag->tag_len] = '\0';
1637 len += itag->tag_len;
1639 (*tag)->obj_type = itag->obj_type;
1640 (*tag)->tagger_time = itag->tagger_time;
1641 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1643 if (itag->tagger_len == 0) {
1644 (*tag)->tagger = strdup("");
1645 if ((*tag)->tagger == NULL) {
1646 err = got_error_from_errno("strdup");
1647 break;
1649 } else {
1650 (*tag)->tagger = malloc(itag->tagger_len + 1);
1651 if ((*tag)->tagger == NULL) {
1652 err = got_error_from_errno("malloc");
1653 break;
1655 memcpy((*tag)->tagger, imsg.data + len,
1656 itag->tagger_len);
1657 (*tag)->tagger[itag->tagger_len] = '\0';
1659 len += itag->tagger_len;
1661 if (itag->tagmsg_len == 0) {
1662 (*tag)->tagmsg = strdup("");
1663 if ((*tag)->tagmsg == NULL) {
1664 err = got_error_from_errno("strdup");
1665 break;
1667 } else {
1668 size_t offset = 0, remain = itag->tagmsg_len;
1670 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1671 if ((*tag)->tagmsg == NULL) {
1672 err = got_error_from_errno("malloc");
1673 break;
1675 while (remain > 0) {
1676 struct imsg imsg_log;
1677 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1678 remain);
1680 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1681 if (err)
1682 return err;
1684 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1685 return got_error(GOT_ERR_PRIVSEP_MSG);
1687 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1688 n);
1689 imsg_free(&imsg_log);
1690 offset += n;
1691 remain -= n;
1693 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1696 break;
1697 default:
1698 err = got_error(GOT_ERR_PRIVSEP_MSG);
1699 break;
1702 imsg_free(&imsg);
1704 return err;
1707 const struct got_error *
1708 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1709 struct got_packidx *packidx)
1711 const struct got_error *err = NULL;
1712 struct got_imsg_packidx ipackidx;
1713 struct got_imsg_pack ipack;
1714 int fd;
1716 ipackidx.len = packidx->len;
1717 fd = dup(packidx->fd);
1718 if (fd == -1)
1719 return got_error_from_errno("dup");
1721 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1722 sizeof(ipackidx)) == -1) {
1723 err = got_error_from_errno("imsg_compose PACKIDX");
1724 close(fd);
1725 return err;
1728 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1729 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1730 return got_error(GOT_ERR_NO_SPACE);
1731 ipack.filesize = pack->filesize;
1733 fd = dup(pack->fd);
1734 if (fd == -1)
1735 return got_error_from_errno("dup");
1737 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1738 == -1) {
1739 err = got_error_from_errno("imsg_compose PACK");
1740 close(fd);
1741 return err;
1744 return flush_imsg(ibuf);
1747 const struct got_error *
1748 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1749 struct got_object_id *id)
1751 struct got_imsg_packed_object iobj;
1753 iobj.idx = idx;
1754 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1756 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1757 &iobj, sizeof(iobj)) == -1)
1758 return got_error_from_errno("imsg_compose "
1759 "PACKED_OBJECT_REQUEST");
1761 return flush_imsg(ibuf);
1764 const struct got_error *
1765 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1767 const struct got_error *err = NULL;
1769 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1770 NULL, 0) == -1) {
1771 err = got_error_from_errno("imsg_compose "
1772 "GITCONFIG_PARSE_REQUEST");
1773 close(fd);
1774 return err;
1777 return flush_imsg(ibuf);
1780 const struct got_error *
1781 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1783 if (imsg_compose(ibuf,
1784 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1785 NULL, 0) == -1)
1786 return got_error_from_errno("imsg_compose "
1787 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1789 return flush_imsg(ibuf);
1792 const struct got_error *
1793 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1795 if (imsg_compose(ibuf,
1796 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1797 return got_error_from_errno("imsg_compose "
1798 "GITCONFIG_AUTHOR_NAME_REQUEST");
1800 return flush_imsg(ibuf);
1803 const struct got_error *
1804 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1806 if (imsg_compose(ibuf,
1807 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1808 return got_error_from_errno("imsg_compose "
1809 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1811 return flush_imsg(ibuf);
1814 const struct got_error *
1815 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1817 if (imsg_compose(ibuf,
1818 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1819 return got_error_from_errno("imsg_compose "
1820 "GITCONFIG_REMOTE_REQUEST");
1822 return flush_imsg(ibuf);
1825 const struct got_error *
1826 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1828 if (imsg_compose(ibuf,
1829 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1830 return got_error_from_errno("imsg_compose "
1831 "GITCONFIG_OWNER_REQUEST");
1833 return flush_imsg(ibuf);
1836 const struct got_error *
1837 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1839 size_t len = value ? strlen(value) + 1 : 0;
1841 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1842 value, len) == -1)
1843 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1845 return flush_imsg(ibuf);
1848 const struct got_error *
1849 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1851 const struct got_error *err = NULL;
1852 struct imsg imsg;
1853 size_t datalen;
1854 const size_t min_datalen = 0;
1856 *str = NULL;
1858 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1859 if (err)
1860 return err;
1861 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1863 switch (imsg.hdr.type) {
1864 case GOT_IMSG_GITCONFIG_STR_VAL:
1865 if (datalen == 0)
1866 break;
1867 *str = malloc(datalen);
1868 if (*str == NULL) {
1869 err = got_error_from_errno("malloc");
1870 break;
1872 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1873 err = got_error(GOT_ERR_NO_SPACE);
1874 break;
1875 default:
1876 err = got_error(GOT_ERR_PRIVSEP_MSG);
1877 break;
1880 imsg_free(&imsg);
1881 return err;
1884 const struct got_error *
1885 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1887 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1888 &value, sizeof(value)) == -1)
1889 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1891 return flush_imsg(ibuf);
1894 const struct got_error *
1895 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1897 const struct got_error *err = NULL;
1898 struct imsg imsg;
1899 size_t datalen;
1900 const size_t min_datalen =
1901 MIN(sizeof(struct got_imsg_error), sizeof(int));
1903 *val = 0;
1905 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1906 if (err)
1907 return err;
1908 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1910 switch (imsg.hdr.type) {
1911 case GOT_IMSG_GITCONFIG_INT_VAL:
1912 if (datalen != sizeof(*val)) {
1913 err = got_error(GOT_ERR_PRIVSEP_LEN);
1914 break;
1916 memcpy(val, imsg.data, sizeof(*val));
1917 break;
1918 default:
1919 err = got_error(GOT_ERR_PRIVSEP_MSG);
1920 break;
1923 imsg_free(&imsg);
1924 return err;
1927 const struct got_error *
1928 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1929 struct got_remote_repo *remotes, int nremotes)
1931 const struct got_error *err = NULL;
1932 struct got_imsg_remotes iremotes;
1933 int i;
1935 iremotes.nremotes = nremotes;
1936 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1937 &iremotes, sizeof(iremotes)) == -1)
1938 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1940 err = flush_imsg(ibuf);
1941 imsg_clear(ibuf);
1942 if (err)
1943 return err;
1945 for (i = 0; i < nremotes; i++) {
1946 struct got_imsg_remote iremote;
1947 size_t len = sizeof(iremote);
1948 struct ibuf *wbuf;
1950 iremote.mirror_references = remotes[i].mirror_references;
1951 iremote.name_len = strlen(remotes[i].name);
1952 len += iremote.name_len;
1953 iremote.url_len = strlen(remotes[i].url);
1954 len += iremote.url_len;
1956 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1957 if (wbuf == NULL)
1958 return got_error_from_errno(
1959 "imsg_create GITCONFIG_REMOTE");
1961 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1962 err = got_error_from_errno(
1963 "imsg_add GITCONFIG_REMOTE");
1964 ibuf_free(wbuf);
1965 return err;
1968 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1969 err = got_error_from_errno(
1970 "imsg_add GITCONFIG_REMOTE");
1971 ibuf_free(wbuf);
1972 return err;
1974 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1975 err = got_error_from_errno(
1976 "imsg_add GITCONFIG_REMOTE");
1977 ibuf_free(wbuf);
1978 return err;
1981 wbuf->fd = -1;
1982 imsg_close(ibuf, wbuf);
1983 err = flush_imsg(ibuf);
1984 if (err)
1985 return err;
1988 return NULL;
1991 const struct got_error *
1992 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1993 int *nremotes, struct imsgbuf *ibuf)
1995 const struct got_error *err = NULL;
1996 struct imsg imsg;
1997 size_t datalen;
1998 struct got_imsg_remotes iremotes;
1999 struct got_imsg_remote iremote;
2001 *remotes = NULL;
2002 *nremotes = 0;
2003 iremotes.nremotes = 0;
2005 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2006 if (err)
2007 return err;
2008 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2010 switch (imsg.hdr.type) {
2011 case GOT_IMSG_GITCONFIG_REMOTES:
2012 if (datalen != sizeof(iremotes)) {
2013 err = got_error(GOT_ERR_PRIVSEP_LEN);
2014 break;
2016 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2017 if (iremotes.nremotes == 0) {
2018 imsg_free(&imsg);
2019 return NULL;
2021 break;
2022 default:
2023 imsg_free(&imsg);
2024 return got_error(GOT_ERR_PRIVSEP_MSG);
2027 imsg_free(&imsg);
2029 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2030 if (*remotes == NULL)
2031 return got_error_from_errno("recallocarray");
2033 while (*nremotes < iremotes.nremotes) {
2034 struct got_remote_repo *remote;
2036 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2037 if (err)
2038 break;
2039 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2041 switch (imsg.hdr.type) {
2042 case GOT_IMSG_GITCONFIG_REMOTE:
2043 remote = &(*remotes)[*nremotes];
2044 if (datalen < sizeof(iremote)) {
2045 err = got_error(GOT_ERR_PRIVSEP_LEN);
2046 break;
2048 memcpy(&iremote, imsg.data, sizeof(iremote));
2049 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2050 (sizeof(iremote) + iremote.name_len +
2051 iremote.url_len) > datalen) {
2052 err = got_error(GOT_ERR_PRIVSEP_LEN);
2053 break;
2055 remote->name = strndup(imsg.data + sizeof(iremote),
2056 iremote.name_len);
2057 if (remote->name == NULL) {
2058 err = got_error_from_errno("strndup");
2059 break;
2061 remote->url = strndup(imsg.data + sizeof(iremote) +
2062 iremote.name_len, iremote.url_len);
2063 if (remote->url == NULL) {
2064 err = got_error_from_errno("strndup");
2065 free(remote->name);
2066 break;
2068 remote->mirror_references = iremote.mirror_references;
2069 (*nremotes)++;
2070 break;
2071 default:
2072 err = got_error(GOT_ERR_PRIVSEP_MSG);
2073 break;
2076 imsg_free(&imsg);
2077 if (err)
2078 break;
2081 if (err) {
2082 int i;
2083 for (i = 0; i < *nremotes; i++) {
2084 free((*remotes)[i].name);
2085 free((*remotes)[i].url);
2087 free(*remotes);
2088 *remotes = NULL;
2089 *nremotes = 0;
2091 return err;
2094 const struct got_error *
2095 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2096 struct got_object_id *id, int idx, const char *path)
2098 const struct got_error *err = NULL;
2099 struct ibuf *wbuf;
2100 size_t path_len = strlen(path) + 1;
2102 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2103 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2104 if (wbuf == NULL)
2105 return got_error_from_errno(
2106 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2107 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2108 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2109 ibuf_free(wbuf);
2110 return err;
2112 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2113 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2114 ibuf_free(wbuf);
2115 return err;
2117 if (imsg_add(wbuf, path, path_len) == -1) {
2118 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2119 ibuf_free(wbuf);
2120 return err;
2123 wbuf->fd = -1;
2124 imsg_close(ibuf, wbuf);
2126 return flush_imsg(ibuf);
2129 const struct got_error *
2130 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2131 size_t ncommits, struct imsgbuf *ibuf)
2133 const struct got_error *err;
2134 struct ibuf *wbuf;
2135 int i;
2137 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2138 sizeof(struct got_imsg_traversed_commits) +
2139 ncommits * SHA1_DIGEST_LENGTH);
2140 if (wbuf == NULL)
2141 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2143 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2144 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2145 ibuf_free(wbuf);
2146 return err;
2148 for (i = 0; i < ncommits; i++) {
2149 struct got_object_id *id = &commit_ids[i];
2150 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2151 err = got_error_from_errno(
2152 "imsg_add TRAVERSED_COMMITS");
2153 ibuf_free(wbuf);
2154 return err;
2158 wbuf->fd = -1;
2159 imsg_close(ibuf, wbuf);
2161 return flush_imsg(ibuf);
2164 const struct got_error *
2165 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2166 struct got_object_id **changed_commit_id,
2167 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2169 const struct got_error *err = NULL;
2170 struct imsg imsg;
2171 struct got_imsg_traversed_commits *icommits;
2172 size_t datalen;
2173 int i, done = 0;
2175 *changed_commit = NULL;
2176 *changed_commit_id = NULL;
2178 while (!done) {
2179 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2180 if (err)
2181 return err;
2183 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2184 switch (imsg.hdr.type) {
2185 case GOT_IMSG_TRAVERSED_COMMITS:
2186 icommits = imsg.data;
2187 if (datalen != sizeof(*icommits) +
2188 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2189 err = got_error(GOT_ERR_PRIVSEP_LEN);
2190 break;
2192 for (i = 0; i < icommits->ncommits; i++) {
2193 struct got_object_qid *qid;
2194 uint8_t *sha1 = (uint8_t *)imsg.data +
2195 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2196 err = got_object_qid_alloc_partial(&qid);
2197 if (err)
2198 break;
2199 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2200 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2202 /* The last commit may contain a change. */
2203 if (i == icommits->ncommits - 1) {
2204 *changed_commit_id =
2205 got_object_id_dup(qid->id);
2206 if (*changed_commit_id == NULL) {
2207 err = got_error_from_errno(
2208 "got_object_id_dup");
2209 break;
2213 break;
2214 case GOT_IMSG_COMMIT:
2215 if (*changed_commit_id == NULL) {
2216 err = got_error(GOT_ERR_PRIVSEP_MSG);
2217 break;
2219 err = get_commit_from_imsg(changed_commit, &imsg,
2220 datalen, ibuf);
2221 break;
2222 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2223 done = 1;
2224 break;
2225 default:
2226 err = got_error(GOT_ERR_PRIVSEP_MSG);
2227 break;
2230 imsg_free(&imsg);
2231 if (err)
2232 break;
2235 if (err)
2236 got_object_id_queue_free(commit_ids);
2237 return err;
2240 const struct got_error *
2241 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2243 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2244 NULL, 0) == -1)
2245 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2247 return flush_imsg(ibuf);
2250 const struct got_error *
2251 got_privsep_unveil_exec_helpers(void)
2253 const char *helpers[] = {
2254 GOT_PATH_PROG_READ_PACK,
2255 GOT_PATH_PROG_READ_OBJECT,
2256 GOT_PATH_PROG_READ_COMMIT,
2257 GOT_PATH_PROG_READ_TREE,
2258 GOT_PATH_PROG_READ_BLOB,
2259 GOT_PATH_PROG_READ_TAG,
2260 GOT_PATH_PROG_READ_GITCONFIG,
2261 GOT_PATH_PROG_FETCH_PACK,
2262 GOT_PATH_PROG_INDEX_PACK,
2264 int i;
2266 for (i = 0; i < nitems(helpers); i++) {
2267 if (unveil(helpers[i], "x") == 0)
2268 continue;
2269 return got_error_from_errno2("unveil", helpers[i]);
2272 return NULL;
2275 void
2276 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2278 if (close(imsg_fds[0]) != 0) {
2279 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2280 _exit(1);
2283 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2284 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2285 _exit(1);
2287 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2288 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2289 _exit(1);
2292 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2293 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2294 strerror(errno));
2295 _exit(1);