Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/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 <unistd.h>
33 #include <zlib.h>
34 #include <time.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_sha1.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"
51 #include "got_privsep.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
61 static const struct got_error *
62 poll_fd(int fd, int events, int timeout)
63 {
64 struct pollfd pfd[1];
65 struct timespec ts;
66 sigset_t sigset;
67 int n;
69 pfd[0].fd = fd;
70 pfd[0].events = events;
72 ts.tv_sec = timeout;
73 ts.tv_nsec = 0;
75 if (sigemptyset(&sigset) == -1)
76 return got_error_from_errno("sigemptyset");
77 if (sigaddset(&sigset, SIGWINCH) == -1)
78 return got_error_from_errno("sigaddset");
80 n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
81 if (n == -1)
82 return got_error_from_errno("ppoll");
83 if (n == 0)
84 return got_error(GOT_ERR_TIMEOUT);
85 if (pfd[0].revents & (POLLERR | POLLNVAL))
86 return got_error_from_errno("poll error");
87 if (pfd[0].revents & (events | POLLHUP))
88 return NULL;
90 return got_error(GOT_ERR_INTERRUPT);
91 }
93 static const struct got_error *
94 read_imsg(struct imsgbuf *ibuf)
95 {
96 const struct got_error *err;
97 size_t n;
99 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
100 if (err)
101 return err;
103 n = imsg_read(ibuf);
104 if (n == -1) {
105 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
106 return got_error(GOT_ERR_PRIVSEP_NO_FD);
107 return got_error(GOT_ERR_PRIVSEP_READ);
109 if (n == 0)
110 return got_error(GOT_ERR_PRIVSEP_PIPE);
112 return NULL;
115 const struct got_error *
116 got_privsep_wait_for_child(pid_t pid)
118 int child_status;
120 if (waitpid(pid, &child_status, 0) == -1)
121 return got_error_from_errno("waitpid");
123 if (!WIFEXITED(child_status))
124 return got_error(GOT_ERR_PRIVSEP_DIED);
126 if (WEXITSTATUS(child_status) != 0)
127 return got_error(GOT_ERR_PRIVSEP_EXIT);
129 return NULL;
132 static const struct got_error *
133 recv_imsg_error(struct imsg *imsg, size_t datalen)
135 struct got_imsg_error *ierr;
137 if (datalen != sizeof(*ierr))
138 return got_error(GOT_ERR_PRIVSEP_LEN);
140 ierr = imsg->data;
141 if (ierr->code == GOT_ERR_ERRNO) {
142 static struct got_error serr;
143 serr.code = GOT_ERR_ERRNO;
144 serr.msg = strerror(ierr->errno_code);
145 return &serr;
148 return got_error(ierr->code);
151 const struct got_error *
152 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
153 size_t min_datalen)
155 const struct got_error *err;
156 ssize_t n;
158 n = imsg_get(ibuf, imsg);
159 if (n == -1)
160 return got_error_from_errno("imsg_get");
162 while (n == 0) {
163 err = read_imsg(ibuf);
164 if (err)
165 return err;
166 n = imsg_get(ibuf, imsg);
167 if (n == -1)
168 return got_error_from_errno("imsg_get");
171 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
172 return got_error(GOT_ERR_PRIVSEP_LEN);
174 if (imsg->hdr.type == GOT_IMSG_ERROR) {
175 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
176 return recv_imsg_error(imsg, datalen);
179 return NULL;
182 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
183 void
184 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
186 const struct got_error *poll_err;
187 struct got_imsg_error ierr;
188 int ret;
190 ierr.code = err->code;
191 if (err->code == GOT_ERR_ERRNO)
192 ierr.errno_code = errno;
193 else
194 ierr.errno_code = 0;
195 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
196 if (ret == -1) {
197 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
198 getprogname(), err->code, err->msg, strerror(errno));
199 return;
202 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
203 if (poll_err) {
204 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
205 getprogname(), err->code, err->msg, poll_err->msg);
206 return;
209 ret = imsg_flush(ibuf);
210 if (ret == -1) {
211 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
212 getprogname(), err->code, err->msg, strerror(errno));
213 imsg_clear(ibuf);
214 return;
218 static const struct got_error *
219 flush_imsg(struct imsgbuf *ibuf)
221 const struct got_error *err;
223 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
224 if (err)
225 return err;
227 if (imsg_flush(ibuf) == -1) {
228 imsg_clear(ibuf);
229 return got_error_from_errno("imsg_flush");
232 return NULL;
235 const struct got_error *
236 got_privsep_flush_imsg(struct imsgbuf *ibuf)
238 return flush_imsg(ibuf);
241 const struct got_error *
242 got_privsep_send_stop(int fd)
244 const struct got_error *err = NULL;
245 struct imsgbuf ibuf;
247 imsg_init(&ibuf, fd);
249 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
250 return got_error_from_errno("imsg_compose STOP");
252 err = flush_imsg(&ibuf);
253 return err;
256 const struct got_error *
257 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
258 struct got_object_id *id)
260 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
261 id, sizeof(*id)) == -1)
262 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
264 return flush_imsg(ibuf);
267 const struct got_error *
268 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
269 struct got_object_id *id)
271 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
272 id, sizeof(*id)) == -1)
273 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
275 return flush_imsg(ibuf);
278 const struct got_error *
279 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
281 const struct got_error *err = NULL;
283 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
284 == -1) {
285 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
286 close(outfd);
287 return err;
290 return flush_imsg(ibuf);
293 const struct got_error *
294 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
295 uint8_t *data)
297 const struct got_error *err = NULL;
298 struct got_imsg_raw_obj iobj;
299 size_t len = sizeof(iobj);
300 struct ibuf *wbuf;
302 memset(&iobj, 0, sizeof(iobj));
303 iobj.hdrlen = hdrlen;
304 iobj.size = size;
306 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
307 len += (size_t)size + hdrlen;
309 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
310 if (wbuf == NULL) {
311 err = got_error_from_errno("imsg_create RAW_OBJECT");
312 return err;
315 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
316 return got_error_from_errno("imsg_add RAW_OBJECT");
318 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
319 if (imsg_add(wbuf, data, size + hdrlen) == -1)
320 return got_error_from_errno("imsg_add RAW_OBJECT");
323 wbuf->fd = -1;
324 imsg_close(ibuf, wbuf);
326 return flush_imsg(ibuf);
329 const struct got_error *
330 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
331 struct imsgbuf *ibuf)
333 const struct got_error *err = NULL;
334 struct imsg imsg;
335 struct got_imsg_raw_obj *iobj;
336 size_t datalen;
338 *outbuf = NULL;
340 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
341 if (err)
342 return err;
344 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
346 switch (imsg.hdr.type) {
347 case GOT_IMSG_RAW_OBJECT:
348 if (datalen < sizeof(*iobj)) {
349 err = got_error(GOT_ERR_PRIVSEP_LEN);
350 break;
352 iobj = imsg.data;
353 *size = iobj->size;
354 *hdrlen = iobj->hdrlen;
356 if (datalen == sizeof(*iobj)) {
357 /* Data has been written to file descriptor. */
358 break;
361 if (*size < 0 ||
362 *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
363 err = got_error(GOT_ERR_PRIVSEP_LEN);
364 break;
367 *outbuf = malloc(*size + *hdrlen);
368 if (*outbuf == NULL) {
369 err = got_error_from_errno("malloc");
370 break;
372 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
373 break;
374 default:
375 err = got_error(GOT_ERR_PRIVSEP_MSG);
376 break;
379 imsg_free(&imsg);
381 return err;
384 const struct got_error *
385 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
386 struct got_object_id *id, int pack_idx)
388 const struct got_error *err = NULL;
389 struct got_imsg_packed_object iobj;
390 void *data;
391 size_t len;
393 memset(&iobj, 0, sizeof(iobj));
394 if (pack_idx != -1) { /* commit is packed */
395 iobj.idx = pack_idx;
396 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
397 data = &iobj;
398 len = sizeof(iobj);
399 } else {
400 data = id;
401 len = sizeof(*id);
404 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
405 == -1) {
406 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
407 close(fd);
408 return err;
411 return flush_imsg(ibuf);
414 const struct got_error *
415 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
416 struct got_object_id *id, int pack_idx)
418 struct ibuf *wbuf;
419 size_t len;
421 if (pack_idx != -1)
422 len = sizeof(struct got_imsg_packed_object);
423 else
424 len = sizeof(*id);
426 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
427 if (wbuf == NULL)
428 return got_error_from_errno("imsg_create TREE_REQUEST");
430 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
431 return got_error_from_errno("imsg_add TREE_REQUEST");
433 if (pack_idx != -1) { /* tree is packed */
434 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
435 return got_error_from_errno("imsg_add TREE_REQUEST");
438 wbuf->fd = fd;
439 imsg_close(ibuf, wbuf);
441 return flush_imsg(ibuf);
444 const struct got_error *
445 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
446 struct got_object_id *id, int pack_idx)
448 struct got_imsg_packed_object iobj;
449 void *data;
450 size_t len;
452 memset(&iobj, 0, sizeof(iobj));
453 if (pack_idx != -1) { /* tag is packed */
454 iobj.idx = pack_idx;
455 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
456 data = &iobj;
457 len = sizeof(iobj);
458 } else {
459 data = id;
460 len = sizeof(*id);
463 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
464 == -1)
465 return got_error_from_errno("imsg_compose TAG_REQUEST");
467 return flush_imsg(ibuf);
470 const struct got_error *
471 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
472 struct got_object_id *id, int pack_idx)
474 const struct got_error *err = NULL;
475 struct got_imsg_packed_object iobj;
476 void *data;
477 size_t len;
479 memset(&iobj, 0, sizeof(iobj));
480 if (pack_idx != -1) { /* blob is packed */
481 iobj.idx = pack_idx;
482 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
483 data = &iobj;
484 len = sizeof(iobj);
485 } else {
486 data = id;
487 len = sizeof(*id);
490 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
491 == -1) {
492 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
493 close(infd);
494 return err;
497 return flush_imsg(ibuf);
500 const struct got_error *
501 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
503 const struct got_error *err = NULL;
505 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
506 == -1) {
507 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
508 close(outfd);
509 return err;
512 return flush_imsg(ibuf);
515 static const struct got_error *
516 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
518 const struct got_error *err = NULL;
520 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
521 err = got_error_from_errno("imsg_compose TMPFD");
522 close(fd);
523 return err;
526 return flush_imsg(ibuf);
529 const struct got_error *
530 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
532 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
535 const struct got_error *
536 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
538 struct got_imsg_object iobj;
540 memset(&iobj, 0, sizeof(iobj));
542 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
543 iobj.type = obj->type;
544 iobj.flags = obj->flags;
545 iobj.hdrlen = obj->hdrlen;
546 iobj.size = obj->size;
547 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
548 iobj.pack_offset = obj->pack_offset;
549 iobj.pack_idx = obj->pack_idx;
552 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
553 == -1)
554 return got_error_from_errno("imsg_compose OBJECT");
556 return flush_imsg(ibuf);
559 const struct got_error *
560 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
561 struct got_pathlist_head *have_refs, int fetch_all_branches,
562 struct got_pathlist_head *wanted_branches,
563 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
565 const struct got_error *err = NULL;
566 struct ibuf *wbuf;
567 size_t len;
568 struct got_pathlist_entry *pe;
569 struct got_imsg_fetch_request fetchreq;
571 memset(&fetchreq, 0, sizeof(fetchreq));
572 fetchreq.fetch_all_branches = fetch_all_branches;
573 fetchreq.list_refs_only = list_refs_only;
574 fetchreq.verbosity = verbosity;
575 TAILQ_FOREACH(pe, have_refs, entry)
576 fetchreq.n_have_refs++;
577 TAILQ_FOREACH(pe, wanted_branches, entry)
578 fetchreq.n_wanted_branches++;
579 TAILQ_FOREACH(pe, wanted_refs, entry)
580 fetchreq.n_wanted_refs++;
581 len = sizeof(struct got_imsg_fetch_request);
582 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
583 close(fd);
584 return got_error(GOT_ERR_NO_SPACE);
587 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
588 &fetchreq, sizeof(fetchreq)) == -1)
589 return got_error_from_errno(
590 "imsg_compose FETCH_SERVER_PROGRESS");
592 err = flush_imsg(ibuf);
593 if (err) {
594 close(fd);
595 return err;
597 fd = -1;
599 TAILQ_FOREACH(pe, have_refs, entry) {
600 const char *name = pe->path;
601 size_t name_len = pe->path_len;
602 struct got_object_id *id = pe->data;
604 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
605 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
606 if (wbuf == NULL)
607 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
609 /* Keep in sync with struct got_imsg_fetch_have_ref! */
610 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
611 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
612 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
613 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
614 if (imsg_add(wbuf, name, name_len) == -1)
615 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
617 wbuf->fd = -1;
618 imsg_close(ibuf, wbuf);
619 err = flush_imsg(ibuf);
620 if (err)
621 return err;
624 TAILQ_FOREACH(pe, wanted_branches, entry) {
625 const char *name = pe->path;
626 size_t name_len = pe->path_len;
628 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
629 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
630 len);
631 if (wbuf == NULL)
632 return got_error_from_errno(
633 "imsg_create FETCH_WANTED_BRANCH");
635 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
636 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
637 return got_error_from_errno(
638 "imsg_add FETCH_WANTED_BRANCH");
639 if (imsg_add(wbuf, name, name_len) == -1)
640 return got_error_from_errno(
641 "imsg_add FETCH_WANTED_BRANCH");
643 wbuf->fd = -1;
644 imsg_close(ibuf, wbuf);
645 err = flush_imsg(ibuf);
646 if (err)
647 return err;
650 TAILQ_FOREACH(pe, wanted_refs, entry) {
651 const char *name = pe->path;
652 size_t name_len = pe->path_len;
654 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
655 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
656 len);
657 if (wbuf == NULL)
658 return got_error_from_errno(
659 "imsg_create FETCH_WANTED_REF");
661 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
662 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
663 return got_error_from_errno(
664 "imsg_add FETCH_WANTED_REF");
665 if (imsg_add(wbuf, name, name_len) == -1)
666 return got_error_from_errno(
667 "imsg_add FETCH_WANTED_REF");
669 wbuf->fd = -1;
670 imsg_close(ibuf, wbuf);
671 err = flush_imsg(ibuf);
672 if (err)
673 return err;
677 return NULL;
681 const struct got_error *
682 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
684 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
687 const struct got_error *
688 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
689 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
690 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
692 const struct got_error *err = NULL;
693 struct imsg imsg;
694 size_t datalen;
695 struct got_imsg_fetch_symrefs *isymrefs = NULL;
696 size_t n, remain;
697 off_t off;
698 int i;
700 *done = 0;
701 *id = NULL;
702 *refname = NULL;
703 *server_progress = NULL;
704 *packfile_size = 0;
705 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
707 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
708 if (err)
709 return err;
711 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
712 switch (imsg.hdr.type) {
713 case GOT_IMSG_FETCH_SYMREFS:
714 if (datalen < sizeof(*isymrefs)) {
715 err = got_error(GOT_ERR_PRIVSEP_LEN);
716 break;
718 if (isymrefs != NULL) {
719 err = got_error(GOT_ERR_PRIVSEP_MSG);
720 break;
722 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
723 off = sizeof(*isymrefs);
724 remain = datalen - off;
725 for (n = 0; n < isymrefs->nsymrefs; n++) {
726 struct got_imsg_fetch_symref *s;
727 char *name, *target;
728 if (remain < sizeof(struct got_imsg_fetch_symref)) {
729 err = got_error(GOT_ERR_PRIVSEP_LEN);
730 goto done;
732 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
733 off += sizeof(*s);
734 remain -= sizeof(*s);
735 if (remain < s->name_len) {
736 err = got_error(GOT_ERR_PRIVSEP_LEN);
737 goto done;
739 name = strndup(imsg.data + off, s->name_len);
740 if (name == NULL) {
741 err = got_error_from_errno("strndup");
742 goto done;
744 off += s->name_len;
745 remain -= s->name_len;
746 if (remain < s->target_len) {
747 err = got_error(GOT_ERR_PRIVSEP_LEN);
748 free(name);
749 goto done;
751 target = strndup(imsg.data + off, s->target_len);
752 if (target == NULL) {
753 err = got_error_from_errno("strndup");
754 free(name);
755 goto done;
757 off += s->target_len;
758 remain -= s->target_len;
759 err = got_pathlist_append(symrefs, name, target);
760 if (err) {
761 free(name);
762 free(target);
763 goto done;
766 break;
767 case GOT_IMSG_FETCH_REF:
768 if (datalen <= SHA1_DIGEST_LENGTH) {
769 err = got_error(GOT_ERR_PRIVSEP_MSG);
770 break;
772 *id = malloc(sizeof(**id));
773 if (*id == NULL) {
774 err = got_error_from_errno("malloc");
775 break;
777 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
778 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
779 datalen - SHA1_DIGEST_LENGTH);
780 if (*refname == NULL) {
781 err = got_error_from_errno("strndup");
782 break;
784 break;
785 case GOT_IMSG_FETCH_SERVER_PROGRESS:
786 if (datalen == 0) {
787 err = got_error(GOT_ERR_PRIVSEP_LEN);
788 break;
790 *server_progress = strndup(imsg.data, datalen);
791 if (*server_progress == NULL) {
792 err = got_error_from_errno("strndup");
793 break;
795 for (i = 0; i < datalen; i++) {
796 if (!isprint((unsigned char)(*server_progress)[i]) &&
797 !isspace((unsigned char)(*server_progress)[i])) {
798 err = got_error(GOT_ERR_PRIVSEP_MSG);
799 free(*server_progress);
800 *server_progress = NULL;
801 goto done;
804 break;
805 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
806 if (datalen < sizeof(*packfile_size)) {
807 err = got_error(GOT_ERR_PRIVSEP_MSG);
808 break;
810 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
811 break;
812 case GOT_IMSG_FETCH_DONE:
813 if (datalen != SHA1_DIGEST_LENGTH) {
814 err = got_error(GOT_ERR_PRIVSEP_MSG);
815 break;
817 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
818 *done = 1;
819 break;
820 default:
821 err = got_error(GOT_ERR_PRIVSEP_MSG);
822 break;
824 done:
825 if (err) {
826 free(*id);
827 *id = NULL;
828 free(*refname);
829 *refname = NULL;
831 imsg_free(&imsg);
832 return err;
835 static const struct got_error *
836 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
837 int delete, struct imsgbuf *ibuf)
839 size_t len;
840 struct ibuf *wbuf;
842 len = sizeof(struct got_imsg_send_ref) + name_len;
843 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
844 if (wbuf == NULL)
845 return got_error_from_errno("imsg_create SEND_REF");
847 /* Keep in sync with struct got_imsg_send_ref! */
848 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
849 return got_error_from_errno("imsg_add SEND_REF");
850 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
851 return got_error_from_errno("imsg_add SEND_REF");
852 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
853 return got_error_from_errno("imsg_add SEND_REF");
854 if (imsg_add(wbuf, name, name_len) == -1)
855 return got_error_from_errno("imsg_add SEND_REF");
857 wbuf->fd = -1;
858 imsg_close(ibuf, wbuf);
859 return flush_imsg(ibuf);
862 const struct got_error *
863 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
864 struct got_pathlist_head *have_refs,
865 struct got_pathlist_head *delete_refs,
866 int verbosity)
868 const struct got_error *err = NULL;
869 struct got_pathlist_entry *pe;
870 struct got_imsg_send_request sendreq;
871 struct got_object_id zero_id;
873 memset(&zero_id, 0, sizeof(zero_id));
874 memset(&sendreq, 0, sizeof(sendreq));
875 sendreq.verbosity = verbosity;
876 TAILQ_FOREACH(pe, have_refs, entry)
877 sendreq.nrefs++;
878 TAILQ_FOREACH(pe, delete_refs, entry)
879 sendreq.nrefs++;
880 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
881 &sendreq, sizeof(sendreq)) == -1) {
882 err = got_error_from_errno(
883 "imsg_compose FETCH_SERVER_PROGRESS");
884 goto done;
887 err = flush_imsg(ibuf);
888 if (err)
889 goto done;
890 fd = -1;
892 TAILQ_FOREACH(pe, have_refs, entry) {
893 const char *name = pe->path;
894 size_t name_len = pe->path_len;
895 struct got_object_id *id = pe->data;
896 err = send_send_ref(name, name_len, id, 0, ibuf);
897 if (err)
898 goto done;
901 TAILQ_FOREACH(pe, delete_refs, entry) {
902 const char *name = pe->path;
903 size_t name_len = pe->path_len;
904 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
905 if (err)
906 goto done;
908 done:
909 if (fd != -1 && close(fd) == -1 && err == NULL)
910 err = got_error_from_errno("close");
911 return err;
915 const struct got_error *
916 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
917 struct imsgbuf *ibuf)
919 const struct got_error *err = NULL;
920 struct imsg imsg;
921 size_t datalen;
922 int done = 0;
923 struct got_imsg_send_remote_ref iremote_ref;
924 struct got_object_id *id = NULL;
925 char *refname = NULL;
926 struct got_pathlist_entry *new;
928 while (!done) {
929 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
930 if (err)
931 return err;
932 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
933 switch (imsg.hdr.type) {
934 case GOT_IMSG_SEND_REMOTE_REF:
935 if (datalen < sizeof(iremote_ref)) {
936 err = got_error(GOT_ERR_PRIVSEP_MSG);
937 goto done;
939 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
940 if (datalen != sizeof(iremote_ref) +
941 iremote_ref.name_len) {
942 err = got_error(GOT_ERR_PRIVSEP_MSG);
943 goto done;
945 id = malloc(sizeof(*id));
946 if (id == NULL) {
947 err = got_error_from_errno("malloc");
948 goto done;
950 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
951 refname = strndup(imsg.data + sizeof(iremote_ref),
952 datalen - sizeof(iremote_ref));
953 if (refname == NULL) {
954 err = got_error_from_errno("strndup");
955 goto done;
957 err = got_pathlist_insert(&new, remote_refs,
958 refname, id);
959 if (err)
960 goto done;
961 if (new == NULL) { /* duplicate which wasn't inserted */
962 free(id);
963 free(refname);
965 id = NULL;
966 refname = NULL;
967 break;
968 case GOT_IMSG_SEND_PACK_REQUEST:
969 if (datalen != 0) {
970 err = got_error(GOT_ERR_PRIVSEP_MSG);
971 goto done;
973 /* got-send-pack is now waiting for a pack file. */
974 done = 1;
975 break;
976 default:
977 err = got_error(GOT_ERR_PRIVSEP_MSG);
978 break;
981 done:
982 free(id);
983 free(refname);
984 imsg_free(&imsg);
985 return err;
988 const struct got_error *
989 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
991 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
994 const struct got_error *
995 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
996 int *success, char **refname, struct imsgbuf *ibuf)
998 const struct got_error *err = NULL;
999 struct imsg imsg;
1000 size_t datalen;
1001 struct got_imsg_send_ref_status iref_status;
1003 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1004 *done = 0;
1005 *success = 0;
1006 *refname = 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 err = got_error(GOT_ERR_PRIVSEP_MSG);
1029 break;
1031 *success = iref_status.success;
1032 *refname = strndup(imsg.data + sizeof(iref_status),
1033 iref_status.name_len);
1034 break;
1035 case GOT_IMSG_SEND_DONE:
1036 if (datalen != 0) {
1037 err = got_error(GOT_ERR_PRIVSEP_MSG);
1038 break;
1040 *done = 1;
1041 break;
1042 default:
1043 err = got_error(GOT_ERR_PRIVSEP_MSG);
1044 break;
1047 imsg_free(&imsg);
1048 return err;
1051 const struct got_error *
1052 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1053 int fd)
1055 const struct got_error *err = NULL;
1057 /* Keep in sync with struct got_imsg_index_pack_request */
1058 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1059 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1060 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1061 close(fd);
1062 return err;
1064 return flush_imsg(ibuf);
1067 const struct got_error *
1068 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1070 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1073 const struct got_error *
1074 got_privsep_recv_index_progress(int *done, int *nobj_total,
1075 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1076 struct imsgbuf *ibuf)
1078 const struct got_error *err = NULL;
1079 struct imsg imsg;
1080 struct got_imsg_index_pack_progress *iprogress;
1081 size_t datalen;
1083 *done = 0;
1084 *nobj_total = 0;
1085 *nobj_indexed = 0;
1086 *nobj_resolved = 0;
1088 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1089 if (err)
1090 return err;
1092 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1093 switch (imsg.hdr.type) {
1094 case GOT_IMSG_IDXPACK_PROGRESS:
1095 if (datalen < sizeof(*iprogress)) {
1096 err = got_error(GOT_ERR_PRIVSEP_LEN);
1097 break;
1099 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1100 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1101 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1102 err = got_error(GOT_ERR_RANGE);
1103 break;
1105 *nobj_total = iprogress->nobj_total;
1106 *nobj_indexed = iprogress->nobj_indexed;
1107 *nobj_loose = iprogress->nobj_loose;
1108 *nobj_resolved = iprogress->nobj_resolved;
1109 break;
1110 case GOT_IMSG_IDXPACK_DONE:
1111 if (datalen != 0) {
1112 err = got_error(GOT_ERR_PRIVSEP_LEN);
1113 break;
1115 *done = 1;
1116 break;
1117 default:
1118 err = got_error(GOT_ERR_PRIVSEP_MSG);
1119 break;
1122 imsg_free(&imsg);
1123 return err;
1126 const struct got_error *
1127 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1128 struct imsgbuf *ibuf)
1130 struct got_imsg_object *iobj;
1131 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1133 if (datalen != sizeof(*iobj))
1134 return got_error(GOT_ERR_PRIVSEP_LEN);
1135 iobj = imsg->data;
1137 if (iobj->pack_offset < 0)
1138 return got_error(GOT_ERR_PACK_OFFSET);
1140 *obj = calloc(1, sizeof(**obj));
1141 if (*obj == NULL)
1142 return got_error_from_errno("calloc");
1144 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1145 (*obj)->type = iobj->type;
1146 (*obj)->flags = iobj->flags;
1147 (*obj)->hdrlen = iobj->hdrlen;
1148 (*obj)->size = iobj->size;
1149 /* path_packfile is handled by caller */
1150 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1151 (*obj)->pack_offset = iobj->pack_offset;
1152 (*obj)->pack_idx = iobj->pack_idx;
1154 STAILQ_INIT(&(*obj)->deltas.entries);
1155 return NULL;
1158 const struct got_error *
1159 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1161 const struct got_error *err = NULL;
1162 struct imsg imsg;
1163 const size_t min_datalen =
1164 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1166 *obj = NULL;
1168 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1169 if (err)
1170 return err;
1172 switch (imsg.hdr.type) {
1173 case GOT_IMSG_OBJECT:
1174 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1175 break;
1176 default:
1177 err = got_error(GOT_ERR_PRIVSEP_MSG);
1178 break;
1181 imsg_free(&imsg);
1183 return err;
1186 static const struct got_error *
1187 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1188 size_t logmsg_len)
1190 const struct got_error *err = NULL;
1191 size_t offset, remain;
1193 offset = 0;
1194 remain = logmsg_len;
1195 while (remain > 0) {
1196 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1198 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1199 commit->logmsg + offset, n) == -1) {
1200 err = got_error_from_errno("imsg_compose "
1201 "COMMIT_LOGMSG");
1202 break;
1205 err = flush_imsg(ibuf);
1206 if (err)
1207 break;
1209 offset += n;
1210 remain -= n;
1213 return err;
1216 const struct got_error *
1217 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1219 const struct got_error *err = NULL;
1220 struct got_imsg_commit_object *icommit;
1221 uint8_t *buf;
1222 size_t len, total;
1223 struct got_object_qid *qid;
1224 size_t author_len = strlen(commit->author);
1225 size_t committer_len = strlen(commit->committer);
1226 size_t logmsg_len = strlen(commit->logmsg);
1228 total = sizeof(*icommit) + author_len + committer_len +
1229 commit->nparents * SHA1_DIGEST_LENGTH;
1231 buf = malloc(total);
1232 if (buf == NULL)
1233 return got_error_from_errno("malloc");
1235 icommit = (struct got_imsg_commit_object *)buf;
1236 memcpy(icommit->tree_id, commit->tree_id->sha1,
1237 sizeof(icommit->tree_id));
1238 icommit->author_len = author_len;
1239 icommit->author_time = commit->author_time;
1240 icommit->author_gmtoff = commit->author_gmtoff;
1241 icommit->committer_len = committer_len;
1242 icommit->committer_time = commit->committer_time;
1243 icommit->committer_gmtoff = commit->committer_gmtoff;
1244 icommit->logmsg_len = logmsg_len;
1245 icommit->nparents = commit->nparents;
1247 len = sizeof(*icommit);
1248 memcpy(buf + len, commit->author, author_len);
1249 len += author_len;
1250 memcpy(buf + len, commit->committer, committer_len);
1251 len += committer_len;
1252 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1253 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1254 len += SHA1_DIGEST_LENGTH;
1257 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1258 err = got_error_from_errno("imsg_compose COMMIT");
1259 goto done;
1262 if (logmsg_len == 0 ||
1263 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1264 err = flush_imsg(ibuf);
1265 if (err)
1266 goto done;
1268 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1269 done:
1270 free(buf);
1271 return err;
1274 static const struct got_error *
1275 get_commit_from_imsg(struct got_commit_object **commit,
1276 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1278 const struct got_error *err = NULL;
1279 struct got_imsg_commit_object *icommit;
1280 size_t len = 0;
1281 int i;
1283 if (datalen < sizeof(*icommit))
1284 return got_error(GOT_ERR_PRIVSEP_LEN);
1286 icommit = imsg->data;
1287 if (datalen != sizeof(*icommit) + icommit->author_len +
1288 icommit->committer_len +
1289 icommit->nparents * SHA1_DIGEST_LENGTH)
1290 return got_error(GOT_ERR_PRIVSEP_LEN);
1292 if (icommit->nparents < 0)
1293 return got_error(GOT_ERR_PRIVSEP_LEN);
1295 len += sizeof(*icommit);
1297 *commit = got_object_commit_alloc_partial();
1298 if (*commit == NULL)
1299 return got_error_from_errno(
1300 "got_object_commit_alloc_partial");
1302 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1303 SHA1_DIGEST_LENGTH);
1304 (*commit)->author_time = icommit->author_time;
1305 (*commit)->author_gmtoff = icommit->author_gmtoff;
1306 (*commit)->committer_time = icommit->committer_time;
1307 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1309 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1310 if ((*commit)->author == NULL) {
1311 err = got_error_from_errno("strndup");
1312 goto done;
1314 len += icommit->author_len;
1316 (*commit)->committer = strndup(imsg->data + len,
1317 icommit->committer_len);
1318 if ((*commit)->committer == NULL) {
1319 err = got_error_from_errno("strndup");
1320 goto done;
1322 len += icommit->committer_len;
1324 if (icommit->logmsg_len == 0) {
1325 (*commit)->logmsg = strdup("");
1326 if ((*commit)->logmsg == NULL) {
1327 err = got_error_from_errno("strdup");
1328 goto done;
1330 } else {
1331 size_t offset = 0, remain = icommit->logmsg_len;
1333 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1334 if ((*commit)->logmsg == NULL) {
1335 err = got_error_from_errno("malloc");
1336 goto done;
1338 while (remain > 0) {
1339 struct imsg imsg_log;
1340 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1341 remain);
1343 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1344 if (err)
1345 goto done;
1347 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1348 err = got_error(GOT_ERR_PRIVSEP_MSG);
1349 goto done;
1352 memcpy((*commit)->logmsg + offset,
1353 imsg_log.data, n);
1354 imsg_free(&imsg_log);
1355 offset += n;
1356 remain -= n;
1358 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1361 for (i = 0; i < icommit->nparents; i++) {
1362 struct got_object_qid *qid;
1364 err = got_object_qid_alloc_partial(&qid);
1365 if (err)
1366 break;
1367 memcpy(&qid->id, imsg->data + len +
1368 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1369 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1370 (*commit)->nparents++;
1372 done:
1373 if (err) {
1374 got_object_commit_close(*commit);
1375 *commit = NULL;
1377 return err;
1380 const struct got_error *
1381 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1383 const struct got_error *err = NULL;
1384 struct imsg imsg;
1385 size_t datalen;
1386 const size_t min_datalen =
1387 MIN(sizeof(struct got_imsg_error),
1388 sizeof(struct got_imsg_commit_object));
1390 *commit = NULL;
1392 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1393 if (err)
1394 return err;
1396 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1398 switch (imsg.hdr.type) {
1399 case GOT_IMSG_COMMIT:
1400 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1401 break;
1402 default:
1403 err = got_error(GOT_ERR_PRIVSEP_MSG);
1404 break;
1407 imsg_free(&imsg);
1409 return err;
1412 static const struct got_error *
1413 send_tree_entries_batch(struct imsgbuf *ibuf,
1414 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1416 struct ibuf *wbuf;
1417 struct got_imsg_tree_entries ientries;
1418 int i;
1420 memset(&ientries, 0, sizeof(ientries));
1422 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1423 if (wbuf == NULL)
1424 return got_error_from_errno("imsg_create TREE_ENTRY");
1426 ientries.nentries = idxN - idx0 + 1;
1427 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1428 return got_error_from_errno("imsg_add TREE_ENTRY");
1430 for (i = idx0; i <= idxN; i++) {
1431 struct got_parsed_tree_entry *pte = &entries[i];
1433 /* Keep in sync with struct got_imsg_tree_object definition! */
1434 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1435 return got_error_from_errno("imsg_add TREE_ENTRY");
1436 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1437 return got_error_from_errno("imsg_add TREE_ENTRY");
1438 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1439 return got_error_from_errno("imsg_add TREE_ENTRY");
1441 /* Remaining bytes are the entry's name. */
1442 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1443 return got_error_from_errno("imsg_add TREE_ENTRY");
1446 wbuf->fd = -1;
1447 imsg_close(ibuf, wbuf);
1448 return NULL;
1451 static const struct got_error *
1452 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1453 int nentries)
1455 const struct got_error *err = NULL;
1456 int i, j;
1457 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1459 i = 0;
1460 for (j = 0; j < nentries; j++) {
1461 struct got_parsed_tree_entry *pte = &entries[j];
1462 size_t len = SHA1_DIGEST_LENGTH + sizeof(pte->mode) +
1463 sizeof(pte->namelen) + pte->namelen;
1465 if (j > 0 &&
1466 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1467 err = send_tree_entries_batch(ibuf, entries,
1468 i, j - 1, entries_len);
1469 if (err)
1470 return err;
1471 i = j;
1472 entries_len = sizeof(struct got_imsg_tree_entries);
1475 entries_len += len;
1478 if (j > 0) {
1479 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1480 entries_len);
1481 if (err)
1482 return err;
1485 return NULL;
1488 const struct got_error *
1489 got_privsep_send_tree(struct imsgbuf *ibuf,
1490 struct got_parsed_tree_entry *entries, int nentries)
1492 const struct got_error *err = NULL;
1493 struct got_imsg_tree_object itree;
1495 memset(&itree, 0, sizeof(itree));
1496 itree.nentries = nentries;
1497 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1498 == -1)
1499 return got_error_from_errno("imsg_compose TREE");
1501 err = send_tree_entries(ibuf, entries, nentries);
1502 if (err)
1503 return err;
1505 return flush_imsg(ibuf);
1509 static const struct got_error *
1510 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1511 int *nentries)
1513 const struct got_error *err = NULL;
1514 struct got_imsg_tree_entries *ientries;
1515 struct got_tree_entry *te;
1516 size_t te_offset;
1517 size_t i;
1519 if (datalen <= sizeof(*ientries) ||
1520 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1521 return got_error(GOT_ERR_PRIVSEP_LEN);
1523 ientries = (struct got_imsg_tree_entries *)data;
1524 if (ientries->nentries > INT_MAX) {
1525 return got_error_msg(GOT_ERR_NO_SPACE,
1526 "too many tree entries");
1529 te_offset = sizeof(*ientries);
1530 for (i = 0; i < ientries->nentries; i++) {
1531 struct got_imsg_tree_entry ite;
1532 const char *te_name;
1533 uint8_t *buf = (uint8_t *)data + te_offset;
1535 if (te_offset >= datalen) {
1536 err = got_error(GOT_ERR_PRIVSEP_LEN);
1537 break;
1540 /* Might not be aligned, size is ~32 bytes. */
1541 memcpy(&ite, buf, sizeof(ite));
1543 if (ite.namelen >= sizeof(te->name)) {
1544 err = got_error(GOT_ERR_PRIVSEP_LEN);
1545 break;
1547 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1548 err = got_error(GOT_ERR_PRIVSEP_LEN);
1549 break;
1552 if (*nentries >= tree->nentries) {
1553 err = got_error(GOT_ERR_PRIVSEP_LEN);
1554 break;
1556 te = &tree->entries[*nentries];
1557 te_name = buf + sizeof(ite);
1558 memcpy(te->name, te_name, ite.namelen);
1559 te->name[ite.namelen] = '\0';
1560 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1561 te->mode = ite.mode;
1562 te->idx = *nentries;
1563 (*nentries)++;
1565 te_offset += sizeof(ite) + ite.namelen;
1568 return err;
1571 const struct got_error *
1572 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1574 const struct got_error *err = NULL;
1575 const size_t min_datalen =
1576 MIN(sizeof(struct got_imsg_error),
1577 sizeof(struct got_imsg_tree_object));
1578 struct got_imsg_tree_object *itree;
1579 int nentries = 0;
1581 *tree = NULL;
1583 while (*tree == NULL || nentries < (*tree)->nentries) {
1584 struct imsg imsg;
1585 size_t datalen;
1587 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1588 if (err)
1589 break;
1591 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1593 switch (imsg.hdr.type) {
1594 case GOT_IMSG_TREE:
1595 /* This message should only appear once. */
1596 if (*tree != NULL) {
1597 err = got_error(GOT_ERR_PRIVSEP_MSG);
1598 break;
1600 if (datalen != sizeof(*itree)) {
1601 err = got_error(GOT_ERR_PRIVSEP_LEN);
1602 break;
1604 itree = imsg.data;
1605 if (itree->nentries < 0) {
1606 err = got_error(GOT_ERR_PRIVSEP_LEN);
1607 break;
1609 *tree = malloc(sizeof(**tree));
1610 if (*tree == NULL) {
1611 err = got_error_from_errno("malloc");
1612 break;
1614 (*tree)->entries = calloc(itree->nentries,
1615 sizeof(struct got_tree_entry));
1616 if ((*tree)->entries == NULL) {
1617 err = got_error_from_errno("malloc");
1618 free(*tree);
1619 *tree = NULL;
1620 break;
1622 (*tree)->nentries = itree->nentries;
1623 (*tree)->refcnt = 0;
1624 break;
1625 case GOT_IMSG_TREE_ENTRIES:
1626 /* This message should be preceeded by GOT_IMSG_TREE. */
1627 if (*tree == NULL) {
1628 err = got_error(GOT_ERR_PRIVSEP_MSG);
1629 break;
1631 err = recv_tree_entries(imsg.data, datalen,
1632 *tree, &nentries);
1633 break;
1634 default:
1635 err = got_error(GOT_ERR_PRIVSEP_MSG);
1636 break;
1639 imsg_free(&imsg);
1640 if (err)
1641 break;
1644 if (*tree && (*tree)->nentries != nentries) {
1645 if (err == NULL)
1646 err = got_error(GOT_ERR_PRIVSEP_LEN);
1647 got_object_tree_close(*tree);
1648 *tree = NULL;
1651 return err;
1654 const struct got_error *
1655 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1656 const uint8_t *data)
1658 struct got_imsg_blob iblob;
1660 memset(&iblob, 0, sizeof(iblob));
1661 iblob.size = size;
1662 iblob.hdrlen = hdrlen;
1664 if (data) {
1665 uint8_t *buf;
1667 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1668 return got_error(GOT_ERR_NO_SPACE);
1670 buf = malloc(sizeof(iblob) + size);
1671 if (buf == NULL)
1672 return got_error_from_errno("malloc");
1674 memcpy(buf, &iblob, sizeof(iblob));
1675 memcpy(buf + sizeof(iblob), data, size);
1676 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1677 sizeof(iblob) + size) == -1) {
1678 free(buf);
1679 return got_error_from_errno("imsg_compose BLOB");
1681 free(buf);
1682 } else {
1683 /* Data has already been written to file descriptor. */
1684 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1685 sizeof(iblob)) == -1)
1686 return got_error_from_errno("imsg_compose BLOB");
1690 return flush_imsg(ibuf);
1693 const struct got_error *
1694 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1695 struct imsgbuf *ibuf)
1697 const struct got_error *err = NULL;
1698 struct imsg imsg;
1699 struct got_imsg_blob *iblob;
1700 size_t datalen;
1702 *outbuf = NULL;
1704 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1705 if (err)
1706 return err;
1708 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1710 switch (imsg.hdr.type) {
1711 case GOT_IMSG_BLOB:
1712 if (datalen < sizeof(*iblob)) {
1713 err = got_error(GOT_ERR_PRIVSEP_LEN);
1714 break;
1716 iblob = imsg.data;
1717 *size = iblob->size;
1718 *hdrlen = iblob->hdrlen;
1720 if (datalen == sizeof(*iblob)) {
1721 /* Data has been written to file descriptor. */
1722 break;
1725 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1726 *size > datalen + sizeof(*iblob)) {
1727 err = got_error(GOT_ERR_PRIVSEP_LEN);
1728 break;
1731 *outbuf = malloc(*size);
1732 if (*outbuf == NULL) {
1733 err = got_error_from_errno("malloc");
1734 break;
1736 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1737 break;
1738 default:
1739 err = got_error(GOT_ERR_PRIVSEP_MSG);
1740 break;
1743 imsg_free(&imsg);
1745 return err;
1748 static const struct got_error *
1749 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1751 const struct got_error *err = NULL;
1752 size_t offset, remain;
1754 offset = 0;
1755 remain = tagmsg_len;
1756 while (remain > 0) {
1757 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1759 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1760 tag->tagmsg + offset, n) == -1) {
1761 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1762 break;
1765 err = flush_imsg(ibuf);
1766 if (err)
1767 break;
1769 offset += n;
1770 remain -= n;
1773 return err;
1776 const struct got_error *
1777 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1779 const struct got_error *err = NULL;
1780 struct got_imsg_tag_object *itag;
1781 uint8_t *buf;
1782 size_t len, total;
1783 size_t tag_len = strlen(tag->tag);
1784 size_t tagger_len = strlen(tag->tagger);
1785 size_t tagmsg_len = strlen(tag->tagmsg);
1787 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1789 buf = malloc(total);
1790 if (buf == NULL)
1791 return got_error_from_errno("malloc");
1793 itag = (struct got_imsg_tag_object *)buf;
1794 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1795 itag->obj_type = tag->obj_type;
1796 itag->tag_len = tag_len;
1797 itag->tagger_len = tagger_len;
1798 itag->tagger_time = tag->tagger_time;
1799 itag->tagger_gmtoff = tag->tagger_gmtoff;
1800 itag->tagmsg_len = tagmsg_len;
1802 len = sizeof(*itag);
1803 memcpy(buf + len, tag->tag, tag_len);
1804 len += tag_len;
1805 memcpy(buf + len, tag->tagger, tagger_len);
1806 len += tagger_len;
1808 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1809 err = got_error_from_errno("imsg_compose TAG");
1810 goto done;
1813 if (tagmsg_len == 0 ||
1814 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1815 err = flush_imsg(ibuf);
1816 if (err)
1817 goto done;
1819 err = send_tagmsg(ibuf, tag, tagmsg_len);
1820 done:
1821 free(buf);
1822 return err;
1825 const struct got_error *
1826 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1828 const struct got_error *err = NULL;
1829 struct imsg imsg;
1830 struct got_imsg_tag_object *itag;
1831 size_t len, datalen;
1832 const size_t min_datalen =
1833 MIN(sizeof(struct got_imsg_error),
1834 sizeof(struct got_imsg_tag_object));
1836 *tag = NULL;
1838 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1839 if (err)
1840 return err;
1842 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1843 len = 0;
1845 switch (imsg.hdr.type) {
1846 case GOT_IMSG_TAG:
1847 if (datalen < sizeof(*itag)) {
1848 err = got_error(GOT_ERR_PRIVSEP_LEN);
1849 break;
1851 itag = imsg.data;
1852 if (datalen != sizeof(*itag) + itag->tag_len +
1853 itag->tagger_len) {
1854 err = got_error(GOT_ERR_PRIVSEP_LEN);
1855 break;
1857 len += sizeof(*itag);
1859 *tag = calloc(1, sizeof(**tag));
1860 if (*tag == NULL) {
1861 err = got_error_from_errno("calloc");
1862 break;
1865 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1867 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1868 if ((*tag)->tag == NULL) {
1869 err = got_error_from_errno("strndup");
1870 break;
1872 len += itag->tag_len;
1874 (*tag)->obj_type = itag->obj_type;
1875 (*tag)->tagger_time = itag->tagger_time;
1876 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1878 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1879 if ((*tag)->tagger == NULL) {
1880 err = got_error_from_errno("strndup");
1881 break;
1883 len += itag->tagger_len;
1885 if (itag->tagmsg_len == 0) {
1886 (*tag)->tagmsg = strdup("");
1887 if ((*tag)->tagmsg == NULL) {
1888 err = got_error_from_errno("strdup");
1889 break;
1891 } else {
1892 size_t offset = 0, remain = itag->tagmsg_len;
1894 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1895 if ((*tag)->tagmsg == NULL) {
1896 err = got_error_from_errno("malloc");
1897 break;
1899 while (remain > 0) {
1900 struct imsg imsg_log;
1901 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1902 remain);
1904 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1905 if (err)
1906 return err;
1908 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1909 return got_error(GOT_ERR_PRIVSEP_MSG);
1911 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1912 n);
1913 imsg_free(&imsg_log);
1914 offset += n;
1915 remain -= n;
1917 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1920 break;
1921 default:
1922 err = got_error(GOT_ERR_PRIVSEP_MSG);
1923 break;
1926 imsg_free(&imsg);
1928 return err;
1931 const struct got_error *
1932 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1933 struct got_packidx *packidx)
1935 const struct got_error *err = NULL;
1936 struct got_imsg_packidx ipackidx;
1937 struct got_imsg_pack ipack;
1938 int fd;
1940 memset(&ipackidx, 0, sizeof(ipackidx));
1941 memset(&ipack, 0, sizeof(ipack));
1943 ipackidx.len = packidx->len;
1944 ipackidx.packfile_size = pack->filesize;
1945 fd = dup(packidx->fd);
1946 if (fd == -1)
1947 return got_error_from_errno("dup");
1949 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1950 sizeof(ipackidx)) == -1) {
1951 err = got_error_from_errno("imsg_compose PACKIDX");
1952 close(fd);
1953 return err;
1956 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1957 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1958 return got_error(GOT_ERR_NO_SPACE);
1959 ipack.filesize = pack->filesize;
1961 fd = dup(pack->fd);
1962 if (fd == -1)
1963 return got_error_from_errno("dup");
1965 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1966 == -1) {
1967 err = got_error_from_errno("imsg_compose PACK");
1968 close(fd);
1969 return err;
1972 return flush_imsg(ibuf);
1975 const struct got_error *
1976 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1977 struct got_object_id *id)
1979 struct got_imsg_packed_object iobj;
1981 memset(&iobj, 0, sizeof(iobj));
1982 iobj.idx = idx;
1983 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1985 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1986 &iobj, sizeof(iobj)) == -1)
1987 return got_error_from_errno("imsg_compose "
1988 "PACKED_OBJECT_REQUEST");
1990 return flush_imsg(ibuf);
1993 const struct got_error *
1994 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1995 struct got_object_id *id)
1997 struct got_imsg_packed_object iobj;
1999 memset(&iobj, 0, sizeof(iobj));
2000 iobj.idx = idx;
2001 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2003 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2004 &iobj, sizeof(iobj)) == -1)
2005 return got_error_from_errno("imsg_compose "
2006 "PACKED_OBJECT_REQUEST");
2008 return flush_imsg(ibuf);
2011 const struct got_error *
2012 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2014 const struct got_error *err = NULL;
2016 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2017 NULL, 0) == -1) {
2018 err = got_error_from_errno("imsg_compose "
2019 "GITCONFIG_PARSE_REQUEST");
2020 close(fd);
2021 return err;
2024 return flush_imsg(ibuf);
2027 const struct got_error *
2028 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2030 if (imsg_compose(ibuf,
2031 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2032 NULL, 0) == -1)
2033 return got_error_from_errno("imsg_compose "
2034 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2036 return flush_imsg(ibuf);
2039 const struct got_error *
2040 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2042 if (imsg_compose(ibuf,
2043 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2044 NULL, 0) == -1)
2045 return got_error_from_errno("imsg_compose "
2046 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2048 return flush_imsg(ibuf);
2052 const struct got_error *
2053 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2055 if (imsg_compose(ibuf,
2056 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2057 return got_error_from_errno("imsg_compose "
2058 "GITCONFIG_AUTHOR_NAME_REQUEST");
2060 return flush_imsg(ibuf);
2063 const struct got_error *
2064 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2066 if (imsg_compose(ibuf,
2067 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2068 return got_error_from_errno("imsg_compose "
2069 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2071 return flush_imsg(ibuf);
2074 const struct got_error *
2075 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2077 if (imsg_compose(ibuf,
2078 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2079 return got_error_from_errno("imsg_compose "
2080 "GITCONFIG_REMOTE_REQUEST");
2082 return flush_imsg(ibuf);
2085 const struct got_error *
2086 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2088 if (imsg_compose(ibuf,
2089 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2090 return got_error_from_errno("imsg_compose "
2091 "GITCONFIG_OWNER_REQUEST");
2093 return flush_imsg(ibuf);
2096 const struct got_error *
2097 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2099 const struct got_error *err = NULL;
2100 struct imsg imsg;
2101 size_t datalen;
2103 *str = NULL;
2105 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2106 if (err)
2107 return err;
2108 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2110 switch (imsg.hdr.type) {
2111 case GOT_IMSG_GITCONFIG_STR_VAL:
2112 if (datalen == 0)
2113 break;
2114 /* datalen does not include terminating \0 */
2115 *str = malloc(datalen + 1);
2116 if (*str == NULL) {
2117 err = got_error_from_errno("malloc");
2118 break;
2120 memcpy(*str, imsg.data, datalen);
2121 (*str)[datalen] = '\0';
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_int(int *val, struct imsgbuf *ibuf)
2135 const struct got_error *err = NULL;
2136 struct imsg imsg;
2137 size_t datalen;
2138 const size_t min_datalen =
2139 MIN(sizeof(struct got_imsg_error), sizeof(int));
2141 *val = 0;
2143 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2144 if (err)
2145 return err;
2146 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2148 switch (imsg.hdr.type) {
2149 case GOT_IMSG_GITCONFIG_INT_VAL:
2150 if (datalen != sizeof(*val)) {
2151 err = got_error(GOT_ERR_PRIVSEP_LEN);
2152 break;
2154 memcpy(val, imsg.data, sizeof(*val));
2155 break;
2156 default:
2157 err = got_error(GOT_ERR_PRIVSEP_MSG);
2158 break;
2161 imsg_free(&imsg);
2162 return err;
2165 static void
2166 free_remote_data(struct got_remote_repo *remote)
2168 int i;
2170 free(remote->name);
2171 free(remote->fetch_url);
2172 free(remote->send_url);
2173 for (i = 0; i < remote->nfetch_branches; i++)
2174 free(remote->fetch_branches[i]);
2175 free(remote->fetch_branches);
2176 for (i = 0; i < remote->nsend_branches; i++)
2177 free(remote->send_branches[i]);
2178 free(remote->send_branches);
2179 for (i = 0; i < remote->nfetch_refs; i++)
2180 free(remote->fetch_refs[i]);
2181 free(remote->fetch_refs);
2184 const struct got_error *
2185 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2186 int *nremotes, struct imsgbuf *ibuf)
2188 const struct got_error *err = NULL;
2189 struct imsg imsg;
2190 size_t datalen;
2191 struct got_imsg_remotes iremotes;
2192 struct got_imsg_remote iremote;
2194 *remotes = NULL;
2195 *nremotes = 0;
2196 iremotes.nremotes = 0;
2198 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2199 if (err)
2200 return err;
2201 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2203 switch (imsg.hdr.type) {
2204 case GOT_IMSG_GITCONFIG_REMOTES:
2205 if (datalen != sizeof(iremotes)) {
2206 err = got_error(GOT_ERR_PRIVSEP_LEN);
2207 break;
2209 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2210 if (iremotes.nremotes == 0) {
2211 imsg_free(&imsg);
2212 return NULL;
2214 break;
2215 default:
2216 imsg_free(&imsg);
2217 return got_error(GOT_ERR_PRIVSEP_MSG);
2220 imsg_free(&imsg);
2222 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2223 if (*remotes == NULL)
2224 return got_error_from_errno("recallocarray");
2226 while (*nremotes < iremotes.nremotes) {
2227 struct got_remote_repo *remote;
2229 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2230 if (err)
2231 break;
2232 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2234 switch (imsg.hdr.type) {
2235 case GOT_IMSG_GITCONFIG_REMOTE:
2236 remote = &(*remotes)[*nremotes];
2237 memset(remote, 0, sizeof(*remote));
2238 if (datalen < sizeof(iremote)) {
2239 err = got_error(GOT_ERR_PRIVSEP_LEN);
2240 break;
2242 memcpy(&iremote, imsg.data, sizeof(iremote));
2243 if (iremote.name_len == 0 ||
2244 iremote.fetch_url_len == 0 ||
2245 iremote.send_url_len == 0 ||
2246 (sizeof(iremote) + iremote.name_len +
2247 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2248 err = got_error(GOT_ERR_PRIVSEP_LEN);
2249 break;
2251 remote->name = strndup(imsg.data + sizeof(iremote),
2252 iremote.name_len);
2253 if (remote->name == NULL) {
2254 err = got_error_from_errno("strndup");
2255 break;
2257 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2258 iremote.name_len, iremote.fetch_url_len);
2259 if (remote->fetch_url == NULL) {
2260 err = got_error_from_errno("strndup");
2261 free_remote_data(remote);
2262 break;
2264 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2265 iremote.name_len + iremote.fetch_url_len,
2266 iremote.send_url_len);
2267 if (remote->send_url == NULL) {
2268 err = got_error_from_errno("strndup");
2269 free_remote_data(remote);
2270 break;
2272 remote->mirror_references = iremote.mirror_references;
2273 remote->fetch_all_branches = iremote.fetch_all_branches;
2274 remote->nfetch_branches = 0;
2275 remote->fetch_branches = NULL;
2276 remote->nsend_branches = 0;
2277 remote->send_branches = NULL;
2278 remote->nfetch_refs = 0;
2279 remote->fetch_refs = NULL;
2280 (*nremotes)++;
2281 break;
2282 default:
2283 err = got_error(GOT_ERR_PRIVSEP_MSG);
2284 break;
2287 imsg_free(&imsg);
2288 if (err)
2289 break;
2292 if (err) {
2293 int i;
2294 for (i = 0; i < *nremotes; i++)
2295 free_remote_data(&(*remotes)[i]);
2296 free(*remotes);
2297 *remotes = NULL;
2298 *nremotes = 0;
2300 return err;
2303 const struct got_error *
2304 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2306 const struct got_error *err = NULL;
2308 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2309 NULL, 0) == -1) {
2310 err = got_error_from_errno("imsg_compose "
2311 "GOTCONFIG_PARSE_REQUEST");
2312 close(fd);
2313 return err;
2316 return flush_imsg(ibuf);
2319 const struct got_error *
2320 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2322 if (imsg_compose(ibuf,
2323 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2324 return got_error_from_errno("imsg_compose "
2325 "GOTCONFIG_AUTHOR_REQUEST");
2327 return flush_imsg(ibuf);
2330 const struct got_error *
2331 got_privsep_send_gotconfig_allowed_signers_req(struct imsgbuf *ibuf)
2333 if (imsg_compose(ibuf,
2334 GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2335 return got_error_from_errno("imsg_compose "
2336 "GOTCONFIG_ALLOWEDSIGNERS_REQUEST");
2338 return flush_imsg(ibuf);
2341 const struct got_error *
2342 got_privsep_send_gotconfig_revoked_signers_req(struct imsgbuf *ibuf)
2344 if (imsg_compose(ibuf,
2345 GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST, 0, 0, -1, NULL, 0) == -1)
2346 return got_error_from_errno("imsg_compose "
2347 "GOTCONFIG_REVOKEDSIGNERS_REQUEST");
2349 return flush_imsg(ibuf);
2352 const struct got_error *
2353 got_privsep_send_gotconfig_signer_id_req(struct imsgbuf *ibuf)
2355 if (imsg_compose(ibuf,
2356 GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST, 0, 0, -1, NULL, 0) == -1)
2357 return got_error_from_errno("imsg_compose "
2358 "GOTCONFIG_SIGNERID_REQUEST");
2360 return flush_imsg(ibuf);
2363 const struct got_error *
2364 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2366 if (imsg_compose(ibuf,
2367 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2368 return got_error_from_errno("imsg_compose "
2369 "GOTCONFIG_REMOTE_REQUEST");
2371 return flush_imsg(ibuf);
2374 const struct got_error *
2375 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2377 const struct got_error *err = NULL;
2378 struct imsg imsg;
2379 size_t datalen;
2381 *str = NULL;
2383 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2384 if (err)
2385 return err;
2386 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2388 switch (imsg.hdr.type) {
2389 case GOT_IMSG_GOTCONFIG_STR_VAL:
2390 if (datalen == 0)
2391 break;
2392 /* datalen does not include terminating \0 */
2393 *str = malloc(datalen + 1);
2394 if (*str == NULL) {
2395 err = got_error_from_errno("malloc");
2396 break;
2398 memcpy(*str, imsg.data, datalen);
2399 (*str)[datalen] = '\0';
2400 break;
2401 default:
2402 err = got_error(GOT_ERR_PRIVSEP_MSG);
2403 break;
2406 imsg_free(&imsg);
2407 return err;
2410 const struct got_error *
2411 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2412 int *nremotes, struct imsgbuf *ibuf)
2414 const struct got_error *err = NULL;
2415 struct imsg imsg;
2416 size_t datalen;
2417 struct got_imsg_remotes iremotes;
2418 struct got_imsg_remote iremote;
2419 const size_t min_datalen =
2420 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2422 *remotes = NULL;
2423 *nremotes = 0;
2424 iremotes.nremotes = 0;
2426 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2427 if (err)
2428 return err;
2429 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2431 switch (imsg.hdr.type) {
2432 case GOT_IMSG_GOTCONFIG_REMOTES:
2433 if (datalen != sizeof(iremotes)) {
2434 err = got_error(GOT_ERR_PRIVSEP_LEN);
2435 break;
2437 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2438 if (iremotes.nremotes < 0) {
2439 err = got_error(GOT_ERR_PRIVSEP_LEN);
2440 break;
2442 if (iremotes.nremotes == 0) {
2443 imsg_free(&imsg);
2444 return NULL;
2446 break;
2447 default:
2448 imsg_free(&imsg);
2449 return got_error(GOT_ERR_PRIVSEP_MSG);
2452 imsg_free(&imsg);
2454 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2455 if (*remotes == NULL)
2456 return got_error_from_errno("recallocarray");
2458 while (*nremotes < iremotes.nremotes) {
2459 struct got_remote_repo *remote;
2460 const size_t min_datalen =
2461 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2462 int i;
2464 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2465 if (err)
2466 break;
2467 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2469 switch (imsg.hdr.type) {
2470 case GOT_IMSG_GOTCONFIG_REMOTE:
2471 remote = &(*remotes)[*nremotes];
2472 memset(remote, 0, sizeof(*remote));
2473 if (datalen < sizeof(iremote)) {
2474 err = got_error(GOT_ERR_PRIVSEP_LEN);
2475 break;
2477 memcpy(&iremote, imsg.data, sizeof(iremote));
2478 if (iremote.name_len == 0 ||
2479 (iremote.fetch_url_len == 0 &&
2480 iremote.send_url_len == 0) ||
2481 (sizeof(iremote) + iremote.name_len +
2482 iremote.fetch_url_len + iremote.send_url_len) >
2483 datalen) {
2484 err = got_error(GOT_ERR_PRIVSEP_LEN);
2485 break;
2487 remote->name = strndup(imsg.data + sizeof(iremote),
2488 iremote.name_len);
2489 if (remote->name == NULL) {
2490 err = got_error_from_errno("strndup");
2491 break;
2493 remote->fetch_url = strndup(imsg.data +
2494 sizeof(iremote) + iremote.name_len,
2495 iremote.fetch_url_len);
2496 if (remote->fetch_url == NULL) {
2497 err = got_error_from_errno("strndup");
2498 free_remote_data(remote);
2499 break;
2501 remote->send_url = strndup(imsg.data +
2502 sizeof(iremote) + iremote.name_len +
2503 iremote.fetch_url_len, iremote.send_url_len);
2504 if (remote->send_url == NULL) {
2505 err = got_error_from_errno("strndup");
2506 free_remote_data(remote);
2507 break;
2509 remote->mirror_references = iremote.mirror_references;
2510 remote->fetch_all_branches = iremote.fetch_all_branches;
2511 if (iremote.nfetch_branches > 0) {
2512 remote->fetch_branches = recallocarray(NULL, 0,
2513 iremote.nfetch_branches, sizeof(char *));
2514 if (remote->fetch_branches == NULL) {
2515 err = got_error_from_errno("calloc");
2516 free_remote_data(remote);
2517 break;
2520 remote->nfetch_branches = 0;
2521 for (i = 0; i < iremote.nfetch_branches; i++) {
2522 char *branch;
2523 err = got_privsep_recv_gotconfig_str(&branch,
2524 ibuf);
2525 if (err) {
2526 free_remote_data(remote);
2527 goto done;
2529 remote->fetch_branches[i] = branch;
2530 remote->nfetch_branches++;
2532 if (iremote.nsend_branches > 0) {
2533 remote->send_branches = recallocarray(NULL, 0,
2534 iremote.nsend_branches, sizeof(char *));
2535 if (remote->send_branches == NULL) {
2536 err = got_error_from_errno("calloc");
2537 free_remote_data(remote);
2538 break;
2541 remote->nsend_branches = 0;
2542 for (i = 0; i < iremote.nsend_branches; i++) {
2543 char *branch;
2544 err = got_privsep_recv_gotconfig_str(&branch,
2545 ibuf);
2546 if (err) {
2547 free_remote_data(remote);
2548 goto done;
2550 remote->send_branches[i] = branch;
2551 remote->nsend_branches++;
2553 if (iremote.nfetch_refs > 0) {
2554 remote->fetch_refs = recallocarray(NULL, 0,
2555 iremote.nfetch_refs, sizeof(char *));
2556 if (remote->fetch_refs == NULL) {
2557 err = got_error_from_errno("calloc");
2558 free_remote_data(remote);
2559 break;
2562 remote->nfetch_refs = 0;
2563 for (i = 0; i < iremote.nfetch_refs; i++) {
2564 char *ref;
2565 err = got_privsep_recv_gotconfig_str(&ref,
2566 ibuf);
2567 if (err) {
2568 free_remote_data(remote);
2569 goto done;
2571 remote->fetch_refs[i] = ref;
2572 remote->nfetch_refs++;
2574 (*nremotes)++;
2575 break;
2576 default:
2577 err = got_error(GOT_ERR_PRIVSEP_MSG);
2578 break;
2581 imsg_free(&imsg);
2582 if (err)
2583 break;
2585 done:
2586 if (err) {
2587 int i;
2588 for (i = 0; i < *nremotes; i++)
2589 free_remote_data(&(*remotes)[i]);
2590 free(*remotes);
2591 *remotes = NULL;
2592 *nremotes = 0;
2594 return err;
2597 const struct got_error *
2598 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2599 struct got_object_id *id, int idx, const char *path)
2601 struct ibuf *wbuf;
2602 size_t path_len = strlen(path) + 1;
2604 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2605 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2606 if (wbuf == NULL)
2607 return got_error_from_errno(
2608 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2609 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2610 return got_error_from_errno("imsg_add "
2611 "COMMIT_TRAVERSAL_REQUEST");
2612 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2613 return got_error_from_errno("imsg_add "
2614 "COMMIT_TRAVERSAL_REQUEST");
2615 if (imsg_add(wbuf, path, path_len) == -1)
2616 return got_error_from_errno("imsg_add "
2617 "COMMIT_TRAVERSAL_REQUEST");
2619 wbuf->fd = -1;
2620 imsg_close(ibuf, wbuf);
2622 return flush_imsg(ibuf);
2625 const struct got_error *
2626 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2627 struct got_object_id **changed_commit_id,
2628 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2630 const struct got_error *err = NULL;
2631 struct imsg imsg;
2632 struct got_imsg_traversed_commits *icommits;
2633 size_t datalen;
2634 int i, done = 0;
2636 *changed_commit = NULL;
2637 *changed_commit_id = NULL;
2639 while (!done) {
2640 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2641 if (err)
2642 return err;
2644 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2645 switch (imsg.hdr.type) {
2646 case GOT_IMSG_TRAVERSED_COMMITS:
2647 icommits = imsg.data;
2648 if (datalen != sizeof(*icommits) +
2649 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2650 err = got_error(GOT_ERR_PRIVSEP_LEN);
2651 break;
2653 for (i = 0; i < icommits->ncommits; i++) {
2654 struct got_object_qid *qid;
2655 uint8_t *sha1 = (uint8_t *)imsg.data +
2656 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2657 err = got_object_qid_alloc_partial(&qid);
2658 if (err)
2659 break;
2660 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2661 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2663 /* The last commit may contain a change. */
2664 if (i == icommits->ncommits - 1) {
2665 *changed_commit_id =
2666 got_object_id_dup(&qid->id);
2667 if (*changed_commit_id == NULL) {
2668 err = got_error_from_errno(
2669 "got_object_id_dup");
2670 break;
2674 break;
2675 case GOT_IMSG_COMMIT:
2676 if (*changed_commit_id == NULL) {
2677 err = got_error(GOT_ERR_PRIVSEP_MSG);
2678 break;
2680 err = get_commit_from_imsg(changed_commit, &imsg,
2681 datalen, ibuf);
2682 break;
2683 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2684 done = 1;
2685 break;
2686 default:
2687 err = got_error(GOT_ERR_PRIVSEP_MSG);
2688 break;
2691 imsg_free(&imsg);
2692 if (err)
2693 break;
2696 if (err)
2697 got_object_id_queue_free(commit_ids);
2698 return err;
2701 const struct got_error *
2702 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2703 struct got_object_id *tree_id, const char *path,
2704 struct got_parsed_tree_entry *entries, int nentries)
2706 const struct got_error *err = NULL;
2707 struct ibuf *wbuf;
2708 size_t path_len = strlen(path);
2709 size_t msglen;
2711 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2712 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2713 if (wbuf == NULL)
2714 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2716 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2717 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2718 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2719 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2720 if (imsg_add(wbuf, path, path_len) == -1)
2721 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2723 wbuf->fd = -1;
2724 imsg_close(ibuf, wbuf);
2726 if (entries) {
2727 err = send_tree_entries(ibuf, entries, nentries);
2728 if (err)
2729 return err;
2732 return flush_imsg(ibuf);
2735 const struct got_error *
2736 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2738 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2739 0, 0, -1, NULL, 0) == -1)
2740 return got_error_from_errno("imsg_compose "
2741 "OBJECT_ENUMERATION_REQUEST");
2743 return flush_imsg(ibuf);
2746 const struct got_error *
2747 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2749 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2750 0, 0, -1, NULL, 0) == -1)
2751 return got_error_from_errno("imsg_compose "
2752 "OBJECT_ENUMERATION_DONE");
2754 return flush_imsg(ibuf);
2757 const struct got_error *
2758 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2760 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2761 0, 0, -1, NULL, 0) == -1)
2762 return got_error_from_errno("imsg_compose "
2763 "OBJECT_ENUMERATION_INCOMPLETE");
2765 return flush_imsg(ibuf);
2768 const struct got_error *
2769 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2770 struct got_object_id *id, time_t mtime)
2772 struct ibuf *wbuf;
2774 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2775 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2776 if (wbuf == NULL)
2777 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2779 /* Keep in sync with struct got_imsg_enumerated_commit! */
2780 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2781 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2782 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2783 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2785 wbuf->fd = -1;
2786 imsg_close(ibuf, wbuf);
2787 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2788 return NULL;
2791 const struct got_error *
2792 got_privsep_recv_enumerated_objects(int *found_all_objects,
2793 struct imsgbuf *ibuf,
2794 got_object_enumerate_commit_cb cb_commit,
2795 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2796 struct got_repository *repo)
2798 const struct got_error *err = NULL;
2799 struct imsg imsg;
2800 struct got_imsg_enumerated_commit *icommit = NULL;
2801 struct got_object_id commit_id;
2802 int have_commit = 0;
2803 time_t mtime = 0;
2804 struct got_tree_object tree;
2805 struct got_imsg_enumerated_tree *itree;
2806 struct got_object_id tree_id;
2807 char *path = NULL, *canon_path = NULL;
2808 size_t datalen, path_len;
2809 int nentries = -1;
2810 int done = 0;
2812 *found_all_objects = 0;
2813 memset(&tree, 0, sizeof(tree));
2815 while (!done) {
2816 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2817 if (err)
2818 break;
2820 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2821 switch (imsg.hdr.type) {
2822 case GOT_IMSG_ENUMERATED_COMMIT:
2823 if (have_commit && nentries != -1) {
2824 err = got_error(GOT_ERR_PRIVSEP_MSG);
2825 break;
2827 if (datalen != sizeof(*icommit)) {
2828 err = got_error(GOT_ERR_PRIVSEP_LEN);
2829 break;
2831 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2832 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2833 mtime = icommit->mtime;
2834 have_commit = 1;
2835 break;
2836 case GOT_IMSG_ENUMERATED_TREE:
2837 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2838 if (!have_commit) {
2839 err = got_error(GOT_ERR_PRIVSEP_MSG);
2840 break;
2842 if (datalen < sizeof(*itree)) {
2843 err = got_error(GOT_ERR_PRIVSEP_LEN);
2844 break;
2846 itree = imsg.data;
2847 path_len = datalen - sizeof(*itree);
2848 if (path_len == 0) {
2849 err = got_error(GOT_ERR_PRIVSEP_LEN);
2850 break;
2852 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2853 free(path);
2854 path = malloc(path_len + 1);
2855 if (path == NULL) {
2856 err = got_error_from_errno("malloc");
2857 break;
2859 free(canon_path);
2860 canon_path = malloc(path_len + 1);
2861 if (canon_path == NULL) {
2862 err = got_error_from_errno("malloc");
2863 break;
2865 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2866 path_len);
2867 path[path_len] = '\0';
2868 if (!got_path_is_absolute(path)) {
2869 err = got_error(GOT_ERR_BAD_PATH);
2870 break;
2872 if (got_path_is_root_dir(path)) {
2873 /* XXX check what got_canonpath() does wrong */
2874 canon_path[0] = '/';
2875 canon_path[1] = '\0';
2876 } else {
2877 err = got_canonpath(path, canon_path,
2878 path_len + 1);
2879 if (err)
2880 break;
2882 if (strcmp(path, canon_path) != 0) {
2883 err = got_error(GOT_ERR_BAD_PATH);
2884 break;
2886 if (nentries != -1) {
2887 err = got_error(GOT_ERR_PRIVSEP_MSG);
2888 break;
2890 if (itree->nentries < -1) {
2891 err = got_error(GOT_ERR_PRIVSEP_MSG);
2892 break;
2894 if (itree->nentries == -1) {
2895 /* Tree was not found in pack file. */
2896 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2897 path, repo);
2898 break;
2900 if (itree->nentries > INT_MAX) {
2901 err = got_error(GOT_ERR_PRIVSEP_LEN);
2902 break;
2904 tree.entries = calloc(itree->nentries,
2905 sizeof(struct got_tree_entry));
2906 if (tree.entries == NULL) {
2907 err = got_error_from_errno("calloc");
2908 break;
2910 if (itree->nentries == 0) {
2911 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2912 path, repo);
2913 if (err)
2914 break;
2916 /* Prepare for next tree. */
2917 free(tree.entries);
2918 memset(&tree, 0, sizeof(tree));
2919 nentries = -1;
2920 } else {
2921 tree.nentries = itree->nentries;
2922 nentries = 0;
2924 break;
2925 case GOT_IMSG_TREE_ENTRIES:
2926 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2927 if (nentries <= -1) {
2928 err = got_error(GOT_ERR_PRIVSEP_MSG);
2929 break;
2931 err = recv_tree_entries(imsg.data, datalen,
2932 &tree, &nentries);
2933 if (err)
2934 break;
2935 if (tree.nentries == nentries) {
2936 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2937 path, repo);
2938 if (err)
2939 break;
2941 /* Prepare for next tree. */
2942 free(tree.entries);
2943 memset(&tree, 0, sizeof(tree));
2944 nentries = -1;
2946 break;
2947 case GOT_IMSG_TREE_ENUMERATION_DONE:
2948 /* All trees have been found and traversed. */
2949 if (!have_commit || path == NULL || nentries != -1) {
2950 err = got_error(GOT_ERR_PRIVSEP_MSG);
2951 break;
2953 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2954 if (err)
2955 break;
2956 have_commit = 0;
2957 break;
2958 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2959 *found_all_objects = 1;
2960 done = 1;
2961 break;
2962 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
2963 done = 1;
2964 break;
2965 default:
2966 err = got_error(GOT_ERR_PRIVSEP_MSG);
2967 break;
2970 imsg_free(&imsg);
2971 if (err)
2972 break;
2975 free(path);
2976 free(canon_path);
2977 free(tree.entries);
2978 return err;
2981 const struct got_error *
2982 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2983 struct got_object_id *id)
2985 struct got_imsg_raw_delta_request dreq;
2987 memset(&dreq, 0, sizeof(dreq));
2988 dreq.idx = idx;
2989 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2991 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2992 &dreq, sizeof(dreq)) == -1)
2993 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2995 return flush_imsg(ibuf);
2998 const struct got_error *
2999 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
3001 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3004 const struct got_error *
3005 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3006 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3007 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3009 struct got_imsg_raw_delta idelta;
3010 int ret;
3012 memset(&idelta, 0, sizeof(idelta));
3013 idelta.base_size = base_size;
3014 idelta.result_size = result_size;
3015 idelta.delta_size = delta_size;
3016 idelta.delta_compressed_size = delta_compressed_size;
3017 idelta.delta_offset = delta_offset;
3018 idelta.delta_out_offset = delta_out_offset;
3019 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
3021 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3022 &idelta, sizeof(idelta));
3023 if (ret == -1)
3024 return got_error_from_errno("imsg_compose RAW_DELTA");
3026 return flush_imsg(ibuf);
3029 const struct got_error *
3030 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3031 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3032 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
3034 const struct got_error *err = NULL;
3035 struct imsg imsg;
3036 struct got_imsg_raw_delta *delta;
3037 size_t datalen;
3039 *base_size = 0;
3040 *result_size = 0;
3041 *delta_size = 0;
3042 *delta_compressed_size = 0;
3043 *delta_offset = 0;
3044 *delta_out_offset = 0;
3045 *base_id = NULL;
3047 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3048 if (err)
3049 return err;
3051 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3053 switch (imsg.hdr.type) {
3054 case GOT_IMSG_RAW_DELTA:
3055 if (datalen != sizeof(*delta)) {
3056 err = got_error(GOT_ERR_PRIVSEP_LEN);
3057 break;
3059 delta = imsg.data;
3060 *base_size = delta->base_size;
3061 *result_size = delta->result_size;
3062 *delta_size = delta->delta_size;
3063 *delta_compressed_size = delta->delta_compressed_size;
3064 *delta_offset = delta->delta_offset;
3065 *delta_out_offset = delta->delta_out_offset;
3066 *base_id = calloc(1, sizeof(**base_id));
3067 if (*base_id == NULL) {
3068 err = got_error_from_errno("malloc");
3069 break;
3071 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3072 break;
3073 default:
3074 err = got_error(GOT_ERR_PRIVSEP_MSG);
3075 break;
3078 imsg_free(&imsg);
3080 if (err) {
3081 free(*base_id);
3082 *base_id = NULL;
3084 return err;
3087 static const struct got_error *
3088 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3090 const struct got_error *err = NULL;
3091 struct got_imsg_object_idlist idlist;
3092 struct ibuf *wbuf;
3093 size_t i;
3095 memset(&idlist, 0, sizeof(idlist));
3097 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3098 return got_error(GOT_ERR_NO_SPACE);
3100 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3101 sizeof(idlist) + nids * sizeof(**ids));
3102 if (wbuf == NULL) {
3103 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3104 return err;
3107 idlist.nids = nids;
3108 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3109 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3111 for (i = 0; i < nids; i++) {
3112 struct got_object_id *id = ids[i];
3113 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3114 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3117 wbuf->fd = -1;
3118 imsg_close(ibuf, wbuf);
3120 return flush_imsg(ibuf);
3123 const struct got_error *
3124 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3125 struct got_object_id **ids, size_t nids)
3127 const struct got_error *err = NULL;
3128 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3129 int i, queued = 0;
3131 for (i = 0; i < nids; i++) {
3132 idlist[i % nitems(idlist)] = ids[i];
3133 queued++;
3134 if (queued >= nitems(idlist)) {
3135 err = send_idlist(ibuf, idlist, queued);
3136 if (err)
3137 return err;
3138 queued = 0;
3142 if (queued > 0) {
3143 err = send_idlist(ibuf, idlist, queued);
3144 if (err)
3145 return err;
3148 return NULL;
3151 const struct got_error *
3152 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3154 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3155 == -1)
3156 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3158 return flush_imsg(ibuf);
3161 const struct got_error *
3162 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3163 size_t *nids, struct imsgbuf *ibuf)
3165 const struct got_error *err = NULL;
3166 struct imsg imsg;
3167 struct got_imsg_object_idlist *idlist;
3168 size_t datalen;
3170 *ids = NULL;
3171 *done = 0;
3172 *nids = 0;
3174 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3175 if (err)
3176 return err;
3178 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3179 switch (imsg.hdr.type) {
3180 case GOT_IMSG_OBJ_ID_LIST:
3181 if (datalen < sizeof(*idlist)) {
3182 err = got_error(GOT_ERR_PRIVSEP_LEN);
3183 break;
3185 idlist = imsg.data;
3186 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3187 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3188 err = got_error(GOT_ERR_PRIVSEP_LEN);
3189 break;
3191 *nids = idlist->nids;
3192 *ids = calloc(*nids, sizeof(**ids));
3193 if (*ids == NULL) {
3194 err = got_error_from_errno("calloc");
3195 break;
3197 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3198 *nids * sizeof(**ids));
3199 break;
3200 case GOT_IMSG_OBJ_ID_LIST_DONE:
3201 *done = 1;
3202 break;
3203 default:
3204 err = got_error(GOT_ERR_PRIVSEP_MSG);
3205 break;
3208 imsg_free(&imsg);
3210 return err;
3213 const struct got_error *
3214 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3216 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3217 == -1)
3218 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3220 return flush_imsg(ibuf);
3223 const struct got_error *
3224 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3225 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3227 const struct got_error *err = NULL;
3228 struct ibuf *wbuf;
3229 struct got_imsg_reused_deltas ideltas;
3230 size_t i;
3232 memset(&ideltas, 0, sizeof(ideltas));
3234 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3235 return got_error(GOT_ERR_NO_SPACE);
3237 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3238 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3239 if (wbuf == NULL) {
3240 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3241 return err;
3244 ideltas.ndeltas = ndeltas;
3245 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3246 return got_error_from_errno("imsg_add REUSED_DELTAS");
3248 for (i = 0; i < ndeltas; i++) {
3249 struct got_imsg_reused_delta *delta = &deltas[i];
3250 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3251 return got_error_from_errno("imsg_add REUSED_DELTAS");
3254 wbuf->fd = -1;
3255 imsg_close(ibuf, wbuf);
3257 return flush_imsg(ibuf);
3260 const struct got_error *
3261 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3263 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3264 == -1)
3265 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3267 return flush_imsg(ibuf);
3270 const struct got_error *
3271 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3272 size_t *ndeltas, struct imsgbuf *ibuf)
3274 const struct got_error *err = NULL;
3275 struct imsg imsg;
3276 struct got_imsg_reused_deltas *ideltas;
3277 size_t datalen;
3279 *done = 0;
3280 *ndeltas = 0;
3282 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3283 if (err)
3284 return err;
3286 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3287 switch (imsg.hdr.type) {
3288 case GOT_IMSG_REUSED_DELTAS:
3289 if (datalen < sizeof(*ideltas)) {
3290 err = got_error(GOT_ERR_PRIVSEP_LEN);
3291 break;
3293 ideltas = imsg.data;
3294 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3295 ideltas->ndeltas * sizeof(*deltas) >
3296 datalen - sizeof(*ideltas)) {
3297 err = got_error(GOT_ERR_PRIVSEP_LEN);
3298 break;
3300 *ndeltas = ideltas->ndeltas;
3301 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3302 *ndeltas * sizeof(*deltas));
3303 break;
3304 case GOT_IMSG_DELTA_REUSE_DONE:
3305 *done = 1;
3306 break;
3307 default:
3308 err = got_error(GOT_ERR_PRIVSEP_MSG);
3309 break;
3312 imsg_free(&imsg);
3314 return err;
3317 const struct got_error *
3318 got_privsep_init_commit_painting(struct imsgbuf *ibuf)
3320 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_INIT,
3321 0, 0, -1, NULL, 0)
3322 == -1)
3323 return got_error_from_errno("imsg_compose "
3324 "COMMIT_PAINTING_INIT");
3326 return flush_imsg(ibuf);
3329 const struct got_error *
3330 got_privsep_send_painting_request(struct imsgbuf *ibuf, int idx,
3331 struct got_object_id *id, intptr_t color)
3333 struct got_imsg_commit_painting_request ireq;
3335 memset(&ireq, 0, sizeof(ireq));
3336 memcpy(ireq.id, id->sha1, sizeof(ireq.id));
3337 ireq.idx = idx;
3338 ireq.color = color;
3340 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_REQUEST, 0, 0, -1,
3341 &ireq, sizeof(ireq)) == -1)
3342 return got_error_from_errno("imsg_compose "
3343 "COMMIT_PAINTING_REQUEST");
3345 return flush_imsg(ibuf);
3348 static const struct got_error *
3349 send_painted_commits(struct got_object_id_queue *ids, int *nids,
3350 size_t remain, int present_in_pack, struct imsgbuf *ibuf)
3352 const struct got_error *err = NULL;
3353 struct ibuf *wbuf = NULL;
3354 struct got_object_qid *qid;
3355 size_t msglen;
3356 int ncommits;
3357 intptr_t color;
3359 msglen = MIN(remain, MAX_IMSGSIZE - IMSG_HEADER_SIZE);
3360 ncommits = (msglen - sizeof(struct got_imsg_painted_commits)) /
3361 sizeof(struct got_imsg_painted_commit);
3363 wbuf = imsg_create(ibuf, GOT_IMSG_PAINTED_COMMITS, 0, 0, msglen);
3364 if (wbuf == NULL) {
3365 err = got_error_from_errno("imsg_create PAINTED_COMMITS");
3366 return err;
3369 /* Keep in sync with struct got_imsg_painted_commits! */
3370 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
3371 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3372 if (imsg_add(wbuf, &present_in_pack, sizeof(present_in_pack)) == -1)
3373 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3375 while (ncommits > 0) {
3376 qid = STAILQ_FIRST(ids);
3377 STAILQ_REMOVE_HEAD(ids, entry);
3378 ncommits--;
3379 (*nids)--;
3380 color = (intptr_t)qid->data;
3382 /* Keep in sync with struct got_imsg_painted_commit! */
3383 if (imsg_add(wbuf, qid->id.sha1, SHA1_DIGEST_LENGTH) == -1)
3384 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3385 if (imsg_add(wbuf, &color, sizeof(color)) == -1)
3386 return got_error_from_errno("imsg_add PAINTED_COMMITS");
3388 got_object_qid_free(qid);
3391 wbuf->fd = -1;
3392 imsg_close(ibuf, wbuf);
3394 return flush_imsg(ibuf);
3397 const struct got_error *
3398 got_privsep_send_painted_commits(struct imsgbuf *ibuf,
3399 struct got_object_id_queue *ids, int *nids,
3400 int present_in_pack, int flush)
3402 const struct got_error *err;
3403 size_t remain;
3405 if (*nids <= 0)
3406 return NULL;
3408 do {
3409 remain = (sizeof(struct got_imsg_painted_commits)) +
3410 *nids * sizeof(struct got_imsg_painted_commit);
3411 if (flush || remain >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
3412 err = send_painted_commits(ids, nids, remain,
3413 present_in_pack, ibuf);
3414 if (err)
3415 return err;
3417 } while (flush && *nids > 0);
3419 return NULL;
3422 const struct got_error *
3423 got_privsep_send_painting_commits_done(struct imsgbuf *ibuf)
3425 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_PAINTING_DONE,
3426 0, 0, -1, NULL, 0)
3427 == -1)
3428 return got_error_from_errno("imsg_compose "
3429 "COMMIT_PAINTING_DONE");
3431 return flush_imsg(ibuf);
3434 const struct got_error *
3435 got_privsep_recv_painted_commits(struct got_object_id_queue *new_ids,
3436 got_privsep_recv_painted_commit_cb cb, void *cb_arg, struct imsgbuf *ibuf)
3438 const struct got_error *err = NULL;
3439 struct imsg imsg;
3440 struct got_imsg_painted_commits icommits;
3441 struct got_imsg_painted_commit icommit;
3442 size_t datalen;
3443 int i;
3445 for (;;) {
3446 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3447 if (err)
3448 return err;
3450 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3451 if (imsg.hdr.type == GOT_IMSG_COMMIT_PAINTING_DONE)
3452 break;
3453 if (imsg.hdr.type != GOT_IMSG_PAINTED_COMMITS)
3454 return got_error(GOT_ERR_PRIVSEP_MSG);
3456 if (datalen < sizeof(icommits))
3457 return got_error(GOT_ERR_PRIVSEP_LEN);
3458 memcpy(&icommits, imsg.data, sizeof(icommits));
3459 if (icommits.ncommits * sizeof(icommit) < icommits.ncommits ||
3460 datalen < sizeof(icommits) +
3461 icommits.ncommits * sizeof(icommit))
3462 return got_error(GOT_ERR_PRIVSEP_LEN);
3464 for (i = 0; i < icommits.ncommits; i++) {
3465 memcpy(&icommit,
3466 (uint8_t *)imsg.data + sizeof(icommits) + i * sizeof(icommit),
3467 sizeof(icommit));
3469 if (icommits.present_in_pack) {
3470 struct got_object_id id;
3471 memcpy(id.sha1, icommit.id, SHA1_DIGEST_LENGTH);
3472 err = cb(cb_arg, &id, icommit.color);
3473 if (err)
3474 break;
3475 } else {
3476 struct got_object_qid *qid;
3477 err = got_object_qid_alloc_partial(&qid);
3478 if (err)
3479 break;
3480 memcpy(qid->id.sha1, icommit.id,
3481 SHA1_DIGEST_LENGTH);
3482 qid->data = (void *)icommit.color;
3483 STAILQ_INSERT_TAIL(new_ids, qid, entry);
3487 imsg_free(&imsg);
3490 return err;
3493 const struct got_error *
3494 got_privsep_unveil_exec_helpers(void)
3496 const char *helpers[] = {
3497 GOT_PATH_PROG_READ_PACK,
3498 GOT_PATH_PROG_READ_OBJECT,
3499 GOT_PATH_PROG_READ_COMMIT,
3500 GOT_PATH_PROG_READ_TREE,
3501 GOT_PATH_PROG_READ_BLOB,
3502 GOT_PATH_PROG_READ_TAG,
3503 GOT_PATH_PROG_READ_GITCONFIG,
3504 GOT_PATH_PROG_READ_GOTCONFIG,
3505 GOT_PATH_PROG_READ_PATCH,
3506 GOT_PATH_PROG_FETCH_PACK,
3507 GOT_PATH_PROG_INDEX_PACK,
3508 GOT_PATH_PROG_SEND_PACK,
3510 size_t i;
3512 for (i = 0; i < nitems(helpers); i++) {
3513 if (unveil(helpers[i], "x") == 0)
3514 continue;
3515 return got_error_from_errno2("unveil", helpers[i]);
3518 return NULL;
3521 void
3522 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3524 if (close(imsg_fds[0]) == -1) {
3525 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3526 _exit(1);
3529 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3530 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3531 _exit(1);
3534 closefrom(GOT_IMSG_FD_CHILD + 1);
3536 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3537 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3538 strerror(errno));
3539 _exit(1);