portable: configure: use config.h
[got-portable.git] / lib / privsep.c
blob57c7e0e532d44492699622e9ff0ff3feab6aee72
1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
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.
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.
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/wait.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdint.h>
31 #include <poll.h>
32 #include <imsg.h>
33 #include <unistd.h>
34 #include <zlib.h>
36 #include "got_compat.h"
38 #include "got_object.h"
39 #include "got_error.h"
40 #include "got_path.h"
41 #include "got_repository.h"
43 #include "got_lib_hash.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_poll.h"
52 #include "got_privsep.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 #ifndef nitems
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 #endif
62 static const struct got_error *
63 read_imsg(struct imsgbuf *ibuf)
65 const struct got_error *err;
66 size_t n;
68 err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
69 if (err) {
70 if (err->code == GOT_ERR_EOF)
71 return got_error(GOT_ERR_PRIVSEP_PIPE);
72 return err;
75 n = imsg_read(ibuf);
76 if (n == -1) {
77 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
78 return got_error(GOT_ERR_PRIVSEP_NO_FD);
79 return got_error(GOT_ERR_PRIVSEP_READ);
81 if (n == 0)
82 return got_error(GOT_ERR_PRIVSEP_PIPE);
84 return NULL;
87 const struct got_error *
88 got_privsep_wait_for_child(pid_t pid)
90 int child_status;
92 if (waitpid(pid, &child_status, 0) == -1)
93 return got_error_from_errno("waitpid");
95 if (!WIFEXITED(child_status))
96 return got_error(GOT_ERR_PRIVSEP_DIED);
98 if (WEXITSTATUS(child_status) != 0)
99 return got_error(GOT_ERR_PRIVSEP_EXIT);
101 return NULL;
104 static const struct got_error *
105 recv_imsg_error(struct imsg *imsg, size_t datalen)
107 struct got_imsg_error *ierr;
109 if (datalen != sizeof(*ierr))
110 return got_error(GOT_ERR_PRIVSEP_LEN);
112 ierr = imsg->data;
113 if (ierr->code == GOT_ERR_ERRNO) {
114 static struct got_error serr;
115 serr.code = GOT_ERR_ERRNO;
116 serr.msg = strerror(ierr->errno_code);
117 return &serr;
120 return got_error(ierr->code);
123 const struct got_error *
124 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
125 size_t min_datalen)
127 const struct got_error *err;
128 ssize_t n;
130 n = imsg_get(ibuf, imsg);
131 if (n == -1)
132 return got_error_from_errno("imsg_get");
134 while (n == 0) {
135 err = read_imsg(ibuf);
136 if (err)
137 return err;
138 n = imsg_get(ibuf, imsg);
139 if (n == -1)
140 return got_error_from_errno("imsg_get");
143 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
144 return got_error(GOT_ERR_PRIVSEP_LEN);
146 if (imsg->hdr.type == GOT_IMSG_ERROR) {
147 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
148 return recv_imsg_error(imsg, datalen);
151 return NULL;
154 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
155 void
156 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
158 const struct got_error *poll_err;
159 struct got_imsg_error ierr;
160 int ret;
162 ierr.code = err->code;
163 if (err->code == GOT_ERR_ERRNO)
164 ierr.errno_code = errno;
165 else
166 ierr.errno_code = 0;
167 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
168 if (ret == -1) {
169 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
170 getprogname(), err->code, err->msg, strerror(errno));
171 return;
174 poll_err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
175 if (poll_err) {
176 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
177 getprogname(), err->code, err->msg, poll_err->msg);
178 return;
181 ret = imsg_flush(ibuf);
182 if (ret == -1) {
183 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
184 getprogname(), err->code, err->msg, strerror(errno));
185 imsg_clear(ibuf);
186 return;
190 static const struct got_error *
191 flush_imsg(struct imsgbuf *ibuf)
193 const struct got_error *err;
195 err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
196 if (err)
197 return err;
199 if (imsg_flush(ibuf) == -1) {
200 imsg_clear(ibuf);
201 return got_error_from_errno("imsg_flush");
204 return NULL;
207 const struct got_error *
208 got_privsep_flush_imsg(struct imsgbuf *ibuf)
210 return flush_imsg(ibuf);
213 const struct got_error *
214 got_privsep_send_stop(int fd)
216 const struct got_error *err = NULL;
217 struct imsgbuf ibuf;
219 imsg_init(&ibuf, fd);
221 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
222 return got_error_from_errno("imsg_compose STOP");
224 err = flush_imsg(&ibuf);
225 return err;
228 const struct got_error *
229 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
230 struct got_object_id *id)
232 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
233 id, sizeof(*id)) == -1)
234 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
236 return flush_imsg(ibuf);
239 const struct got_error *
240 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
241 struct got_object_id *id)
243 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
244 id, sizeof(*id)) == -1)
245 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
247 return flush_imsg(ibuf);
250 const struct got_error *
251 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
253 const struct got_error *err = NULL;
255 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
256 == -1) {
257 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
258 close(outfd);
259 return err;
262 return flush_imsg(ibuf);
265 const struct got_error *
266 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
267 uint8_t *data)
269 const struct got_error *err = NULL;
270 struct got_imsg_raw_obj iobj;
271 size_t len = sizeof(iobj);
272 struct ibuf *wbuf;
274 memset(&iobj, 0, sizeof(iobj));
275 iobj.hdrlen = hdrlen;
276 iobj.size = size;
278 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
279 len += (size_t)size + hdrlen;
281 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
282 if (wbuf == NULL) {
283 err = got_error_from_errno("imsg_create RAW_OBJECT");
284 return err;
287 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
288 return got_error_from_errno("imsg_add RAW_OBJECT");
290 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
291 if (imsg_add(wbuf, data, size + hdrlen) == -1)
292 return got_error_from_errno("imsg_add RAW_OBJECT");
295 wbuf->fd = -1;
296 imsg_close(ibuf, wbuf);
298 return flush_imsg(ibuf);
301 const struct got_error *
302 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
303 struct imsgbuf *ibuf)
305 const struct got_error *err = NULL;
306 struct imsg imsg;
307 struct got_imsg_raw_obj *iobj;
308 size_t datalen;
310 *outbuf = NULL;
312 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
313 if (err)
314 return err;
316 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
318 switch (imsg.hdr.type) {
319 case GOT_IMSG_RAW_OBJECT:
320 if (datalen < sizeof(*iobj)) {
321 err = got_error(GOT_ERR_PRIVSEP_LEN);
322 break;
324 iobj = imsg.data;
325 *size = iobj->size;
326 *hdrlen = iobj->hdrlen;
328 if (datalen == sizeof(*iobj)) {
329 /* Data has been written to file descriptor. */
330 break;
333 if (*size < 0 ||
334 *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
335 err = got_error(GOT_ERR_PRIVSEP_LEN);
336 break;
339 *outbuf = malloc(*size + *hdrlen);
340 if (*outbuf == NULL) {
341 err = got_error_from_errno("malloc");
342 break;
344 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
345 break;
346 default:
347 err = got_error(GOT_ERR_PRIVSEP_MSG);
348 break;
351 imsg_free(&imsg);
353 return err;
356 const struct got_error *
357 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
358 struct got_object_id *id, int pack_idx)
360 const struct got_error *err = NULL;
361 struct got_imsg_packed_object iobj;
362 void *data;
363 size_t len;
365 memset(&iobj, 0, sizeof(iobj));
366 if (pack_idx != -1) { /* commit is packed */
367 iobj.idx = pack_idx;
368 memcpy(&iobj.id, id, sizeof(iobj.id));
369 data = &iobj;
370 len = sizeof(iobj);
371 } else {
372 data = id;
373 len = sizeof(*id);
376 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
377 == -1) {
378 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
379 close(fd);
380 return err;
383 return flush_imsg(ibuf);
386 const struct got_error *
387 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
388 struct got_object_id *id, int pack_idx)
390 struct ibuf *wbuf;
391 size_t len;
393 if (pack_idx != -1)
394 len = sizeof(struct got_imsg_packed_object);
395 else
396 len = sizeof(*id);
398 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
399 if (wbuf == NULL)
400 return got_error_from_errno("imsg_create TREE_REQUEST");
402 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
403 return got_error_from_errno("imsg_add TREE_REQUEST");
405 if (pack_idx != -1) { /* tree is packed */
406 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
407 return got_error_from_errno("imsg_add TREE_REQUEST");
410 wbuf->fd = fd;
411 imsg_close(ibuf, wbuf);
413 return flush_imsg(ibuf);
416 const struct got_error *
417 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
418 struct got_object_id *id, int pack_idx)
420 struct got_imsg_packed_object iobj;
421 void *data;
422 size_t len;
424 memset(&iobj, 0, sizeof(iobj));
425 if (pack_idx != -1) { /* tag is packed */
426 iobj.idx = pack_idx;
427 memcpy(&iobj.id, id, sizeof(iobj.id));
428 data = &iobj;
429 len = sizeof(iobj);
430 } else {
431 data = id;
432 len = sizeof(*id);
435 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
436 == -1)
437 return got_error_from_errno("imsg_compose TAG_REQUEST");
439 return flush_imsg(ibuf);
442 const struct got_error *
443 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
444 struct got_object_id *id, int pack_idx)
446 const struct got_error *err = NULL;
447 struct got_imsg_packed_object iobj;
448 void *data;
449 size_t len;
451 memset(&iobj, 0, sizeof(iobj));
452 if (pack_idx != -1) { /* blob is packed */
453 iobj.idx = pack_idx;
454 memcpy(&iobj.id, id, sizeof(iobj.id));
455 data = &iobj;
456 len = sizeof(iobj);
457 } else {
458 data = id;
459 len = sizeof(*id);
462 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
463 == -1) {
464 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
465 close(infd);
466 return err;
469 return flush_imsg(ibuf);
472 const struct got_error *
473 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
475 const struct got_error *err = NULL;
477 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
478 == -1) {
479 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
480 close(outfd);
481 return err;
484 return flush_imsg(ibuf);
487 static const struct got_error *
488 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
490 const struct got_error *err = NULL;
492 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
493 err = got_error_from_errno("imsg_compose TMPFD");
494 close(fd);
495 return err;
498 return flush_imsg(ibuf);
501 const struct got_error *
502 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
504 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
507 const struct got_error *
508 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
510 struct got_imsg_object iobj;
512 memset(&iobj, 0, sizeof(iobj));
514 memcpy(&iobj.id, &obj->id, sizeof(iobj.id));
515 iobj.type = obj->type;
516 iobj.flags = obj->flags;
517 iobj.hdrlen = obj->hdrlen;
518 iobj.size = obj->size;
519 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
520 iobj.pack_offset = obj->pack_offset;
521 iobj.pack_idx = obj->pack_idx;
524 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
525 == -1)
526 return got_error_from_errno("imsg_compose OBJECT");
528 return flush_imsg(ibuf);
531 const struct got_error *
532 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
533 struct got_pathlist_head *have_refs, int fetch_all_branches,
534 struct got_pathlist_head *wanted_branches,
535 struct got_pathlist_head *wanted_refs, int list_refs_only,
536 const char *worktree_branch, const char *remote_head,
537 int no_head, int verbosity)
539 const struct got_error *err = NULL;
540 struct ibuf *wbuf;
541 struct got_pathlist_entry *pe;
542 struct got_imsg_fetch_request fetchreq;
543 size_t remote_head_len, worktree_branch_len, len = sizeof(fetchreq);
545 if (worktree_branch) {
546 worktree_branch_len = strlen(worktree_branch);
547 len += worktree_branch_len;
549 if (remote_head) {
550 remote_head_len = strlen(remote_head);
551 len += remote_head_len;
554 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
555 close(fd);
556 return got_error(GOT_ERR_NO_SPACE);
559 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
560 if (wbuf == NULL)
561 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
563 memset(&fetchreq, 0, sizeof(fetchreq));
564 fetchreq.no_head = no_head;
565 fetchreq.fetch_all_branches = fetch_all_branches;
566 fetchreq.list_refs_only = list_refs_only;
567 fetchreq.verbosity = verbosity;
568 if (worktree_branch != NULL)
569 fetchreq.worktree_branch_len = worktree_branch_len;
570 if (remote_head != NULL)
571 fetchreq.remote_head_len = remote_head_len;
572 TAILQ_FOREACH(pe, have_refs, entry)
573 fetchreq.n_have_refs++;
574 TAILQ_FOREACH(pe, wanted_branches, entry)
575 fetchreq.n_wanted_branches++;
576 TAILQ_FOREACH(pe, wanted_refs, entry)
577 fetchreq.n_wanted_refs++;
578 if (imsg_add(wbuf, &fetchreq, sizeof(fetchreq)) == -1)
579 return got_error_from_errno("imsg_add FETCH_REQUEST");
580 if (worktree_branch) {
581 if (imsg_add(wbuf, worktree_branch, worktree_branch_len) == -1)
582 return got_error_from_errno("imsg_add FETCH_REQUEST");
584 if (remote_head) {
585 if (imsg_add(wbuf, remote_head, remote_head_len) == -1)
586 return got_error_from_errno("imsg_add FETCH_REQUEST");
588 wbuf->fd = fd;
589 imsg_close(ibuf, wbuf);
591 err = flush_imsg(ibuf);
592 if (err) {
593 close(fd);
594 return err;
596 fd = -1;
598 TAILQ_FOREACH(pe, have_refs, entry) {
599 const char *name = pe->path;
600 size_t name_len = pe->path_len;
601 struct got_object_id *id = pe->data;
603 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
604 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
605 if (wbuf == NULL)
606 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
608 /* Keep in sync with struct got_imsg_fetch_have_ref! */
609 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
610 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
611 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
612 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
613 if (imsg_add(wbuf, name, name_len) == -1)
614 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
616 wbuf->fd = -1;
617 imsg_close(ibuf, wbuf);
618 err = flush_imsg(ibuf);
619 if (err)
620 return err;
623 TAILQ_FOREACH(pe, wanted_branches, entry) {
624 const char *name = pe->path;
625 size_t name_len = pe->path_len;
627 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
628 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
629 len);
630 if (wbuf == NULL)
631 return got_error_from_errno(
632 "imsg_create FETCH_WANTED_BRANCH");
634 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
635 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
636 return got_error_from_errno(
637 "imsg_add FETCH_WANTED_BRANCH");
638 if (imsg_add(wbuf, name, name_len) == -1)
639 return got_error_from_errno(
640 "imsg_add FETCH_WANTED_BRANCH");
642 wbuf->fd = -1;
643 imsg_close(ibuf, wbuf);
644 err = flush_imsg(ibuf);
645 if (err)
646 return err;
649 TAILQ_FOREACH(pe, wanted_refs, entry) {
650 const char *name = pe->path;
651 size_t name_len = pe->path_len;
653 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
654 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
655 len);
656 if (wbuf == NULL)
657 return got_error_from_errno(
658 "imsg_create FETCH_WANTED_REF");
660 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
661 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
662 return got_error_from_errno(
663 "imsg_add FETCH_WANTED_REF");
664 if (imsg_add(wbuf, name, name_len) == -1)
665 return got_error_from_errno(
666 "imsg_add FETCH_WANTED_REF");
668 wbuf->fd = -1;
669 imsg_close(ibuf, wbuf);
670 err = flush_imsg(ibuf);
671 if (err)
672 return err;
676 return NULL;
680 const struct got_error *
681 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
683 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
686 const struct got_error *
687 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
688 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
689 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
691 const struct got_error *err = NULL;
692 struct imsg imsg;
693 size_t datalen;
694 struct got_imsg_fetch_symrefs *isymrefs = NULL;
695 size_t n, remain;
696 off_t off;
697 int i;
699 *done = 0;
700 *id = NULL;
701 *refname = NULL;
702 *server_progress = NULL;
703 *packfile_size = 0;
704 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
706 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
707 if (err)
708 return err;
710 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
711 switch (imsg.hdr.type) {
712 case GOT_IMSG_FETCH_SYMREFS:
713 if (datalen < sizeof(*isymrefs)) {
714 err = got_error(GOT_ERR_PRIVSEP_LEN);
715 break;
717 if (isymrefs != NULL) {
718 err = got_error(GOT_ERR_PRIVSEP_MSG);
719 break;
721 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
722 off = sizeof(*isymrefs);
723 remain = datalen - off;
724 for (n = 0; n < isymrefs->nsymrefs; n++) {
725 struct got_imsg_fetch_symref *s;
726 char *name, *target;
727 if (remain < sizeof(struct got_imsg_fetch_symref)) {
728 err = got_error(GOT_ERR_PRIVSEP_LEN);
729 goto done;
731 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
732 off += sizeof(*s);
733 remain -= sizeof(*s);
734 if (remain < s->name_len) {
735 err = got_error(GOT_ERR_PRIVSEP_LEN);
736 goto done;
738 name = strndup(imsg.data + off, s->name_len);
739 if (name == NULL) {
740 err = got_error_from_errno("strndup");
741 goto done;
743 off += s->name_len;
744 remain -= s->name_len;
745 if (remain < s->target_len) {
746 err = got_error(GOT_ERR_PRIVSEP_LEN);
747 free(name);
748 goto done;
750 target = strndup(imsg.data + off, s->target_len);
751 if (target == NULL) {
752 err = got_error_from_errno("strndup");
753 free(name);
754 goto done;
756 off += s->target_len;
757 remain -= s->target_len;
758 err = got_pathlist_append(symrefs, name, target);
759 if (err) {
760 free(name);
761 free(target);
762 goto done;
765 break;
766 case GOT_IMSG_FETCH_REF:
767 if (datalen <= sizeof(**id)) {
768 err = got_error(GOT_ERR_PRIVSEP_MSG);
769 break;
771 *id = malloc(sizeof(**id));
772 if (*id == NULL) {
773 err = got_error_from_errno("malloc");
774 break;
776 memcpy(*id, imsg.data, sizeof(**id));
777 *refname = strndup(imsg.data + sizeof(**id),
778 datalen - sizeof(**id));
779 if (*refname == NULL) {
780 err = got_error_from_errno("strndup");
781 break;
783 break;
784 case GOT_IMSG_FETCH_SERVER_PROGRESS:
785 if (datalen == 0) {
786 err = got_error(GOT_ERR_PRIVSEP_LEN);
787 break;
789 *server_progress = strndup(imsg.data, datalen);
790 if (*server_progress == NULL) {
791 err = got_error_from_errno("strndup");
792 break;
794 for (i = 0; i < datalen; i++) {
795 if (!isprint((unsigned char)(*server_progress)[i]) &&
796 !isspace((unsigned char)(*server_progress)[i])) {
797 err = got_error(GOT_ERR_PRIVSEP_MSG);
798 free(*server_progress);
799 *server_progress = NULL;
800 goto done;
803 break;
804 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
805 if (datalen < sizeof(*packfile_size)) {
806 err = got_error(GOT_ERR_PRIVSEP_MSG);
807 break;
809 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
810 break;
811 case GOT_IMSG_FETCH_DONE:
812 if (datalen != SHA1_DIGEST_LENGTH) {
813 err = got_error(GOT_ERR_PRIVSEP_MSG);
814 break;
816 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
817 *done = 1;
818 break;
819 default:
820 err = got_error(GOT_ERR_PRIVSEP_MSG);
821 break;
823 done:
824 if (err) {
825 free(*id);
826 *id = NULL;
827 free(*refname);
828 *refname = NULL;
830 imsg_free(&imsg);
831 return err;
834 static const struct got_error *
835 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
836 int delete, struct imsgbuf *ibuf)
838 size_t len;
839 struct ibuf *wbuf;
841 len = sizeof(struct got_imsg_send_ref) + name_len;
842 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
843 if (wbuf == NULL)
844 return got_error_from_errno("imsg_create SEND_REF");
846 /* Keep in sync with struct got_imsg_send_ref! */
847 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
848 return got_error_from_errno("imsg_add SEND_REF");
849 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
850 return got_error_from_errno("imsg_add SEND_REF");
851 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
852 return got_error_from_errno("imsg_add SEND_REF");
853 if (imsg_add(wbuf, name, name_len) == -1)
854 return got_error_from_errno("imsg_add SEND_REF");
856 wbuf->fd = -1;
857 imsg_close(ibuf, wbuf);
858 return flush_imsg(ibuf);
861 const struct got_error *
862 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
863 struct got_pathlist_head *have_refs,
864 struct got_pathlist_head *delete_refs,
865 int verbosity)
867 const struct got_error *err = NULL;
868 struct got_pathlist_entry *pe;
869 struct got_imsg_send_request sendreq;
870 struct got_object_id zero_id;
872 memset(&zero_id, 0, sizeof(zero_id));
873 memset(&sendreq, 0, sizeof(sendreq));
874 sendreq.verbosity = verbosity;
875 TAILQ_FOREACH(pe, have_refs, entry)
876 sendreq.nrefs++;
877 TAILQ_FOREACH(pe, delete_refs, entry)
878 sendreq.nrefs++;
879 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
880 &sendreq, sizeof(sendreq)) == -1) {
881 err = got_error_from_errno(
882 "imsg_compose FETCH_SERVER_PROGRESS");
883 goto done;
886 err = flush_imsg(ibuf);
887 if (err)
888 goto done;
889 fd = -1;
891 TAILQ_FOREACH(pe, have_refs, entry) {
892 const char *name = pe->path;
893 size_t name_len = pe->path_len;
894 struct got_object_id *id = pe->data;
895 err = send_send_ref(name, name_len, id, 0, ibuf);
896 if (err)
897 goto done;
900 TAILQ_FOREACH(pe, delete_refs, entry) {
901 const char *name = pe->path;
902 size_t name_len = pe->path_len;
903 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
904 if (err)
905 goto done;
907 done:
908 if (fd != -1 && close(fd) == -1 && err == NULL)
909 err = got_error_from_errno("close");
910 return err;
914 const struct got_error *
915 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
916 struct imsgbuf *ibuf)
918 const struct got_error *err = NULL;
919 struct imsg imsg;
920 size_t datalen;
921 int done = 0;
922 struct got_imsg_send_remote_ref iremote_ref;
923 struct got_object_id *id = NULL;
924 char *refname = NULL;
925 struct got_pathlist_entry *new;
927 while (!done) {
928 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
929 if (err)
930 return err;
931 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
932 switch (imsg.hdr.type) {
933 case GOT_IMSG_SEND_REMOTE_REF:
934 if (datalen < sizeof(iremote_ref)) {
935 err = got_error(GOT_ERR_PRIVSEP_MSG);
936 goto done;
938 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
939 if (datalen != sizeof(iremote_ref) +
940 iremote_ref.name_len) {
941 err = got_error(GOT_ERR_PRIVSEP_MSG);
942 goto done;
944 id = malloc(sizeof(*id));
945 if (id == NULL) {
946 err = got_error_from_errno("malloc");
947 goto done;
949 memcpy(id, &iremote_ref.id, sizeof(*id));
950 refname = strndup(imsg.data + sizeof(iremote_ref),
951 datalen - sizeof(iremote_ref));
952 if (refname == NULL) {
953 err = got_error_from_errno("strndup");
954 goto done;
956 err = got_pathlist_insert(&new, remote_refs,
957 refname, id);
958 if (err)
959 goto done;
960 if (new == NULL) { /* duplicate which wasn't inserted */
961 free(id);
962 free(refname);
964 id = NULL;
965 refname = NULL;
966 break;
967 case GOT_IMSG_SEND_PACK_REQUEST:
968 if (datalen != 0) {
969 err = got_error(GOT_ERR_PRIVSEP_MSG);
970 goto done;
972 /* got-send-pack is now waiting for a pack file. */
973 done = 1;
974 break;
975 default:
976 err = got_error(GOT_ERR_PRIVSEP_MSG);
977 break;
980 done:
981 free(id);
982 free(refname);
983 imsg_free(&imsg);
984 return err;
987 const struct got_error *
988 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
990 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
993 const struct got_error *
994 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
995 int *success, char **refname, char **errmsg, struct imsgbuf *ibuf)
997 const struct got_error *err = NULL;
998 struct imsg imsg;
999 size_t datalen;
1000 struct got_imsg_send_ref_status iref_status;
1002 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1003 *done = 0;
1004 *success = 0;
1005 *refname = NULL;
1006 *errmsg = NULL;
1008 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1009 if (err)
1010 return err;
1012 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1013 switch (imsg.hdr.type) {
1014 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1015 if (datalen < sizeof(*bytes_sent)) {
1016 err = got_error(GOT_ERR_PRIVSEP_MSG);
1017 break;
1019 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1020 break;
1021 case GOT_IMSG_SEND_REF_STATUS:
1022 if (datalen < sizeof(iref_status)) {
1023 err = got_error(GOT_ERR_PRIVSEP_MSG);
1024 break;
1026 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1027 if (datalen != sizeof(iref_status) + iref_status.name_len +
1028 iref_status.errmsg_len) {
1029 err = got_error(GOT_ERR_PRIVSEP_MSG);
1030 break;
1032 *success = iref_status.success;
1033 *refname = strndup(imsg.data + sizeof(iref_status),
1034 iref_status.name_len);
1036 if (iref_status.errmsg_len != 0)
1037 *errmsg = strndup(imsg.data + sizeof(iref_status) +
1038 iref_status.name_len, iref_status.errmsg_len);
1039 break;
1040 case GOT_IMSG_SEND_DONE:
1041 if (datalen != 0) {
1042 err = got_error(GOT_ERR_PRIVSEP_MSG);
1043 break;
1045 *done = 1;
1046 break;
1047 default:
1048 err = got_error(GOT_ERR_PRIVSEP_MSG);
1049 break;
1052 imsg_free(&imsg);
1053 return err;
1056 const struct got_error *
1057 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1058 int fd)
1060 const struct got_error *err = NULL;
1062 /* Keep in sync with struct got_imsg_index_pack_request */
1063 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1064 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1065 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1066 close(fd);
1067 return err;
1069 return flush_imsg(ibuf);
1072 const struct got_error *
1073 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1075 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1078 const struct got_error *
1079 got_privsep_recv_index_progress(int *done, int *nobj_total,
1080 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1081 struct imsgbuf *ibuf)
1083 const struct got_error *err = NULL;
1084 struct imsg imsg;
1085 struct got_imsg_index_pack_progress *iprogress;
1086 size_t datalen;
1088 *done = 0;
1089 *nobj_total = 0;
1090 *nobj_indexed = 0;
1091 *nobj_resolved = 0;
1093 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1094 if (err)
1095 return err;
1097 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1098 switch (imsg.hdr.type) {
1099 case GOT_IMSG_IDXPACK_PROGRESS:
1100 if (datalen < sizeof(*iprogress)) {
1101 err = got_error(GOT_ERR_PRIVSEP_LEN);
1102 break;
1104 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1105 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1106 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1107 err = got_error(GOT_ERR_RANGE);
1108 break;
1110 *nobj_total = iprogress->nobj_total;
1111 *nobj_indexed = iprogress->nobj_indexed;
1112 *nobj_loose = iprogress->nobj_loose;
1113 *nobj_resolved = iprogress->nobj_resolved;
1114 break;
1115 case GOT_IMSG_IDXPACK_DONE:
1116 if (datalen != 0) {
1117 err = got_error(GOT_ERR_PRIVSEP_LEN);
1118 break;
1120 *done = 1;
1121 break;
1122 default:
1123 err = got_error(GOT_ERR_PRIVSEP_MSG);
1124 break;
1127 imsg_free(&imsg);
1128 return err;
1131 const struct got_error *
1132 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1133 struct imsgbuf *ibuf)
1135 struct got_imsg_object *iobj;
1136 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1138 if (datalen != sizeof(*iobj))
1139 return got_error(GOT_ERR_PRIVSEP_LEN);
1140 iobj = imsg->data;
1142 if (iobj->pack_offset < 0)
1143 return got_error(GOT_ERR_PACK_OFFSET);
1145 *obj = calloc(1, sizeof(**obj));
1146 if (*obj == NULL)
1147 return got_error_from_errno("calloc");
1149 memcpy(&(*obj)->id, &iobj->id, sizeof(iobj->id));
1150 (*obj)->type = iobj->type;
1151 (*obj)->flags = iobj->flags;
1152 (*obj)->hdrlen = iobj->hdrlen;
1153 (*obj)->size = iobj->size;
1154 /* path_packfile is handled by caller */
1155 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1156 (*obj)->pack_offset = iobj->pack_offset;
1157 (*obj)->pack_idx = iobj->pack_idx;
1159 STAILQ_INIT(&(*obj)->deltas.entries);
1160 return NULL;
1163 const struct got_error *
1164 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1166 const struct got_error *err = NULL;
1167 struct imsg imsg;
1168 const size_t min_datalen =
1169 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1171 *obj = NULL;
1173 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1174 if (err)
1175 return err;
1177 switch (imsg.hdr.type) {
1178 case GOT_IMSG_OBJECT:
1179 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1180 break;
1181 default:
1182 err = got_error(GOT_ERR_PRIVSEP_MSG);
1183 break;
1186 imsg_free(&imsg);
1188 return err;
1191 static const struct got_error *
1192 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1193 size_t logmsg_len)
1195 const struct got_error *err = NULL;
1196 size_t offset, remain;
1198 offset = 0;
1199 remain = logmsg_len;
1200 while (remain > 0) {
1201 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1203 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1204 commit->logmsg + offset, n) == -1) {
1205 err = got_error_from_errno("imsg_compose "
1206 "COMMIT_LOGMSG");
1207 break;
1210 err = flush_imsg(ibuf);
1211 if (err)
1212 break;
1214 offset += n;
1215 remain -= n;
1218 return err;
1221 const struct got_error *
1222 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1224 const struct got_error *err = NULL;
1225 struct got_imsg_commit_object *icommit;
1226 uint8_t *buf;
1227 size_t len, total;
1228 struct got_object_qid *qid;
1229 size_t author_len = strlen(commit->author);
1230 size_t committer_len = strlen(commit->committer);
1231 size_t logmsg_len = strlen(commit->logmsg);
1233 total = sizeof(*icommit) + author_len + committer_len +
1234 commit->nparents * sizeof(struct got_object_id);
1236 buf = malloc(total);
1237 if (buf == NULL)
1238 return got_error_from_errno("malloc");
1240 icommit = (struct got_imsg_commit_object *)buf;
1241 memcpy(&icommit->tree_id, commit->tree_id, sizeof(icommit->tree_id));
1242 icommit->author_len = author_len;
1243 icommit->author_time = commit->author_time;
1244 icommit->author_gmtoff = commit->author_gmtoff;
1245 icommit->committer_len = committer_len;
1246 icommit->committer_time = commit->committer_time;
1247 icommit->committer_gmtoff = commit->committer_gmtoff;
1248 icommit->logmsg_len = logmsg_len;
1249 icommit->nparents = commit->nparents;
1251 len = sizeof(*icommit);
1252 memcpy(buf + len, commit->author, author_len);
1253 len += author_len;
1254 memcpy(buf + len, commit->committer, committer_len);
1255 len += committer_len;
1256 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1257 memcpy(buf + len, &qid->id, sizeof(qid->id));
1258 len += sizeof(qid->id);
1261 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1262 err = got_error_from_errno("imsg_compose COMMIT");
1263 goto done;
1266 if (logmsg_len == 0 ||
1267 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1268 err = flush_imsg(ibuf);
1269 if (err)
1270 goto done;
1272 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1273 done:
1274 free(buf);
1275 return err;
1278 static const struct got_error *
1279 get_commit_from_imsg(struct got_commit_object **commit,
1280 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1282 const struct got_error *err = NULL;
1283 struct got_imsg_commit_object *icommit;
1284 size_t len = 0;
1285 int i;
1287 if (datalen < sizeof(*icommit))
1288 return got_error(GOT_ERR_PRIVSEP_LEN);
1290 icommit = imsg->data;
1291 if (datalen != sizeof(*icommit) + icommit->author_len +
1292 icommit->committer_len +
1293 icommit->nparents * sizeof(struct got_object_id))
1294 return got_error(GOT_ERR_PRIVSEP_LEN);
1296 if (icommit->nparents < 0)
1297 return got_error(GOT_ERR_PRIVSEP_LEN);
1299 len += sizeof(*icommit);
1301 *commit = got_object_commit_alloc_partial();
1302 if (*commit == NULL)
1303 return got_error_from_errno(
1304 "got_object_commit_alloc_partial");
1306 memcpy((*commit)->tree_id, &icommit->tree_id,
1307 sizeof(icommit->tree_id));
1308 (*commit)->author_time = icommit->author_time;
1309 (*commit)->author_gmtoff = icommit->author_gmtoff;
1310 (*commit)->committer_time = icommit->committer_time;
1311 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1313 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1314 if ((*commit)->author == NULL) {
1315 err = got_error_from_errno("strndup");
1316 goto done;
1318 len += icommit->author_len;
1320 (*commit)->committer = strndup(imsg->data + len,
1321 icommit->committer_len);
1322 if ((*commit)->committer == NULL) {
1323 err = got_error_from_errno("strndup");
1324 goto done;
1326 len += icommit->committer_len;
1328 if (icommit->logmsg_len == 0) {
1329 (*commit)->logmsg = strdup("");
1330 if ((*commit)->logmsg == NULL) {
1331 err = got_error_from_errno("strdup");
1332 goto done;
1334 } else {
1335 size_t offset = 0, remain = icommit->logmsg_len;
1337 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1338 if ((*commit)->logmsg == NULL) {
1339 err = got_error_from_errno("malloc");
1340 goto done;
1342 while (remain > 0) {
1343 struct imsg imsg_log;
1344 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1345 remain);
1347 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1348 if (err)
1349 goto done;
1351 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1352 err = got_error(GOT_ERR_PRIVSEP_MSG);
1353 goto done;
1356 memcpy((*commit)->logmsg + offset,
1357 imsg_log.data, n);
1358 imsg_free(&imsg_log);
1359 offset += n;
1360 remain -= n;
1362 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1365 for (i = 0; i < icommit->nparents; i++) {
1366 struct got_object_qid *qid;
1368 err = got_object_qid_alloc_partial(&qid);
1369 if (err)
1370 break;
1371 memcpy(&qid->id, imsg->data + len +
1372 i * sizeof(qid->id), sizeof(qid->id));
1373 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1374 (*commit)->nparents++;
1376 done:
1377 if (err) {
1378 got_object_commit_close(*commit);
1379 *commit = NULL;
1381 return err;
1384 const struct got_error *
1385 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1387 const struct got_error *err = NULL;
1388 struct imsg imsg;
1389 size_t datalen;
1390 const size_t min_datalen =
1391 MIN(sizeof(struct got_imsg_error),
1392 sizeof(struct got_imsg_commit_object));
1394 *commit = NULL;
1396 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1397 if (err)
1398 return err;
1400 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1402 switch (imsg.hdr.type) {
1403 case GOT_IMSG_COMMIT:
1404 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1405 break;
1406 default:
1407 err = got_error(GOT_ERR_PRIVSEP_MSG);
1408 break;
1411 imsg_free(&imsg);
1413 return err;
1416 static const struct got_error *
1417 send_tree_entries_batch(struct imsgbuf *ibuf,
1418 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1420 struct ibuf *wbuf;
1421 struct got_imsg_tree_entries ientries;
1422 int i;
1424 memset(&ientries, 0, sizeof(ientries));
1426 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1427 if (wbuf == NULL)
1428 return got_error_from_errno("imsg_create TREE_ENTRY");
1430 ientries.nentries = idxN - idx0 + 1;
1431 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1432 return got_error_from_errno("imsg_add TREE_ENTRY");
1434 for (i = idx0; i <= idxN; i++) {
1435 struct got_parsed_tree_entry *pte = &entries[i];
1437 /* Keep in sync with struct got_imsg_tree_entry definition! */
1438 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1439 return got_error_from_errno("imsg_add TREE_ENTRY");
1440 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1441 return got_error_from_errno("imsg_add TREE_ENTRY");
1442 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1443 return got_error_from_errno("imsg_add TREE_ENTRY");
1445 /* Remaining bytes are the entry's name. */
1446 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1447 return got_error_from_errno("imsg_add TREE_ENTRY");
1450 wbuf->fd = -1;
1451 imsg_close(ibuf, wbuf);
1452 return NULL;
1455 static const struct got_error *
1456 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1457 int nentries)
1459 const struct got_error *err = NULL;
1460 int i, j;
1461 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1463 i = 0;
1464 for (j = 0; j < nentries; j++) {
1465 struct got_parsed_tree_entry *pte = &entries[j];
1466 size_t len = sizeof(struct got_imsg_tree_entry) + pte->namelen;
1468 if (j > 0 &&
1469 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1470 err = send_tree_entries_batch(ibuf, entries,
1471 i, j - 1, entries_len);
1472 if (err)
1473 return err;
1474 i = j;
1475 entries_len = sizeof(struct got_imsg_tree_entries);
1478 entries_len += len;
1481 if (j > 0) {
1482 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1483 entries_len);
1484 if (err)
1485 return err;
1488 return NULL;
1491 const struct got_error *
1492 got_privsep_send_tree(struct imsgbuf *ibuf,
1493 struct got_parsed_tree_entry *entries, int nentries)
1495 const struct got_error *err = NULL;
1496 struct got_imsg_tree_object itree;
1498 memset(&itree, 0, sizeof(itree));
1499 itree.nentries = nentries;
1500 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1501 == -1)
1502 return got_error_from_errno("imsg_compose TREE");
1504 err = send_tree_entries(ibuf, entries, nentries);
1505 if (err)
1506 return err;
1508 return flush_imsg(ibuf);
1512 static const struct got_error *
1513 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1514 int *nentries)
1516 const struct got_error *err = NULL;
1517 struct got_imsg_tree_entries *ientries;
1518 struct got_tree_entry *te;
1519 size_t te_offset;
1520 size_t i;
1522 if (datalen <= sizeof(*ientries) ||
1523 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1524 return got_error(GOT_ERR_PRIVSEP_LEN);
1526 ientries = (struct got_imsg_tree_entries *)data;
1527 if (ientries->nentries > INT_MAX) {
1528 return got_error_msg(GOT_ERR_NO_SPACE,
1529 "too many tree entries");
1532 te_offset = sizeof(*ientries);
1533 for (i = 0; i < ientries->nentries; i++) {
1534 struct got_imsg_tree_entry ite;
1535 const char *te_name;
1536 uint8_t *buf = (uint8_t *)data + te_offset;
1538 if (te_offset >= datalen) {
1539 err = got_error(GOT_ERR_PRIVSEP_LEN);
1540 break;
1543 /* Might not be aligned, size is ~32 bytes. */
1544 memcpy(&ite, buf, sizeof(ite));
1546 if (ite.namelen >= sizeof(te->name)) {
1547 err = got_error(GOT_ERR_PRIVSEP_LEN);
1548 break;
1550 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1551 err = got_error(GOT_ERR_PRIVSEP_LEN);
1552 break;
1555 if (*nentries >= tree->nentries) {
1556 err = got_error(GOT_ERR_PRIVSEP_LEN);
1557 break;
1559 te = &tree->entries[*nentries];
1560 te_name = buf + sizeof(ite);
1561 memcpy(te->name, te_name, ite.namelen);
1562 te->name[ite.namelen] = '\0';
1563 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1564 te->mode = ite.mode;
1565 te->idx = *nentries;
1566 (*nentries)++;
1568 te_offset += sizeof(ite) + ite.namelen;
1571 return err;
1574 const struct got_error *
1575 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1577 const struct got_error *err = NULL;
1578 const size_t min_datalen =
1579 MIN(sizeof(struct got_imsg_error),
1580 sizeof(struct got_imsg_tree_object));
1581 struct got_imsg_tree_object *itree;
1582 int nentries = 0;
1584 *tree = NULL;
1586 while (*tree == NULL || nentries < (*tree)->nentries) {
1587 struct imsg imsg;
1588 size_t datalen;
1590 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1591 if (err)
1592 break;
1594 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1596 switch (imsg.hdr.type) {
1597 case GOT_IMSG_TREE:
1598 /* This message should only appear once. */
1599 if (*tree != NULL) {
1600 err = got_error(GOT_ERR_PRIVSEP_MSG);
1601 break;
1603 if (datalen != sizeof(*itree)) {
1604 err = got_error(GOT_ERR_PRIVSEP_LEN);
1605 break;
1607 itree = imsg.data;
1608 if (itree->nentries < 0) {
1609 err = got_error(GOT_ERR_PRIVSEP_LEN);
1610 break;
1612 *tree = malloc(sizeof(**tree));
1613 if (*tree == NULL) {
1614 err = got_error_from_errno("malloc");
1615 break;
1617 (*tree)->entries = calloc(itree->nentries,
1618 sizeof(struct got_tree_entry));
1619 if ((*tree)->entries == NULL) {
1620 err = got_error_from_errno("malloc");
1621 free(*tree);
1622 *tree = NULL;
1623 break;
1625 (*tree)->nentries = itree->nentries;
1626 (*tree)->refcnt = 0;
1627 break;
1628 case GOT_IMSG_TREE_ENTRIES:
1629 /* This message should be preceeded by GOT_IMSG_TREE. */
1630 if (*tree == NULL) {
1631 err = got_error(GOT_ERR_PRIVSEP_MSG);
1632 break;
1634 err = recv_tree_entries(imsg.data, datalen,
1635 *tree, &nentries);
1636 break;
1637 default:
1638 err = got_error(GOT_ERR_PRIVSEP_MSG);
1639 break;
1642 imsg_free(&imsg);
1643 if (err)
1644 break;
1647 if (*tree && (*tree)->nentries != nentries) {
1648 if (err == NULL)
1649 err = got_error(GOT_ERR_PRIVSEP_LEN);
1650 got_object_tree_close(*tree);
1651 *tree = NULL;
1654 return err;
1657 const struct got_error *
1658 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1659 const uint8_t *data)
1661 struct got_imsg_blob iblob;
1663 memset(&iblob, 0, sizeof(iblob));
1664 iblob.size = size;
1665 iblob.hdrlen = hdrlen;
1667 if (data) {
1668 uint8_t *buf;
1670 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1671 return got_error(GOT_ERR_NO_SPACE);
1673 buf = malloc(sizeof(iblob) + size);
1674 if (buf == NULL)
1675 return got_error_from_errno("malloc");
1677 memcpy(buf, &iblob, sizeof(iblob));
1678 memcpy(buf + sizeof(iblob), data, size);
1679 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1680 sizeof(iblob) + size) == -1) {
1681 free(buf);
1682 return got_error_from_errno("imsg_compose BLOB");
1684 free(buf);
1685 } else {
1686 /* Data has already been written to file descriptor. */
1687 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1688 sizeof(iblob)) == -1)
1689 return got_error_from_errno("imsg_compose BLOB");
1693 return flush_imsg(ibuf);
1696 const struct got_error *
1697 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1698 struct imsgbuf *ibuf)
1700 const struct got_error *err = NULL;
1701 struct imsg imsg;
1702 struct got_imsg_blob *iblob;
1703 size_t datalen;
1705 *outbuf = NULL;
1707 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1708 if (err)
1709 return err;
1711 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1713 switch (imsg.hdr.type) {
1714 case GOT_IMSG_BLOB:
1715 if (datalen < sizeof(*iblob)) {
1716 err = got_error(GOT_ERR_PRIVSEP_LEN);
1717 break;
1719 iblob = imsg.data;
1720 *size = iblob->size;
1721 *hdrlen = iblob->hdrlen;
1723 if (datalen == sizeof(*iblob)) {
1724 /* Data has been written to file descriptor. */
1725 break;
1728 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1729 *size > datalen + sizeof(*iblob)) {
1730 err = got_error(GOT_ERR_PRIVSEP_LEN);
1731 break;
1734 *outbuf = malloc(*size);
1735 if (*outbuf == NULL) {
1736 err = got_error_from_errno("malloc");
1737 break;
1739 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1740 break;
1741 default:
1742 err = got_error(GOT_ERR_PRIVSEP_MSG);
1743 break;
1746 imsg_free(&imsg);
1748 return err;
1751 static const struct got_error *
1752 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1754 const struct got_error *err = NULL;
1755 size_t offset, remain;
1757 offset = 0;
1758 remain = tagmsg_len;
1759 while (remain > 0) {
1760 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1762 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1763 tag->tagmsg + offset, n) == -1) {
1764 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1765 break;
1768 err = flush_imsg(ibuf);
1769 if (err)
1770 break;
1772 offset += n;
1773 remain -= n;
1776 return err;
1779 const struct got_error *
1780 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1782 const struct got_error *err = NULL;
1783 struct got_imsg_tag_object *itag;
1784 uint8_t *buf;
1785 size_t len, total;
1786 size_t tag_len = strlen(tag->tag);
1787 size_t tagger_len = strlen(tag->tagger);
1788 size_t tagmsg_len = strlen(tag->tagmsg);
1790 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1792 buf = malloc(total);
1793 if (buf == NULL)
1794 return got_error_from_errno("malloc");
1796 itag = (struct got_imsg_tag_object *)buf;
1797 memcpy(&itag->id, &tag->id, sizeof(itag->id));
1798 itag->obj_type = tag->obj_type;
1799 itag->tag_len = tag_len;
1800 itag->tagger_len = tagger_len;
1801 itag->tagger_time = tag->tagger_time;
1802 itag->tagger_gmtoff = tag->tagger_gmtoff;
1803 itag->tagmsg_len = tagmsg_len;
1805 len = sizeof(*itag);
1806 memcpy(buf + len, tag->tag, tag_len);
1807 len += tag_len;
1808 memcpy(buf + len, tag->tagger, tagger_len);
1809 len += tagger_len;
1811 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1812 err = got_error_from_errno("imsg_compose TAG");
1813 goto done;
1816 if (tagmsg_len == 0 ||
1817 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1818 err = flush_imsg(ibuf);
1819 if (err)
1820 goto done;
1822 err = send_tagmsg(ibuf, tag, tagmsg_len);
1823 done:
1824 free(buf);
1825 return err;
1828 const struct got_error *
1829 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1831 const struct got_error *err = NULL;
1832 struct imsg imsg;
1833 struct got_imsg_tag_object *itag;
1834 size_t len, datalen;
1835 const size_t min_datalen =
1836 MIN(sizeof(struct got_imsg_error),
1837 sizeof(struct got_imsg_tag_object));
1839 *tag = NULL;
1841 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1842 if (err)
1843 return err;
1845 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1846 len = 0;
1848 switch (imsg.hdr.type) {
1849 case GOT_IMSG_TAG:
1850 if (datalen < sizeof(*itag)) {
1851 err = got_error(GOT_ERR_PRIVSEP_LEN);
1852 break;
1854 itag = imsg.data;
1855 if (datalen != sizeof(*itag) + itag->tag_len +
1856 itag->tagger_len) {
1857 err = got_error(GOT_ERR_PRIVSEP_LEN);
1858 break;
1860 len += sizeof(*itag);
1862 *tag = calloc(1, sizeof(**tag));
1863 if (*tag == NULL) {
1864 err = got_error_from_errno("calloc");
1865 break;
1868 memcpy(&(*tag)->id, &itag->id, sizeof(itag->id));
1870 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1871 if ((*tag)->tag == NULL) {
1872 err = got_error_from_errno("strndup");
1873 break;
1875 len += itag->tag_len;
1877 (*tag)->obj_type = itag->obj_type;
1878 (*tag)->tagger_time = itag->tagger_time;
1879 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1881 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1882 if ((*tag)->tagger == NULL) {
1883 err = got_error_from_errno("strndup");
1884 break;
1886 len += itag->tagger_len;
1888 if (itag->tagmsg_len == 0) {
1889 (*tag)->tagmsg = strdup("");
1890 if ((*tag)->tagmsg == NULL) {
1891 err = got_error_from_errno("strdup");
1892 break;
1894 } else {
1895 size_t offset = 0, remain = itag->tagmsg_len;
1897 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1898 if ((*tag)->tagmsg == NULL) {
1899 err = got_error_from_errno("malloc");
1900 break;
1902 while (remain > 0) {
1903 struct imsg imsg_log;
1904 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1905 remain);
1907 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1908 if (err)
1909 return err;
1911 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1912 return got_error(GOT_ERR_PRIVSEP_MSG);
1914 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1916 imsg_free(&imsg_log);
1917 offset += n;
1918 remain -= n;
1920 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1923 break;
1924 default:
1925 err = got_error(GOT_ERR_PRIVSEP_MSG);
1926 break;
1929 imsg_free(&imsg);
1931 return err;
1934 const struct got_error *
1935 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1936 struct got_packidx *packidx)
1938 const struct got_error *err = NULL;
1939 struct got_imsg_packidx ipackidx;
1940 struct got_imsg_pack ipack;
1941 int fd;
1943 memset(&ipackidx, 0, sizeof(ipackidx));
1944 memset(&ipack, 0, sizeof(ipack));
1946 ipackidx.len = packidx->len;
1947 ipackidx.packfile_size = pack->filesize;
1948 fd = dup(packidx->fd);
1949 if (fd == -1)
1950 return got_error_from_errno("dup");
1952 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1953 sizeof(ipackidx)) == -1) {
1954 err = got_error_from_errno("imsg_compose PACKIDX");
1955 close(fd);
1956 return err;
1959 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1960 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1961 return got_error(GOT_ERR_NO_SPACE);
1962 ipack.filesize = pack->filesize;
1964 fd = dup(pack->fd);
1965 if (fd == -1)
1966 return got_error_from_errno("dup");
1968 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1969 == -1) {
1970 err = got_error_from_errno("imsg_compose PACK");
1971 close(fd);
1972 return err;
1975 return flush_imsg(ibuf);
1978 const struct got_error *
1979 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1980 struct got_object_id *id)
1982 struct got_imsg_packed_object iobj;
1984 memset(&iobj, 0, sizeof(iobj));
1985 iobj.idx = idx;
1986 memcpy(&iobj.id, id, sizeof(iobj.id));
1988 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1989 &iobj, sizeof(iobj)) == -1)
1990 return got_error_from_errno("imsg_compose "
1991 "PACKED_OBJECT_REQUEST");
1993 return flush_imsg(ibuf);
1996 const struct got_error *
1997 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1998 struct got_object_id *id)
2000 struct got_imsg_packed_object iobj;
2002 memset(&iobj, 0, sizeof(iobj));
2003 iobj.idx = idx;
2004 memcpy(&iobj.id, id, sizeof(iobj.id));
2006 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2007 &iobj, sizeof(iobj)) == -1)
2008 return got_error_from_errno("imsg_compose "
2009 "PACKED_OBJECT_REQUEST");
2011 return flush_imsg(ibuf);
2014 const struct got_error *
2015 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2017 const struct got_error *err = NULL;
2019 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2020 NULL, 0) == -1) {
2021 err = got_error_from_errno("imsg_compose "
2022 "GITCONFIG_PARSE_REQUEST");
2023 close(fd);
2024 return err;
2027 return flush_imsg(ibuf);
2030 const struct got_error *
2031 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2033 if (imsg_compose(ibuf,
2034 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2035 NULL, 0) == -1)
2036 return got_error_from_errno("imsg_compose "
2037 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2039 return flush_imsg(ibuf);
2042 const struct got_error *
2043 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2045 if (imsg_compose(ibuf,
2046 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2047 NULL, 0) == -1)
2048 return got_error_from_errno("imsg_compose "
2049 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2051 return flush_imsg(ibuf);
2055 const struct got_error *
2056 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2058 if (imsg_compose(ibuf,
2059 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2060 return got_error_from_errno("imsg_compose "
2061 "GITCONFIG_AUTHOR_NAME_REQUEST");
2063 return flush_imsg(ibuf);
2066 const struct got_error *
2067 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2069 if (imsg_compose(ibuf,
2070 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2071 return got_error_from_errno("imsg_compose "
2072 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2074 return flush_imsg(ibuf);
2077 const struct got_error *
2078 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2080 if (imsg_compose(ibuf,
2081 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2082 return got_error_from_errno("imsg_compose "
2083 "GITCONFIG_REMOTE_REQUEST");
2085 return flush_imsg(ibuf);
2088 const struct got_error *
2089 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2091 if (imsg_compose(ibuf,
2092 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2093 return got_error_from_errno("imsg_compose "
2094 "GITCONFIG_OWNER_REQUEST");
2096 return flush_imsg(ibuf);
2099 const struct got_error *
2100 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2102 const struct got_error *err = NULL;
2103 struct imsg imsg;
2104 size_t datalen;
2106 *str = NULL;
2108 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2109 if (err)
2110 return err;
2111 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2113 switch (imsg.hdr.type) {
2114 case GOT_IMSG_GITCONFIG_STR_VAL:
2115 if (datalen == 0)
2116 break;
2117 *str = strndup(imsg.data, datalen);
2118 if (*str == NULL) {
2119 err = got_error_from_errno("strndup");
2120 break;
2122 break;
2123 default:
2124 err = got_error(GOT_ERR_PRIVSEP_MSG);
2125 break;
2128 imsg_free(&imsg);
2129 return err;
2132 const struct got_error *
2133 got_privsep_recv_gitconfig_pair(char **key, char **val, struct imsgbuf *ibuf)
2135 const struct got_error *err = NULL;
2136 struct got_imsg_gitconfig_pair p;
2137 struct imsg imsg;
2138 size_t datalen;
2139 uint8_t *data;
2141 *key = *val = NULL;
2143 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2144 if (err)
2145 return err;
2147 data = imsg.data;
2148 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2150 if (imsg.hdr.type != GOT_IMSG_GITCONFIG_PAIR) {
2151 err = got_error(GOT_ERR_PRIVSEP_MSG);
2152 goto done;
2155 if (datalen < sizeof(p)) {
2156 err = got_error(GOT_ERR_PRIVSEP_LEN);
2157 goto done;
2160 memcpy(&p, data, sizeof(p));
2161 data += sizeof(p);
2163 if (datalen != sizeof(p) + p.klen + p.vlen) {
2164 err = got_error(GOT_ERR_PRIVSEP_LEN);
2165 goto done;
2168 *key = strndup(data, p.klen);
2169 if (*key == NULL) {
2170 err = got_error_from_errno("strndup");
2171 goto done;
2173 data += p.klen;
2175 *val = strndup(data, p.vlen);
2176 if (*val == NULL) {
2177 err = got_error_from_errno("strndup");
2178 goto done;
2181 done:
2182 imsg_free(&imsg);
2183 return err;
2186 const struct got_error *
2187 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2189 const struct got_error *err = NULL;
2190 struct imsg imsg;
2191 size_t datalen;
2192 const size_t min_datalen =
2193 MIN(sizeof(struct got_imsg_error), sizeof(int));
2195 *val = 0;
2197 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2198 if (err)
2199 return err;
2200 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2202 switch (imsg.hdr.type) {
2203 case GOT_IMSG_GITCONFIG_INT_VAL:
2204 if (datalen != sizeof(*val)) {
2205 err = got_error(GOT_ERR_PRIVSEP_LEN);
2206 break;
2208 memcpy(val, imsg.data, sizeof(*val));
2209 break;
2210 default:
2211 err = got_error(GOT_ERR_PRIVSEP_MSG);
2212 break;
2215 imsg_free(&imsg);
2216 return err;
2219 static void
2220 free_remote_data(struct got_remote_repo *remote)
2222 int i;
2224 free(remote->name);
2225 free(remote->fetch_url);
2226 free(remote->send_url);
2227 for (i = 0; i < remote->nfetch_branches; i++)
2228 free(remote->fetch_branches[i]);
2229 free(remote->fetch_branches);
2230 for (i = 0; i < remote->nsend_branches; i++)
2231 free(remote->send_branches[i]);
2232 free(remote->send_branches);
2233 for (i = 0; i < remote->nfetch_refs; i++)
2234 free(remote->fetch_refs[i]);
2235 free(remote->fetch_refs);
2238 const struct got_error *
2239 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2240 int *nremotes, struct imsgbuf *ibuf)
2242 const struct got_error *err = NULL;
2243 struct imsg imsg;
2244 size_t datalen;
2245 struct got_imsg_remotes iremotes;
2246 struct got_imsg_remote iremote;
2248 *remotes = NULL;
2249 *nremotes = 0;
2250 iremotes.nremotes = 0;
2252 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2253 if (err)
2254 return err;
2255 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2257 switch (imsg.hdr.type) {
2258 case GOT_IMSG_GITCONFIG_REMOTES:
2259 if (datalen != sizeof(iremotes)) {
2260 err = got_error(GOT_ERR_PRIVSEP_LEN);
2261 break;
2263 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2264 if (iremotes.nremotes == 0) {
2265 imsg_free(&imsg);
2266 return NULL;
2268 break;
2269 default:
2270 imsg_free(&imsg);
2271 return got_error(GOT_ERR_PRIVSEP_MSG);
2274 imsg_free(&imsg);
2276 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2277 if (*remotes == NULL)
2278 return got_error_from_errno("recallocarray");
2280 while (*nremotes < iremotes.nremotes) {
2281 struct got_remote_repo *remote;
2283 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2284 if (err)
2285 break;
2286 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2288 switch (imsg.hdr.type) {
2289 case GOT_IMSG_GITCONFIG_REMOTE:
2290 remote = &(*remotes)[*nremotes];
2291 memset(remote, 0, sizeof(*remote));
2292 if (datalen < sizeof(iremote)) {
2293 err = got_error(GOT_ERR_PRIVSEP_LEN);
2294 break;
2296 memcpy(&iremote, imsg.data, sizeof(iremote));
2297 if (iremote.name_len == 0 ||
2298 iremote.fetch_url_len == 0 ||
2299 iremote.send_url_len == 0 ||
2300 (sizeof(iremote) + iremote.name_len +
2301 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2302 err = got_error(GOT_ERR_PRIVSEP_LEN);
2303 break;
2305 remote->name = strndup(imsg.data + sizeof(iremote),
2306 iremote.name_len);
2307 if (remote->name == NULL) {
2308 err = got_error_from_errno("strndup");
2309 break;
2311 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2312 iremote.name_len, iremote.fetch_url_len);
2313 if (remote->fetch_url == NULL) {
2314 err = got_error_from_errno("strndup");
2315 free_remote_data(remote);
2316 break;
2318 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2319 iremote.name_len + iremote.fetch_url_len,
2320 iremote.send_url_len);
2321 if (remote->send_url == NULL) {
2322 err = got_error_from_errno("strndup");
2323 free_remote_data(remote);
2324 break;
2326 remote->mirror_references = iremote.mirror_references;
2327 remote->fetch_all_branches = iremote.fetch_all_branches;
2328 remote->nfetch_branches = 0;
2329 remote->fetch_branches = NULL;
2330 remote->nsend_branches = 0;
2331 remote->send_branches = NULL;
2332 remote->nfetch_refs = 0;
2333 remote->fetch_refs = NULL;
2334 (*nremotes)++;
2335 break;
2336 default:
2337 err = got_error(GOT_ERR_PRIVSEP_MSG);
2338 break;
2341 imsg_free(&imsg);
2342 if (err)
2343 break;
2346 if (err) {
2347 int i;
2348 for (i = 0; i < *nremotes; i++)
2349 free_remote_data(&(*remotes)[i]);
2350 free(*remotes);
2351 *remotes = NULL;
2352 *nremotes = 0;
2354 return err;
2357 const struct got_error *
2358 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2360 const struct got_error *err = NULL;
2362 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2363 NULL, 0) == -1) {
2364 err = got_error_from_errno("imsg_compose "
2365 "GOTCONFIG_PARSE_REQUEST");
2366 close(fd);
2367 return err;
2370 return flush_imsg(ibuf);
2373 const struct got_error *
2374 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2376 if (imsg_compose(ibuf,
2377 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2378 return got_error_from_errno("imsg_compose "
2379 "GOTCONFIG_AUTHOR_REQUEST");
2381 return flush_imsg(ibuf);
2384 const struct got_error *
2385 got_privsep_send_gotconfig_allowed_signers_req(struct imsgbuf *ibuf)
2387 if (imsg_compose(ibuf,
2388 GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2389 return got_error_from_errno("imsg_compose "
2390 "GOTCONFIG_ALLOWEDSIGNERS_REQUEST");
2392 return flush_imsg(ibuf);
2395 const struct got_error *
2396 got_privsep_send_gotconfig_revoked_signers_req(struct imsgbuf *ibuf)
2398 if (imsg_compose(ibuf,
2399 GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2400 return got_error_from_errno("imsg_compose "
2401 "GOTCONFIG_REVOKEDSIGNERS_REQUEST");
2403 return flush_imsg(ibuf);
2406 const struct got_error *
2407 got_privsep_send_gotconfig_signer_id_req(struct imsgbuf *ibuf)
2409 if (imsg_compose(ibuf,
2410 GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST, 0, 0, -1, NULL, 0) == -1)
2411 return got_error_from_errno("imsg_compose "
2412 "GOTCONFIG_SIGNERID_REQUEST");
2414 return flush_imsg(ibuf);
2417 const struct got_error *
2418 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2420 if (imsg_compose(ibuf,
2421 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2422 return got_error_from_errno("imsg_compose "
2423 "GOTCONFIG_REMOTE_REQUEST");
2425 return flush_imsg(ibuf);
2428 const struct got_error *
2429 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2431 const struct got_error *err = NULL;
2432 struct imsg imsg;
2433 size_t datalen;
2435 *str = NULL;
2437 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2438 if (err)
2439 return err;
2440 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2442 switch (imsg.hdr.type) {
2443 case GOT_IMSG_GOTCONFIG_STR_VAL:
2444 if (datalen == 0)
2445 break;
2446 *str = strndup(imsg.data, datalen);
2447 if (*str == NULL) {
2448 err = got_error_from_errno("strndup");
2449 break;
2451 break;
2452 default:
2453 err = got_error(GOT_ERR_PRIVSEP_MSG);
2454 break;
2457 imsg_free(&imsg);
2458 return err;
2461 const struct got_error *
2462 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2463 int *nremotes, struct imsgbuf *ibuf)
2465 const struct got_error *err = NULL;
2466 struct imsg imsg;
2467 size_t datalen;
2468 struct got_imsg_remotes iremotes;
2469 struct got_imsg_remote iremote;
2470 const size_t min_datalen =
2471 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2473 *remotes = NULL;
2474 *nremotes = 0;
2475 iremotes.nremotes = 0;
2477 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2478 if (err)
2479 return err;
2480 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2482 switch (imsg.hdr.type) {
2483 case GOT_IMSG_GOTCONFIG_REMOTES:
2484 if (datalen != sizeof(iremotes)) {
2485 err = got_error(GOT_ERR_PRIVSEP_LEN);
2486 break;
2488 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2489 if (iremotes.nremotes < 0) {
2490 err = got_error(GOT_ERR_PRIVSEP_LEN);
2491 break;
2493 if (iremotes.nremotes == 0) {
2494 imsg_free(&imsg);
2495 return NULL;
2497 break;
2498 default:
2499 imsg_free(&imsg);
2500 return got_error(GOT_ERR_PRIVSEP_MSG);
2503 imsg_free(&imsg);
2505 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2506 if (*remotes == NULL)
2507 return got_error_from_errno("recallocarray");
2509 while (*nremotes < iremotes.nremotes) {
2510 struct got_remote_repo *remote;
2511 const size_t min_datalen =
2512 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2513 int i;
2515 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2516 if (err)
2517 break;
2518 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2520 switch (imsg.hdr.type) {
2521 case GOT_IMSG_GOTCONFIG_REMOTE:
2522 remote = &(*remotes)[*nremotes];
2523 memset(remote, 0, sizeof(*remote));
2524 if (datalen < sizeof(iremote)) {
2525 err = got_error(GOT_ERR_PRIVSEP_LEN);
2526 break;
2528 memcpy(&iremote, imsg.data, sizeof(iremote));
2529 if (iremote.name_len == 0 ||
2530 (iremote.fetch_url_len == 0 &&
2531 iremote.send_url_len == 0) ||
2532 (sizeof(iremote) + iremote.name_len +
2533 iremote.fetch_url_len + iremote.send_url_len) >
2534 datalen) {
2535 err = got_error(GOT_ERR_PRIVSEP_LEN);
2536 break;
2538 remote->name = strndup(imsg.data + sizeof(iremote),
2539 iremote.name_len);
2540 if (remote->name == NULL) {
2541 err = got_error_from_errno("strndup");
2542 break;
2544 remote->fetch_url = strndup(imsg.data +
2545 sizeof(iremote) + iremote.name_len,
2546 iremote.fetch_url_len);
2547 if (remote->fetch_url == NULL) {
2548 err = got_error_from_errno("strndup");
2549 free_remote_data(remote);
2550 break;
2552 remote->send_url = strndup(imsg.data +
2553 sizeof(iremote) + iremote.name_len +
2554 iremote.fetch_url_len, iremote.send_url_len);
2555 if (remote->send_url == NULL) {
2556 err = got_error_from_errno("strndup");
2557 free_remote_data(remote);
2558 break;
2560 remote->mirror_references = iremote.mirror_references;
2561 remote->fetch_all_branches = iremote.fetch_all_branches;
2562 if (iremote.nfetch_branches > 0) {
2563 remote->fetch_branches = recallocarray(NULL, 0,
2564 iremote.nfetch_branches, sizeof(char *));
2565 if (remote->fetch_branches == NULL) {
2566 err = got_error_from_errno("calloc");
2567 free_remote_data(remote);
2568 break;
2571 remote->nfetch_branches = 0;
2572 for (i = 0; i < iremote.nfetch_branches; i++) {
2573 char *branch;
2574 err = got_privsep_recv_gotconfig_str(&branch,
2575 ibuf);
2576 if (err) {
2577 free_remote_data(remote);
2578 goto done;
2580 remote->fetch_branches[i] = branch;
2581 remote->nfetch_branches++;
2583 if (iremote.nsend_branches > 0) {
2584 remote->send_branches = recallocarray(NULL, 0,
2585 iremote.nsend_branches, sizeof(char *));
2586 if (remote->send_branches == NULL) {
2587 err = got_error_from_errno("calloc");
2588 free_remote_data(remote);
2589 break;
2592 remote->nsend_branches = 0;
2593 for (i = 0; i < iremote.nsend_branches; i++) {
2594 char *branch;
2595 err = got_privsep_recv_gotconfig_str(&branch,
2596 ibuf);
2597 if (err) {
2598 free_remote_data(remote);
2599 goto done;
2601 remote->send_branches[i] = branch;
2602 remote->nsend_branches++;
2604 if (iremote.nfetch_refs > 0) {
2605 remote->fetch_refs = recallocarray(NULL, 0,
2606 iremote.nfetch_refs, sizeof(char *));
2607 if (remote->fetch_refs == NULL) {
2608 err = got_error_from_errno("calloc");
2609 free_remote_data(remote);
2610 break;
2613 remote->nfetch_refs = 0;
2614 for (i = 0; i < iremote.nfetch_refs; i++) {
2615 char *ref;
2616 err = got_privsep_recv_gotconfig_str(&ref,
2617 ibuf);
2618 if (err) {
2619 free_remote_data(remote);
2620 goto done;
2622 remote->fetch_refs[i] = ref;
2623 remote->nfetch_refs++;
2625 (*nremotes)++;
2626 break;
2627 default:
2628 err = got_error(GOT_ERR_PRIVSEP_MSG);
2629 break;
2632 imsg_free(&imsg);
2633 if (err)
2634 break;
2636 done:
2637 if (err) {
2638 int i;
2639 for (i = 0; i < *nremotes; i++)
2640 free_remote_data(&(*remotes)[i]);
2641 free(*remotes);
2642 *remotes = NULL;
2643 *nremotes = 0;
2645 return err;
2648 const struct got_error *
2649 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2650 struct got_object_id *id, int idx, const char *path)
2652 struct ibuf *wbuf;
2653 size_t path_len = strlen(path);
2655 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2656 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2657 if (wbuf == NULL)
2658 return got_error_from_errno(
2659 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2661 * Keep in sync with struct got_imsg_commit_traversal_request
2662 * and struct got_imsg_packed_object.
2664 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
2665 return got_error_from_errno("imsg_add "
2666 "COMMIT_TRAVERSAL_REQUEST");
2667 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2668 return got_error_from_errno("imsg_add "
2669 "COMMIT_TRAVERSAL_REQUEST");
2670 if (imsg_add(wbuf, &path_len, sizeof(path_len)) == -1)
2671 return got_error_from_errno("imsg_add "
2672 "COMMIT_TRAVERSAL_REQUEST");
2673 if (imsg_add(wbuf, path, path_len) == -1)
2674 return got_error_from_errno("imsg_add "
2675 "COMMIT_TRAVERSAL_REQUEST");
2677 wbuf->fd = -1;
2678 imsg_close(ibuf, wbuf);
2680 return flush_imsg(ibuf);
2683 const struct got_error *
2684 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2685 struct got_object_id **changed_commit_id,
2686 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2688 const struct got_error *err = NULL;
2689 struct imsg imsg;
2690 struct got_imsg_traversed_commits *icommits;
2691 struct got_object_id *ids;
2692 size_t datalen;
2693 int i, done = 0;
2695 *changed_commit = NULL;
2696 *changed_commit_id = NULL;
2698 while (!done) {
2699 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2700 if (err)
2701 return err;
2703 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2704 switch (imsg.hdr.type) {
2705 case GOT_IMSG_TRAVERSED_COMMITS:
2706 icommits = imsg.data;
2707 if (datalen != sizeof(*icommits) +
2708 icommits->ncommits * sizeof(*ids)) {
2709 err = got_error(GOT_ERR_PRIVSEP_LEN);
2710 break;
2712 ids = imsg.data + sizeof(*icommits);
2713 for (i = 0; i < icommits->ncommits; i++) {
2714 struct got_object_qid *qid;
2716 err = got_object_qid_alloc_partial(&qid);
2717 if (err)
2718 break;
2719 memcpy(&qid->id, &ids[i], sizeof(ids[i]));
2720 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2722 /* The last commit may contain a change. */
2723 if (i == icommits->ncommits - 1) {
2724 *changed_commit_id =
2725 got_object_id_dup(&qid->id);
2726 if (*changed_commit_id == NULL) {
2727 err = got_error_from_errno(
2728 "got_object_id_dup");
2729 break;
2733 break;
2734 case GOT_IMSG_COMMIT:
2735 if (*changed_commit_id == NULL) {
2736 err = got_error(GOT_ERR_PRIVSEP_MSG);
2737 break;
2739 err = get_commit_from_imsg(changed_commit, &imsg,
2740 datalen, ibuf);
2741 break;
2742 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2743 done = 1;
2744 break;
2745 default:
2746 err = got_error(GOT_ERR_PRIVSEP_MSG);
2747 break;
2750 imsg_free(&imsg);
2751 if (err)
2752 break;
2755 if (err)
2756 got_object_id_queue_free(commit_ids);
2757 return err;
2760 const struct got_error *
2761 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2762 struct got_object_id *tree_id, const char *path,
2763 struct got_parsed_tree_entry *entries, int nentries)
2765 const struct got_error *err = NULL;
2766 struct ibuf *wbuf;
2767 size_t path_len = strlen(path);
2768 size_t msglen;
2770 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2771 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2772 if (wbuf == NULL)
2773 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2775 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2776 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2777 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2778 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2779 if (imsg_add(wbuf, path, path_len) == -1)
2780 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2782 wbuf->fd = -1;
2783 imsg_close(ibuf, wbuf);
2785 if (entries) {
2786 err = send_tree_entries(ibuf, entries, nentries);
2787 if (err)
2788 return err;
2791 return flush_imsg(ibuf);
2794 const struct got_error *
2795 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2797 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2798 0, 0, -1, NULL, 0) == -1)
2799 return got_error_from_errno("imsg_compose "
2800 "OBJECT_ENUMERATION_REQUEST");
2802 return flush_imsg(ibuf);
2805 const struct got_error *
2806 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2808 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2809 0, 0, -1, NULL, 0) == -1)
2810 return got_error_from_errno("imsg_compose "
2811 "OBJECT_ENUMERATION_DONE");
2813 return flush_imsg(ibuf);
2816 const struct got_error *
2817 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2819 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2820 0, 0, -1, NULL, 0) == -1)
2821 return got_error_from_errno("imsg_compose "
2822 "OBJECT_ENUMERATION_INCOMPLETE");
2824 return flush_imsg(ibuf);
2827 const struct got_error *
2828 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2829 struct got_object_id *id, time_t mtime)
2831 struct ibuf *wbuf;
2833 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2834 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2835 if (wbuf == NULL)
2836 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2838 /* Keep in sync with struct got_imsg_enumerated_commit! */
2839 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2840 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2841 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2842 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2844 wbuf->fd = -1;
2845 imsg_close(ibuf, wbuf);
2846 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2847 return NULL;
2850 const struct got_error *
2851 got_privsep_recv_enumerated_objects(int *found_all_objects,
2852 struct imsgbuf *ibuf,
2853 got_object_enumerate_commit_cb cb_commit,
2854 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2855 struct got_repository *repo)
2857 const struct got_error *err = NULL;
2858 struct imsg imsg;
2859 struct got_imsg_enumerated_commit *icommit = NULL;
2860 struct got_object_id commit_id;
2861 int have_commit = 0;
2862 time_t mtime = 0;
2863 struct got_tree_object tree;
2864 struct got_imsg_enumerated_tree *itree;
2865 struct got_object_id tree_id;
2866 char *path = NULL, *canon_path = NULL;
2867 size_t datalen, path_len;
2868 int nentries = -1;
2869 int done = 0;
2871 *found_all_objects = 0;
2872 memset(&tree, 0, sizeof(tree));
2874 while (!done) {
2875 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2876 if (err)
2877 break;
2879 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2880 switch (imsg.hdr.type) {
2881 case GOT_IMSG_ENUMERATED_COMMIT:
2882 if (have_commit && nentries != -1) {
2883 err = got_error(GOT_ERR_PRIVSEP_MSG);
2884 break;
2886 if (datalen != sizeof(*icommit)) {
2887 err = got_error(GOT_ERR_PRIVSEP_LEN);
2888 break;
2890 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2891 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2892 mtime = icommit->mtime;
2893 have_commit = 1;
2894 break;
2895 case GOT_IMSG_ENUMERATED_TREE:
2896 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2897 if (!have_commit) {
2898 err = got_error(GOT_ERR_PRIVSEP_MSG);
2899 break;
2901 if (datalen < sizeof(*itree)) {
2902 err = got_error(GOT_ERR_PRIVSEP_LEN);
2903 break;
2905 itree = imsg.data;
2906 path_len = datalen - sizeof(*itree);
2907 if (path_len == 0) {
2908 err = got_error(GOT_ERR_PRIVSEP_LEN);
2909 break;
2911 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2912 free(path);
2913 path = strndup(imsg.data + sizeof(*itree), path_len);
2914 if (path == NULL) {
2915 err = got_error_from_errno("strndup");
2916 break;
2918 free(canon_path);
2919 canon_path = malloc(path_len + 1);
2920 if (canon_path == NULL) {
2921 err = got_error_from_errno("malloc");
2922 break;
2924 if (!got_path_is_absolute(path)) {
2925 err = got_error(GOT_ERR_BAD_PATH);
2926 break;
2928 if (got_path_is_root_dir(path)) {
2929 /* XXX check what got_canonpath() does wrong */
2930 canon_path[0] = '/';
2931 canon_path[1] = '\0';
2932 } else {
2933 err = got_canonpath(path, canon_path,
2934 path_len + 1);
2935 if (err)
2936 break;
2938 if (strcmp(path, canon_path) != 0) {
2939 err = got_error(GOT_ERR_BAD_PATH);
2940 break;
2942 if (nentries != -1) {
2943 err = got_error(GOT_ERR_PRIVSEP_MSG);
2944 break;
2946 if (itree->nentries < -1) {
2947 err = got_error(GOT_ERR_PRIVSEP_MSG);
2948 break;
2950 if (itree->nentries == -1) {
2951 /* Tree was not found in pack file. */
2952 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2953 path, repo);
2954 break;
2956 if (itree->nentries > INT_MAX) {
2957 err = got_error(GOT_ERR_PRIVSEP_LEN);
2958 break;
2960 tree.entries = calloc(itree->nentries,
2961 sizeof(struct got_tree_entry));
2962 if (tree.entries == NULL) {
2963 err = got_error_from_errno("calloc");
2964 break;
2966 if (itree->nentries == 0) {
2967 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2968 path, repo);
2969 if (err)
2970 break;
2972 /* Prepare for next tree. */
2973 free(tree.entries);
2974 memset(&tree, 0, sizeof(tree));
2975 nentries = -1;
2976 } else {
2977 tree.nentries = itree->nentries;
2978 nentries = 0;
2980 break;
2981 case GOT_IMSG_TREE_ENTRIES:
2982 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2983 if (nentries <= -1) {
2984 err = got_error(GOT_ERR_PRIVSEP_MSG);
2985 break;
2987 err = recv_tree_entries(imsg.data, datalen,
2988 &tree, &nentries);
2989 if (err)
2990 break;
2991 if (tree.nentries == nentries) {
2992 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2993 path, repo);
2994 if (err)
2995 break;
2997 /* Prepare for next tree. */
2998 free(tree.entries);
2999 memset(&tree, 0, sizeof(tree));
3000 nentries = -1;
3002 break;
3003 case GOT_IMSG_TREE_ENUMERATION_DONE:
3004 /* All trees have been found and traversed. */
3005 if (!have_commit || path == NULL || nentries != -1) {
3006 err = got_error(GOT_ERR_PRIVSEP_MSG);
3007 break;
3009 err = cb_commit(cb_arg, mtime, &commit_id, repo);
3010 if (err)
3011 break;
3012 have_commit = 0;
3013 break;
3014 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
3015 *found_all_objects = 1;
3016 done = 1;
3017 break;
3018 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
3019 done = 1;
3020 break;
3021 default:
3022 err = got_error(GOT_ERR_PRIVSEP_MSG);
3023 break;
3026 imsg_free(&imsg);
3027 if (err)
3028 break;
3031 free(path);
3032 free(canon_path);
3033 free(tree.entries);
3034 return err;
3037 const struct got_error *
3038 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
3039 struct got_object_id *id)
3041 struct got_imsg_raw_delta_request dreq;
3043 memset(&dreq, 0, sizeof(dreq));
3044 dreq.idx = idx;
3045 memcpy(&dreq.id, id, sizeof(dreq.id));
3047 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
3048 &dreq, sizeof(dreq)) == -1)
3049 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
3051 return flush_imsg(ibuf);
3054 const struct got_error *
3055 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
3057 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3060 const struct got_error *
3061 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3062 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3063 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3065 struct got_imsg_raw_delta idelta;
3066 int ret;
3068 memset(&idelta, 0, sizeof(idelta));
3069 idelta.base_size = base_size;
3070 idelta.result_size = result_size;
3071 idelta.delta_size = delta_size;
3072 idelta.delta_compressed_size = delta_compressed_size;
3073 idelta.delta_offset = delta_offset;
3074 idelta.delta_out_offset = delta_out_offset;
3075 memcpy(&idelta.base_id, &base_id, sizeof(idelta.base_id));
3077 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3078 &idelta, sizeof(idelta));
3079 if (ret == -1)
3080 return got_error_from_errno("imsg_compose RAW_DELTA");
3082 return flush_imsg(ibuf);
3085 const struct got_error *
3086 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3087 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3088 off_t *delta_out_offset, struct got_object_id **base_id,
3089 struct imsgbuf *ibuf)
3091 const struct got_error *err = NULL;
3092 struct imsg imsg;
3093 struct got_imsg_raw_delta *delta;
3094 size_t datalen;
3096 *base_size = 0;
3097 *result_size = 0;
3098 *delta_size = 0;
3099 *delta_compressed_size = 0;
3100 *delta_offset = 0;
3101 *delta_out_offset = 0;
3102 *base_id = NULL;
3104 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3105 if (err)
3106 return err;
3108 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3110 switch (imsg.hdr.type) {
3111 case GOT_IMSG_RAW_DELTA:
3112 if (datalen != sizeof(*delta)) {
3113 err = got_error(GOT_ERR_PRIVSEP_LEN);
3114 break;
3116 delta = imsg.data;
3117 *base_size = delta->base_size;
3118 *result_size = delta->result_size;
3119 *delta_size = delta->delta_size;
3120 *delta_compressed_size = delta->delta_compressed_size;
3121 *delta_offset = delta->delta_offset;
3122 *delta_out_offset = delta->delta_out_offset;
3123 *base_id = calloc(1, sizeof(**base_id));
3124 if (*base_id == NULL) {
3125 err = got_error_from_errno("malloc");
3126 break;
3128 memcpy(*base_id, &delta->base_id, sizeof(**base_id));
3129 break;
3130 default:
3131 err = got_error(GOT_ERR_PRIVSEP_MSG);
3132 break;
3135 imsg_free(&imsg);
3137 if (err) {
3138 free(*base_id);
3139 *base_id = NULL;
3141 return err;
3144 static const struct got_error *
3145 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3147 const struct got_error *err = NULL;
3148 struct got_imsg_object_idlist idlist;
3149 struct ibuf *wbuf;
3150 size_t i;
3152 memset(&idlist, 0, sizeof(idlist));
3154 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3155 return got_error(GOT_ERR_NO_SPACE);
3157 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3158 sizeof(idlist) + nids * sizeof(**ids));
3159 if (wbuf == NULL) {
3160 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3161 return err;
3164 idlist.nids = nids;
3165 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3166 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3168 for (i = 0; i < nids; i++) {
3169 struct got_object_id *id = ids[i];
3170 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3171 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3174 wbuf->fd = -1;
3175 imsg_close(ibuf, wbuf);
3177 return flush_imsg(ibuf);
3180 const struct got_error *
3181 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3182 struct got_object_id **ids, size_t nids)
3184 const struct got_error *err = NULL;
3185 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3186 int i, queued = 0;
3188 for (i = 0; i < nids; i++) {
3189 idlist[i % nitems(idlist)] = ids[i];
3190 queued++;
3191 if (queued >= nitems(idlist)) {
3192 err = send_idlist(ibuf, idlist, queued);
3193 if (err)
3194 return err;
3195 queued = 0;
3199 if (queued > 0) {
3200 err = send_idlist(ibuf, idlist, queued);
3201 if (err)
3202 return err;
3205 return NULL;
3208 const struct got_error *
3209 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3211 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3212 == -1)
3213 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3215 return flush_imsg(ibuf);
3218 const struct got_error *
3219 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3220 size_t *nids, struct imsgbuf *ibuf)
3222 const struct got_error *err = NULL;
3223 struct imsg imsg;
3224 struct got_imsg_object_idlist *idlist;
3225 size_t datalen;
3227 *ids = NULL;
3228 *done = 0;
3229 *nids = 0;
3231 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3232 if (err)
3233 return err;
3235 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3236 switch (imsg.hdr.type) {
3237 case GOT_IMSG_OBJ_ID_LIST:
3238 if (datalen < sizeof(*idlist)) {
3239 err = got_error(GOT_ERR_PRIVSEP_LEN);
3240 break;
3242 idlist = imsg.data;
3243 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3244 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3245 err = got_error(GOT_ERR_PRIVSEP_LEN);
3246 break;
3248 *nids = idlist->nids;
3249 *ids = calloc(*nids, sizeof(**ids));
3250 if (*ids == NULL) {
3251 err = got_error_from_errno("calloc");
3252 break;
3254 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3255 *nids * sizeof(**ids));
3256 break;
3257 case GOT_IMSG_OBJ_ID_LIST_DONE:
3258 *done = 1;
3259 break;
3260 default:
3261 err = got_error(GOT_ERR_PRIVSEP_MSG);
3262 break;
3265 imsg_free(&imsg);
3267 return err;
3270 const struct got_error *
3271 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3273 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3274 == -1)
3275 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3277 return flush_imsg(ibuf);
3280 const struct got_error *
3281 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3282 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3284 const struct got_error *err = NULL;
3285 struct ibuf *wbuf;
3286 struct got_imsg_reused_deltas ideltas;
3287 size_t i;
3289 memset(&ideltas, 0, sizeof(ideltas));
3291 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3292 return got_error(GOT_ERR_NO_SPACE);
3294 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3295 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3296 if (wbuf == NULL) {
3297 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3298 return err;
3301 ideltas.ndeltas = ndeltas;
3302 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3303 return got_error_from_errno("imsg_add REUSED_DELTAS");
3305 for (i = 0; i < ndeltas; i++) {
3306 struct got_imsg_reused_delta *delta = &deltas[i];
3307 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3308 return got_error_from_errno("imsg_add REUSED_DELTAS");
3311 wbuf->fd = -1;
3312 imsg_close(ibuf, wbuf);
3314 return flush_imsg(ibuf);
3317 const struct got_error *
3318 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3320 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3321 == -1)
3322 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3324 return flush_imsg(ibuf);
3327 const struct got_error *
3328 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3329 size_t *ndeltas, struct imsgbuf *ibuf)
3331 const struct got_error *err = NULL;
3332 struct imsg imsg;
3333 struct got_imsg_reused_deltas *ideltas;
3334 size_t datalen;
3336 *done = 0;
3337 *ndeltas = 0;
3339 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3340 if (err)
3341 return err;
3343 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3344 switch (imsg.hdr.type) {
3345 case GOT_IMSG_REUSED_DELTAS:
3346 if (datalen < sizeof(*ideltas)) {
3347 err = got_error(GOT_ERR_PRIVSEP_LEN);
3348 break;
3350 ideltas = imsg.data;
3351 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3352 ideltas->ndeltas * sizeof(*deltas) >
3353 datalen - sizeof(*ideltas)) {
3354 err = got_error(GOT_ERR_PRIVSEP_LEN);
3355 break;
3357 *ndeltas = ideltas->ndeltas;
3358 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3359 *ndeltas * sizeof(*deltas));
3360 break;
3361 case GOT_IMSG_DELTA_REUSE_DONE:
3362 *done = 1;
3363 break;
3364 default:
3365 err = got_error(GOT_ERR_PRIVSEP_MSG);
3366 break;
3369 imsg_free(&imsg);
3371 return err;
3374 const struct got_error *
3375 got_privsep_init_commit_painting(struct imsgbuf *ibuf)
3377 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_INIT,
3378 0, 0, -1, NULL, 0)
3379 == -1)
3380 return got_error_from_errno("imsg_compose "
3381 "COMMIT_PAINTING_INIT");
3383 return flush_imsg(ibuf);
3386 const struct got_error *
3387 got_privsep_send_painting_request(struct imsgbuf *ibuf, int idx,
3388 struct got_object_id *id, intptr_t color)
3390 struct got_imsg_commit_painting_request ireq;
3392 memset(&ireq, 0, sizeof(ireq));
3393 memcpy(&ireq.id, id, sizeof(ireq.id));
3394 ireq.idx = idx;
3395 ireq.color = color;
3397 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_REQUEST, 0, 0, -1,
3398 &ireq, sizeof(ireq)) == -1)
3399 return got_error_from_errno("imsg_compose "
3400 "COMMIT_PAINTING_REQUEST");
3402 return flush_imsg(ibuf);
3405 static const struct got_error *
3406 send_painted_commits(struct got_object_id_queue *ids, int *nids,
3407 size_t remain, int present_in_pack, struct imsgbuf *ibuf)
3409 const struct got_error *err = NULL;
3410 struct ibuf *wbuf = NULL;
3411 struct got_object_qid *qid;
3412 size_t msglen;
3413 int ncommits;
3414 intptr_t color;
3416 msglen = MIN(remain, MAX_IMSGSIZE - IMSG_HEADER_SIZE);
3417 ncommits = (msglen - sizeof(struct got_imsg_painted_commits)) /
3418 sizeof(struct got_imsg_painted_commit);
3420 wbuf = imsg_create(ibuf, GOT_IMSG_PAINTED_COMMITS, 0, 0, msglen);
3421 if (wbuf == NULL) {
3422 err = got_error_from_errno("imsg_create PAINTED_COMMITS");
3423 return err;
3426 /* Keep in sync with struct got_imsg_painted_commits! */
3427 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
3428 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3429 if (imsg_add(wbuf, &present_in_pack, sizeof(present_in_pack)) == -1)
3430 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3432 while (ncommits > 0) {
3433 qid = STAILQ_FIRST(ids);
3434 STAILQ_REMOVE_HEAD(ids, entry);
3435 ncommits--;
3436 (*nids)--;
3437 color = (intptr_t)qid->data;
3439 /* Keep in sync with struct got_imsg_painted_commit! */
3440 if (imsg_add(wbuf, qid->id.sha1, SHA1_DIGEST_LENGTH) == -1)
3441 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3442 if (imsg_add(wbuf, &color, sizeof(color)) == -1)
3443 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3445 got_object_qid_free(qid);
3448 wbuf->fd = -1;
3449 imsg_close(ibuf, wbuf);
3451 return flush_imsg(ibuf);
3454 const struct got_error *
3455 got_privsep_send_painted_commits(struct imsgbuf *ibuf,
3456 struct got_object_id_queue *ids, int *nids,
3457 int present_in_pack, int flush)
3459 const struct got_error *err;
3460 size_t remain;
3462 if (*nids <= 0)
3463 return NULL;
3465 do {
3466 remain = (sizeof(struct got_imsg_painted_commits)) +
3467 *nids * sizeof(struct got_imsg_painted_commit);
3468 if (flush || remain >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
3469 err = send_painted_commits(ids, nids, remain,
3470 present_in_pack, ibuf);
3471 if (err)
3472 return err;
3474 } while (flush && *nids > 0);
3476 return NULL;
3479 const struct got_error *
3480 got_privsep_send_painting_commits_done(struct imsgbuf *ibuf)
3482 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_DONE,
3483 0, 0, -1, NULL, 0)
3484 == -1)
3485 return got_error_from_errno("imsg_compose "
3486 "COMMIT_PAINTING_DONE");
3488 return flush_imsg(ibuf);
3491 const struct got_error *
3492 got_privsep_recv_painted_commits(struct got_object_id_queue *new_ids,
3493 got_privsep_recv_painted_commit_cb cb, void *cb_arg, struct imsgbuf *ibuf)
3495 const struct got_error *err = NULL;
3496 struct imsg imsg;
3497 struct got_imsg_painted_commits icommits;
3498 struct got_imsg_painted_commit icommit;
3499 size_t datalen;
3500 int i;
3502 for (;;) {
3503 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3504 if (err)
3505 return err;
3507 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3508 if (imsg.hdr.type == GOT_IMSG_COMMIT_PAINTING_DONE)
3509 break;
3510 if (imsg.hdr.type != GOT_IMSG_PAINTED_COMMITS)
3511 return got_error(GOT_ERR_PRIVSEP_MSG);
3513 if (datalen < sizeof(icommits))
3514 return got_error(GOT_ERR_PRIVSEP_LEN);
3515 memcpy(&icommits, imsg.data, sizeof(icommits));
3516 if (icommits.ncommits * sizeof(icommit) < icommits.ncommits ||
3517 datalen < sizeof(icommits) +
3518 icommits.ncommits * sizeof(icommit))
3519 return got_error(GOT_ERR_PRIVSEP_LEN);
3521 for (i = 0; i < icommits.ncommits; i++) {
3522 memcpy(&icommit,
3523 (uint8_t *)imsg.data + sizeof(icommits) + i * sizeof(icommit),
3524 sizeof(icommit));
3526 if (icommits.present_in_pack) {
3527 struct got_object_id id;
3528 memcpy(id.sha1, icommit.id, SHA1_DIGEST_LENGTH);
3529 err = cb(cb_arg, &id, icommit.color);
3530 if (err)
3531 break;
3532 } else {
3533 struct got_object_qid *qid;
3534 err = got_object_qid_alloc_partial(&qid);
3535 if (err)
3536 break;
3537 memcpy(qid->id.sha1, icommit.id,
3538 SHA1_DIGEST_LENGTH);
3539 qid->data = (void *)icommit.color;
3540 STAILQ_INSERT_TAIL(new_ids, qid, entry);
3544 imsg_free(&imsg);
3547 return err;
3550 const struct got_error *
3551 got_privsep_unveil_exec_helpers(void)
3553 const char *helpers[] = {
3554 GOT_PATH_PROG_READ_PACK,
3555 GOT_PATH_PROG_READ_OBJECT,
3556 GOT_PATH_PROG_READ_COMMIT,
3557 GOT_PATH_PROG_READ_TREE,
3558 GOT_PATH_PROG_READ_BLOB,
3559 GOT_PATH_PROG_READ_TAG,
3560 GOT_PATH_PROG_READ_GITCONFIG,
3561 GOT_PATH_PROG_READ_GOTCONFIG,
3562 GOT_PATH_PROG_READ_PATCH,
3563 GOT_PATH_PROG_FETCH_PACK,
3564 GOT_PATH_PROG_INDEX_PACK,
3565 GOT_PATH_PROG_SEND_PACK,
3567 size_t i;
3569 for (i = 0; i < nitems(helpers); i++) {
3570 if (unveil(helpers[i], "x") == 0)
3571 continue;
3572 return got_error_from_errno2("unveil", helpers[i]);
3575 return NULL;
3578 void
3579 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3581 if (close(imsg_fds[0]) == -1) {
3582 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3583 _exit(1);
3586 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3587 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3588 _exit(1);
3591 closefrom(GOT_IMSG_FD_CHILD + 1);
3593 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3594 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3595 strerror(errno));
3596 _exit(1);