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 <imsg.h>
33 #include <sha1.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <time.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 #ifndef MIN
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #endif
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static const struct got_error *
60 poll_fd(int fd, int events, int timeout)
61 {
62 struct pollfd pfd[1];
63 struct timespec ts;
64 sigset_t sigset;
65 int n;
67 pfd[0].fd = fd;
68 pfd[0].events = events;
70 ts.tv_sec = timeout;
71 ts.tv_nsec = 0;
73 if (sigemptyset(&sigset) == -1)
74 return got_error_from_errno("sigemptyset");
75 if (sigaddset(&sigset, SIGWINCH) == -1)
76 return got_error_from_errno("sigaddset");
78 n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
79 if (n == -1)
80 return got_error_from_errno("ppoll");
81 if (n == 0)
82 return got_error(GOT_ERR_TIMEOUT);
83 if (pfd[0].revents & (POLLERR | POLLNVAL))
84 return got_error_from_errno("poll error");
85 if (pfd[0].revents & (events | POLLHUP))
86 return NULL;
88 return got_error(GOT_ERR_INTERRUPT);
89 }
91 static const struct got_error *
92 read_imsg(struct imsgbuf *ibuf)
93 {
94 const struct got_error *err;
95 size_t n;
97 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
98 if (err)
99 return err;
101 n = imsg_read(ibuf);
102 if (n == -1) {
103 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
104 return got_error(GOT_ERR_PRIVSEP_NO_FD);
105 return got_error(GOT_ERR_PRIVSEP_READ);
107 if (n == 0)
108 return got_error(GOT_ERR_PRIVSEP_PIPE);
110 return NULL;
113 const struct got_error *
114 got_privsep_wait_for_child(pid_t pid)
116 int child_status;
118 if (waitpid(pid, &child_status, 0) == -1)
119 return got_error_from_errno("waitpid");
121 if (!WIFEXITED(child_status))
122 return got_error(GOT_ERR_PRIVSEP_DIED);
124 if (WEXITSTATUS(child_status) != 0)
125 return got_error(GOT_ERR_PRIVSEP_EXIT);
127 return NULL;
130 static const struct got_error *
131 recv_imsg_error(struct imsg *imsg, size_t datalen)
133 struct got_imsg_error *ierr;
135 if (datalen != sizeof(*ierr))
136 return got_error(GOT_ERR_PRIVSEP_LEN);
138 ierr = imsg->data;
139 if (ierr->code == GOT_ERR_ERRNO) {
140 static struct got_error serr;
141 serr.code = GOT_ERR_ERRNO;
142 serr.msg = strerror(ierr->errno_code);
143 return &serr;
146 return got_error(ierr->code);
149 const struct got_error *
150 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
151 size_t min_datalen)
153 const struct got_error *err;
154 ssize_t n;
156 n = imsg_get(ibuf, imsg);
157 if (n == -1)
158 return got_error_from_errno("imsg_get");
160 while (n == 0) {
161 err = read_imsg(ibuf);
162 if (err)
163 return err;
164 n = imsg_get(ibuf, imsg);
165 if (n == -1)
166 return got_error_from_errno("imsg_get");
169 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
170 return got_error(GOT_ERR_PRIVSEP_LEN);
172 if (imsg->hdr.type == GOT_IMSG_ERROR) {
173 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
174 return recv_imsg_error(imsg, datalen);
177 return NULL;
180 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
181 void
182 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
184 const struct got_error *poll_err;
185 struct got_imsg_error ierr;
186 int ret;
188 ierr.code = err->code;
189 if (err->code == GOT_ERR_ERRNO)
190 ierr.errno_code = errno;
191 else
192 ierr.errno_code = 0;
193 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
194 if (ret == -1) {
195 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
196 getprogname(), err->code, err->msg, strerror(errno));
197 return;
200 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
201 if (poll_err) {
202 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
203 getprogname(), err->code, err->msg, poll_err->msg);
204 return;
207 ret = imsg_flush(ibuf);
208 if (ret == -1) {
209 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
210 getprogname(), err->code, err->msg, strerror(errno));
211 imsg_clear(ibuf);
212 return;
216 static const struct got_error *
217 flush_imsg(struct imsgbuf *ibuf)
219 const struct got_error *err;
221 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
222 if (err)
223 return err;
225 if (imsg_flush(ibuf) == -1) {
226 imsg_clear(ibuf);
227 return got_error_from_errno("imsg_flush");
230 return NULL;
233 const struct got_error *
234 got_privsep_flush_imsg(struct imsgbuf *ibuf)
236 return flush_imsg(ibuf);
239 const struct got_error *
240 got_privsep_send_stop(int fd)
242 const struct got_error *err = NULL;
243 struct imsgbuf ibuf;
245 imsg_init(&ibuf, fd);
247 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
248 return got_error_from_errno("imsg_compose STOP");
250 err = flush_imsg(&ibuf);
251 return err;
254 const struct got_error *
255 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
256 struct got_object_id *id)
258 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
259 id, sizeof(*id)) == -1)
260 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
262 return flush_imsg(ibuf);
265 const struct got_error *
266 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
267 struct got_object_id *id)
269 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
270 id, sizeof(*id)) == -1)
271 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
273 return flush_imsg(ibuf);
276 const struct got_error *
277 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
279 const struct got_error *err = NULL;
281 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
282 == -1) {
283 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
284 close(outfd);
285 return err;
288 return flush_imsg(ibuf);
291 const struct got_error *
292 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
293 uint8_t *data)
295 const struct got_error *err = NULL;
296 struct got_imsg_raw_obj iobj;
297 size_t len = sizeof(iobj);
298 struct ibuf *wbuf;
300 iobj.hdrlen = hdrlen;
301 iobj.size = size;
303 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
304 len += (size_t)size + hdrlen;
306 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
307 if (wbuf == NULL) {
308 err = got_error_from_errno("imsg_create RAW_OBJECT");
309 return err;
312 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
313 return got_error_from_errno("imsg_add RAW_OBJECT");
315 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
316 if (imsg_add(wbuf, data, size + hdrlen) == -1)
317 return got_error_from_errno("imsg_add RAW_OBJECT");
320 wbuf->fd = -1;
321 imsg_close(ibuf, wbuf);
323 return flush_imsg(ibuf);
326 const struct got_error *
327 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
328 struct imsgbuf *ibuf)
330 const struct got_error *err = NULL;
331 struct imsg imsg;
332 struct got_imsg_raw_obj *iobj;
333 size_t datalen;
335 *outbuf = NULL;
337 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
338 if (err)
339 return err;
341 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
343 switch (imsg.hdr.type) {
344 case GOT_IMSG_RAW_OBJECT:
345 if (datalen < sizeof(*iobj)) {
346 err = got_error(GOT_ERR_PRIVSEP_LEN);
347 break;
349 iobj = imsg.data;
350 *size = iobj->size;
351 *hdrlen = iobj->hdrlen;
353 if (datalen == sizeof(*iobj)) {
354 /* Data has been written to file descriptor. */
355 break;
358 if (*size < 0 ||
359 *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
360 err = got_error(GOT_ERR_PRIVSEP_LEN);
361 break;
364 *outbuf = malloc(*size + *hdrlen);
365 if (*outbuf == NULL) {
366 err = got_error_from_errno("malloc");
367 break;
369 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
370 break;
371 default:
372 err = got_error(GOT_ERR_PRIVSEP_MSG);
373 break;
376 imsg_free(&imsg);
378 return err;
381 const struct got_error *
382 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
383 struct got_object_id *id, int pack_idx)
385 const struct got_error *err = NULL;
386 struct got_imsg_packed_object iobj;
387 void *data;
388 size_t len;
390 if (pack_idx != -1) { /* commit is packed */
391 iobj.idx = pack_idx;
392 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
393 data = &iobj;
394 len = sizeof(iobj);
395 } else {
396 data = id;
397 len = sizeof(*id);
400 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
401 == -1) {
402 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
403 close(fd);
404 return err;
407 return flush_imsg(ibuf);
410 const struct got_error *
411 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
412 struct got_object_id *id, int pack_idx)
414 struct ibuf *wbuf;
415 size_t len;
417 if (pack_idx != -1)
418 len = sizeof(struct got_imsg_packed_object);
419 else
420 len = sizeof(*id);
422 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
423 if (wbuf == NULL)
424 return got_error_from_errno("imsg_create TREE_REQUEST");
426 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
427 return got_error_from_errno("imsg_add TREE_REQUEST");
429 if (pack_idx != -1) { /* tree is packed */
430 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
431 return got_error_from_errno("imsg_add TREE_REQUEST");
434 wbuf->fd = fd;
435 imsg_close(ibuf, wbuf);
437 return flush_imsg(ibuf);
440 const struct got_error *
441 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
442 struct got_object_id *id, int pack_idx)
444 struct got_imsg_packed_object iobj;
445 void *data;
446 size_t len;
448 if (pack_idx != -1) { /* tag is packed */
449 iobj.idx = pack_idx;
450 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
451 data = &iobj;
452 len = sizeof(iobj);
453 } else {
454 data = id;
455 len = sizeof(*id);
458 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
459 == -1)
460 return got_error_from_errno("imsg_compose TAG_REQUEST");
462 return flush_imsg(ibuf);
465 const struct got_error *
466 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
467 struct got_object_id *id, int pack_idx)
469 const struct got_error *err = NULL;
470 struct got_imsg_packed_object iobj;
471 void *data;
472 size_t len;
474 if (pack_idx != -1) { /* blob is packed */
475 iobj.idx = pack_idx;
476 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
477 data = &iobj;
478 len = sizeof(iobj);
479 } else {
480 data = id;
481 len = sizeof(*id);
484 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
485 == -1) {
486 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
487 close(infd);
488 return err;
491 return flush_imsg(ibuf);
494 const struct got_error *
495 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
497 const struct got_error *err = NULL;
499 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
500 == -1) {
501 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
502 close(outfd);
503 return err;
506 return flush_imsg(ibuf);
509 static const struct got_error *
510 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
512 const struct got_error *err = NULL;
514 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
515 err = got_error_from_errno("imsg_compose TMPFD");
516 close(fd);
517 return err;
520 return flush_imsg(ibuf);
523 const struct got_error *
524 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
526 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
529 const struct got_error *
530 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
532 struct got_imsg_object iobj;
534 memset(&iobj, 0, sizeof(iobj));
536 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
537 iobj.type = obj->type;
538 iobj.flags = obj->flags;
539 iobj.hdrlen = obj->hdrlen;
540 iobj.size = obj->size;
541 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
542 iobj.pack_offset = obj->pack_offset;
543 iobj.pack_idx = obj->pack_idx;
546 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
547 == -1)
548 return got_error_from_errno("imsg_compose OBJECT");
550 return flush_imsg(ibuf);
553 const struct got_error *
554 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
555 struct got_pathlist_head *have_refs, int fetch_all_branches,
556 struct got_pathlist_head *wanted_branches,
557 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
559 const struct got_error *err = NULL;
560 struct ibuf *wbuf;
561 size_t len;
562 struct got_pathlist_entry *pe;
563 struct got_imsg_fetch_request fetchreq;
565 memset(&fetchreq, 0, sizeof(fetchreq));
566 fetchreq.fetch_all_branches = fetch_all_branches;
567 fetchreq.list_refs_only = list_refs_only;
568 fetchreq.verbosity = verbosity;
569 TAILQ_FOREACH(pe, have_refs, entry)
570 fetchreq.n_have_refs++;
571 TAILQ_FOREACH(pe, wanted_branches, entry)
572 fetchreq.n_wanted_branches++;
573 TAILQ_FOREACH(pe, wanted_refs, entry)
574 fetchreq.n_wanted_refs++;
575 len = sizeof(struct got_imsg_fetch_request);
576 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
577 close(fd);
578 return got_error(GOT_ERR_NO_SPACE);
581 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
582 &fetchreq, sizeof(fetchreq)) == -1)
583 return got_error_from_errno(
584 "imsg_compose FETCH_SERVER_PROGRESS");
586 err = flush_imsg(ibuf);
587 if (err) {
588 close(fd);
589 return err;
591 fd = -1;
593 TAILQ_FOREACH(pe, have_refs, entry) {
594 const char *name = pe->path;
595 size_t name_len = pe->path_len;
596 struct got_object_id *id = pe->data;
598 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
599 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
600 if (wbuf == NULL)
601 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
603 /* Keep in sync with struct got_imsg_fetch_have_ref! */
604 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
605 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
606 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
607 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
608 if (imsg_add(wbuf, name, name_len) == -1)
609 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
611 wbuf->fd = -1;
612 imsg_close(ibuf, wbuf);
613 err = flush_imsg(ibuf);
614 if (err)
615 return err;
618 TAILQ_FOREACH(pe, wanted_branches, entry) {
619 const char *name = pe->path;
620 size_t name_len = pe->path_len;
622 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
623 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
624 len);
625 if (wbuf == NULL)
626 return got_error_from_errno(
627 "imsg_create FETCH_WANTED_BRANCH");
629 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
630 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
631 return got_error_from_errno(
632 "imsg_add FETCH_WANTED_BRANCH");
633 if (imsg_add(wbuf, name, name_len) == -1)
634 return got_error_from_errno(
635 "imsg_add FETCH_WANTED_BRANCH");
637 wbuf->fd = -1;
638 imsg_close(ibuf, wbuf);
639 err = flush_imsg(ibuf);
640 if (err)
641 return err;
644 TAILQ_FOREACH(pe, wanted_refs, entry) {
645 const char *name = pe->path;
646 size_t name_len = pe->path_len;
648 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
649 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
650 len);
651 if (wbuf == NULL)
652 return got_error_from_errno(
653 "imsg_create FETCH_WANTED_REF");
655 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
656 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
657 return got_error_from_errno(
658 "imsg_add FETCH_WANTED_REF");
659 if (imsg_add(wbuf, name, name_len) == -1)
660 return got_error_from_errno(
661 "imsg_add FETCH_WANTED_REF");
663 wbuf->fd = -1;
664 imsg_close(ibuf, wbuf);
665 err = flush_imsg(ibuf);
666 if (err)
667 return err;
671 return NULL;
675 const struct got_error *
676 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
678 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
681 const struct got_error *
682 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
683 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
684 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
686 const struct got_error *err = NULL;
687 struct imsg imsg;
688 size_t datalen;
689 struct got_imsg_fetch_symrefs *isymrefs = NULL;
690 size_t n, remain;
691 off_t off;
692 int i;
694 *done = 0;
695 *id = NULL;
696 *refname = NULL;
697 *server_progress = NULL;
698 *packfile_size = 0;
699 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
701 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
702 if (err)
703 return err;
705 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
706 switch (imsg.hdr.type) {
707 case GOT_IMSG_ERROR:
708 err = recv_imsg_error(&imsg, datalen);
709 break;
710 case GOT_IMSG_FETCH_SYMREFS:
711 if (datalen < sizeof(*isymrefs)) {
712 err = got_error(GOT_ERR_PRIVSEP_LEN);
713 break;
715 if (isymrefs != NULL) {
716 err = got_error(GOT_ERR_PRIVSEP_MSG);
717 break;
719 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
720 off = sizeof(*isymrefs);
721 remain = datalen - off;
722 for (n = 0; n < isymrefs->nsymrefs; n++) {
723 struct got_imsg_fetch_symref *s;
724 char *name, *target;
725 if (remain < sizeof(struct got_imsg_fetch_symref)) {
726 err = got_error(GOT_ERR_PRIVSEP_LEN);
727 goto done;
729 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
730 off += sizeof(*s);
731 remain -= sizeof(*s);
732 if (remain < s->name_len) {
733 err = got_error(GOT_ERR_PRIVSEP_LEN);
734 goto done;
736 name = strndup(imsg.data + off, s->name_len);
737 if (name == NULL) {
738 err = got_error_from_errno("strndup");
739 goto done;
741 off += s->name_len;
742 remain -= s->name_len;
743 if (remain < s->target_len) {
744 err = got_error(GOT_ERR_PRIVSEP_LEN);
745 free(name);
746 goto done;
748 target = strndup(imsg.data + off, s->target_len);
749 if (target == NULL) {
750 err = got_error_from_errno("strndup");
751 free(name);
752 goto done;
754 off += s->target_len;
755 remain -= s->target_len;
756 err = got_pathlist_append(symrefs, name, target);
757 if (err) {
758 free(name);
759 free(target);
760 goto done;
763 break;
764 case GOT_IMSG_FETCH_REF:
765 if (datalen <= SHA1_DIGEST_LENGTH) {
766 err = got_error(GOT_ERR_PRIVSEP_MSG);
767 break;
769 *id = malloc(sizeof(**id));
770 if (*id == NULL) {
771 err = got_error_from_errno("malloc");
772 break;
774 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
775 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
776 datalen - SHA1_DIGEST_LENGTH);
777 if (*refname == NULL) {
778 err = got_error_from_errno("strndup");
779 break;
781 break;
782 case GOT_IMSG_FETCH_SERVER_PROGRESS:
783 if (datalen == 0) {
784 err = got_error(GOT_ERR_PRIVSEP_LEN);
785 break;
787 *server_progress = strndup(imsg.data, datalen);
788 if (*server_progress == NULL) {
789 err = got_error_from_errno("strndup");
790 break;
792 for (i = 0; i < datalen; i++) {
793 if (!isprint((unsigned char)(*server_progress)[i]) &&
794 !isspace((unsigned char)(*server_progress)[i])) {
795 err = got_error(GOT_ERR_PRIVSEP_MSG);
796 free(*server_progress);
797 *server_progress = NULL;
798 goto done;
801 break;
802 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
803 if (datalen < sizeof(*packfile_size)) {
804 err = got_error(GOT_ERR_PRIVSEP_MSG);
805 break;
807 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
808 break;
809 case GOT_IMSG_FETCH_DONE:
810 if (datalen != SHA1_DIGEST_LENGTH) {
811 err = got_error(GOT_ERR_PRIVSEP_MSG);
812 break;
814 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
815 *done = 1;
816 break;
817 default:
818 err = got_error(GOT_ERR_PRIVSEP_MSG);
819 break;
821 done:
822 if (err) {
823 free(*id);
824 *id = NULL;
825 free(*refname);
826 *refname = NULL;
828 imsg_free(&imsg);
829 return err;
832 static const struct got_error *
833 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
834 int delete, struct imsgbuf *ibuf)
836 size_t len;
837 struct ibuf *wbuf;
839 len = sizeof(struct got_imsg_send_ref) + name_len;
840 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
841 if (wbuf == NULL)
842 return got_error_from_errno("imsg_create SEND_REF");
844 /* Keep in sync with struct got_imsg_send_ref! */
845 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
846 return got_error_from_errno("imsg_add SEND_REF");
847 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
848 return got_error_from_errno("imsg_add SEND_REF");
849 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
850 return got_error_from_errno("imsg_add SEND_REF");
851 if (imsg_add(wbuf, name, name_len) == -1)
852 return got_error_from_errno("imsg_add SEND_REF");
854 wbuf->fd = -1;
855 imsg_close(ibuf, wbuf);
856 return flush_imsg(ibuf);
859 const struct got_error *
860 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
861 struct got_pathlist_head *have_refs,
862 struct got_pathlist_head *delete_refs,
863 int verbosity)
865 const struct got_error *err = NULL;
866 struct got_pathlist_entry *pe;
867 struct got_imsg_send_request sendreq;
868 struct got_object_id zero_id;
870 memset(&zero_id, 0, sizeof(zero_id));
871 memset(&sendreq, 0, sizeof(sendreq));
872 sendreq.verbosity = verbosity;
873 TAILQ_FOREACH(pe, have_refs, entry)
874 sendreq.nrefs++;
875 TAILQ_FOREACH(pe, delete_refs, entry)
876 sendreq.nrefs++;
877 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
878 &sendreq, sizeof(sendreq)) == -1) {
879 err = got_error_from_errno(
880 "imsg_compose FETCH_SERVER_PROGRESS");
881 goto done;
884 err = flush_imsg(ibuf);
885 if (err)
886 goto done;
887 fd = -1;
889 TAILQ_FOREACH(pe, have_refs, entry) {
890 const char *name = pe->path;
891 size_t name_len = pe->path_len;
892 struct got_object_id *id = pe->data;
893 err = send_send_ref(name, name_len, id, 0, ibuf);
894 if (err)
895 goto done;
898 TAILQ_FOREACH(pe, delete_refs, entry) {
899 const char *name = pe->path;
900 size_t name_len = pe->path_len;
901 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
902 if (err)
903 goto done;
905 done:
906 if (fd != -1 && close(fd) == -1 && err == NULL)
907 err = got_error_from_errno("close");
908 return err;
912 const struct got_error *
913 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
914 struct imsgbuf *ibuf)
916 const struct got_error *err = NULL;
917 struct imsg imsg;
918 size_t datalen;
919 int done = 0;
920 struct got_imsg_send_remote_ref iremote_ref;
921 struct got_object_id *id = NULL;
922 char *refname = NULL;
923 struct got_pathlist_entry *new;
925 while (!done) {
926 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
927 if (err)
928 return err;
929 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
930 switch (imsg.hdr.type) {
931 case GOT_IMSG_ERROR:
932 err = recv_imsg_error(&imsg, datalen);
933 goto done;
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_ERROR:
1015 err = recv_imsg_error(&imsg, datalen);
1016 break;
1017 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1018 if (datalen < sizeof(*bytes_sent)) {
1019 err = got_error(GOT_ERR_PRIVSEP_MSG);
1020 break;
1022 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1023 break;
1024 case GOT_IMSG_SEND_REF_STATUS:
1025 if (datalen < sizeof(iref_status)) {
1026 err = got_error(GOT_ERR_PRIVSEP_MSG);
1027 break;
1029 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1030 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1031 err = got_error(GOT_ERR_PRIVSEP_MSG);
1032 break;
1034 *success = iref_status.success;
1035 *refname = strndup(imsg.data + sizeof(iref_status),
1036 iref_status.name_len);
1037 break;
1038 case GOT_IMSG_SEND_DONE:
1039 if (datalen != 0) {
1040 err = got_error(GOT_ERR_PRIVSEP_MSG);
1041 break;
1043 *done = 1;
1044 break;
1045 default:
1046 err = got_error(GOT_ERR_PRIVSEP_MSG);
1047 break;
1050 imsg_free(&imsg);
1051 return err;
1054 const struct got_error *
1055 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1056 int fd)
1058 const struct got_error *err = NULL;
1060 /* Keep in sync with struct got_imsg_index_pack_request */
1061 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1062 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1063 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1064 close(fd);
1065 return err;
1067 return flush_imsg(ibuf);
1070 const struct got_error *
1071 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1073 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1076 const struct got_error *
1077 got_privsep_recv_index_progress(int *done, int *nobj_total,
1078 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1079 struct imsgbuf *ibuf)
1081 const struct got_error *err = NULL;
1082 struct imsg imsg;
1083 struct got_imsg_index_pack_progress *iprogress;
1084 size_t datalen;
1086 *done = 0;
1087 *nobj_total = 0;
1088 *nobj_indexed = 0;
1089 *nobj_resolved = 0;
1091 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1092 if (err)
1093 return err;
1095 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1096 switch (imsg.hdr.type) {
1097 case GOT_IMSG_ERROR:
1098 err = recv_imsg_error(&imsg, datalen);
1099 break;
1100 case GOT_IMSG_IDXPACK_PROGRESS:
1101 if (datalen < sizeof(*iprogress)) {
1102 err = got_error(GOT_ERR_PRIVSEP_LEN);
1103 break;
1105 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1106 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1107 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1108 err = got_error(GOT_ERR_RANGE);
1109 break;
1111 *nobj_total = iprogress->nobj_total;
1112 *nobj_indexed = iprogress->nobj_indexed;
1113 *nobj_loose = iprogress->nobj_loose;
1114 *nobj_resolved = iprogress->nobj_resolved;
1115 break;
1116 case GOT_IMSG_IDXPACK_DONE:
1117 if (datalen != 0) {
1118 err = got_error(GOT_ERR_PRIVSEP_LEN);
1119 break;
1121 *done = 1;
1122 break;
1123 default:
1124 err = got_error(GOT_ERR_PRIVSEP_MSG);
1125 break;
1128 imsg_free(&imsg);
1129 return err;
1132 const struct got_error *
1133 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1134 struct imsgbuf *ibuf)
1136 struct got_imsg_object *iobj;
1137 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1139 if (datalen != sizeof(*iobj))
1140 return got_error(GOT_ERR_PRIVSEP_LEN);
1141 iobj = imsg->data;
1143 if (iobj->pack_offset < 0)
1144 return got_error(GOT_ERR_PACK_OFFSET);
1146 *obj = calloc(1, sizeof(**obj));
1147 if (*obj == NULL)
1148 return got_error_from_errno("calloc");
1150 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1151 (*obj)->type = iobj->type;
1152 (*obj)->flags = iobj->flags;
1153 (*obj)->hdrlen = iobj->hdrlen;
1154 (*obj)->size = iobj->size;
1155 /* path_packfile is handled by caller */
1156 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1157 (*obj)->pack_offset = iobj->pack_offset;
1158 (*obj)->pack_idx = iobj->pack_idx;
1160 STAILQ_INIT(&(*obj)->deltas.entries);
1161 return NULL;
1164 const struct got_error *
1165 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1167 const struct got_error *err = NULL;
1168 struct imsg imsg;
1169 const size_t min_datalen =
1170 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1172 *obj = NULL;
1174 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1175 if (err)
1176 return err;
1178 switch (imsg.hdr.type) {
1179 case GOT_IMSG_OBJECT:
1180 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1181 break;
1182 default:
1183 err = got_error(GOT_ERR_PRIVSEP_MSG);
1184 break;
1187 imsg_free(&imsg);
1189 return err;
1192 static const struct got_error *
1193 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1194 size_t logmsg_len)
1196 const struct got_error *err = NULL;
1197 size_t offset, remain;
1199 offset = 0;
1200 remain = logmsg_len;
1201 while (remain > 0) {
1202 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1204 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1205 commit->logmsg + offset, n) == -1) {
1206 err = got_error_from_errno("imsg_compose "
1207 "COMMIT_LOGMSG");
1208 break;
1211 err = flush_imsg(ibuf);
1212 if (err)
1213 break;
1215 offset += n;
1216 remain -= n;
1219 return err;
1222 const struct got_error *
1223 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1225 const struct got_error *err = NULL;
1226 struct got_imsg_commit_object *icommit;
1227 uint8_t *buf;
1228 size_t len, total;
1229 struct got_object_qid *qid;
1230 size_t author_len = strlen(commit->author);
1231 size_t committer_len = strlen(commit->committer);
1232 size_t logmsg_len = strlen(commit->logmsg);
1234 total = sizeof(*icommit) + author_len + committer_len +
1235 commit->nparents * SHA1_DIGEST_LENGTH;
1237 buf = malloc(total);
1238 if (buf == NULL)
1239 return got_error_from_errno("malloc");
1241 icommit = (struct got_imsg_commit_object *)buf;
1242 memcpy(icommit->tree_id, commit->tree_id->sha1,
1243 sizeof(icommit->tree_id));
1244 icommit->author_len = author_len;
1245 icommit->author_time = commit->author_time;
1246 icommit->author_gmtoff = commit->author_gmtoff;
1247 icommit->committer_len = committer_len;
1248 icommit->committer_time = commit->committer_time;
1249 icommit->committer_gmtoff = commit->committer_gmtoff;
1250 icommit->logmsg_len = logmsg_len;
1251 icommit->nparents = commit->nparents;
1253 len = sizeof(*icommit);
1254 memcpy(buf + len, commit->author, author_len);
1255 len += author_len;
1256 memcpy(buf + len, commit->committer, committer_len);
1257 len += committer_len;
1258 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1259 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1260 len += SHA1_DIGEST_LENGTH;
1263 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1264 err = got_error_from_errno("imsg_compose COMMIT");
1265 goto done;
1268 if (logmsg_len == 0 ||
1269 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1270 err = flush_imsg(ibuf);
1271 if (err)
1272 goto done;
1274 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1275 done:
1276 free(buf);
1277 return err;
1280 static const struct got_error *
1281 get_commit_from_imsg(struct got_commit_object **commit,
1282 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1284 const struct got_error *err = NULL;
1285 struct got_imsg_commit_object *icommit;
1286 size_t len = 0;
1287 int i;
1289 if (datalen < sizeof(*icommit))
1290 return got_error(GOT_ERR_PRIVSEP_LEN);
1292 icommit = imsg->data;
1293 if (datalen != sizeof(*icommit) + icommit->author_len +
1294 icommit->committer_len +
1295 icommit->nparents * SHA1_DIGEST_LENGTH)
1296 return got_error(GOT_ERR_PRIVSEP_LEN);
1298 if (icommit->nparents < 0)
1299 return got_error(GOT_ERR_PRIVSEP_LEN);
1301 len += sizeof(*icommit);
1303 *commit = got_object_commit_alloc_partial();
1304 if (*commit == NULL)
1305 return got_error_from_errno(
1306 "got_object_commit_alloc_partial");
1308 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1309 SHA1_DIGEST_LENGTH);
1310 (*commit)->author_time = icommit->author_time;
1311 (*commit)->author_gmtoff = icommit->author_gmtoff;
1312 (*commit)->committer_time = icommit->committer_time;
1313 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1315 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1316 if ((*commit)->author == NULL) {
1317 err = got_error_from_errno("strndup");
1318 goto done;
1320 len += icommit->author_len;
1322 (*commit)->committer = strndup(imsg->data + len,
1323 icommit->committer_len);
1324 if ((*commit)->committer == NULL) {
1325 err = got_error_from_errno("strndup");
1326 goto done;
1328 len += icommit->committer_len;
1330 if (icommit->logmsg_len == 0) {
1331 (*commit)->logmsg = strdup("");
1332 if ((*commit)->logmsg == NULL) {
1333 err = got_error_from_errno("strdup");
1334 goto done;
1336 } else {
1337 size_t offset = 0, remain = icommit->logmsg_len;
1339 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1340 if ((*commit)->logmsg == NULL) {
1341 err = got_error_from_errno("malloc");
1342 goto done;
1344 while (remain > 0) {
1345 struct imsg imsg_log;
1346 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1347 remain);
1349 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1350 if (err)
1351 goto done;
1353 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1354 err = got_error(GOT_ERR_PRIVSEP_MSG);
1355 goto done;
1358 memcpy((*commit)->logmsg + offset,
1359 imsg_log.data, n);
1360 imsg_free(&imsg_log);
1361 offset += n;
1362 remain -= n;
1364 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1367 for (i = 0; i < icommit->nparents; i++) {
1368 struct got_object_qid *qid;
1370 err = got_object_qid_alloc_partial(&qid);
1371 if (err)
1372 break;
1373 memcpy(&qid->id, imsg->data + len +
1374 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1375 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1376 (*commit)->nparents++;
1378 done:
1379 if (err) {
1380 got_object_commit_close(*commit);
1381 *commit = NULL;
1383 return err;
1386 const struct got_error *
1387 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1389 const struct got_error *err = NULL;
1390 struct imsg imsg;
1391 size_t datalen;
1392 const size_t min_datalen =
1393 MIN(sizeof(struct got_imsg_error),
1394 sizeof(struct got_imsg_commit_object));
1396 *commit = NULL;
1398 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1399 if (err)
1400 return err;
1402 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1404 switch (imsg.hdr.type) {
1405 case GOT_IMSG_COMMIT:
1406 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1407 break;
1408 default:
1409 err = got_error(GOT_ERR_PRIVSEP_MSG);
1410 break;
1413 imsg_free(&imsg);
1415 return err;
1418 static const struct got_error *
1419 send_tree_entries_batch(struct imsgbuf *ibuf,
1420 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1422 struct ibuf *wbuf;
1423 struct got_imsg_tree_entries ientries;
1424 int i;
1426 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1427 if (wbuf == NULL)
1428 return got_error_from_errno("imsg_create TREE_ENTRY");
1430 ientries.nentries = idxN - idx0 + 1;
1431 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1432 return got_error_from_errno("imsg_add TREE_ENTRY");
1434 for (i = idx0; i <= idxN; i++) {
1435 struct got_parsed_tree_entry *pte = &entries[i];
1437 /* Keep in sync with struct got_imsg_tree_object definition! */
1438 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1439 return got_error_from_errno("imsg_add TREE_ENTRY");
1440 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1441 return got_error_from_errno("imsg_add TREE_ENTRY");
1442 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1443 return got_error_from_errno("imsg_add TREE_ENTRY");
1445 /* Remaining bytes are the entry's name. */
1446 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1447 return got_error_from_errno("imsg_add TREE_ENTRY");
1450 wbuf->fd = -1;
1451 imsg_close(ibuf, wbuf);
1452 return NULL;
1455 static const struct got_error *
1456 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1457 int nentries)
1459 const struct got_error *err = NULL;
1460 int i, j;
1461 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1463 i = 0;
1464 for (j = 0; j < nentries; j++) {
1465 struct got_parsed_tree_entry *pte = &entries[j];
1466 size_t len = sizeof(*pte) + pte->namelen;
1468 if (j > 0 &&
1469 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1470 err = send_tree_entries_batch(ibuf, entries,
1471 i, j - 1, entries_len);
1472 if (err)
1473 return err;
1474 i = j;
1475 entries_len = sizeof(struct got_imsg_tree_entries);
1478 entries_len += len;
1481 if (j > 0) {
1482 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1483 entries_len);
1484 if (err)
1485 return err;
1488 return NULL;
1491 const struct got_error *
1492 got_privsep_send_tree(struct imsgbuf *ibuf,
1493 struct got_parsed_tree_entry *entries, int nentries)
1495 const struct got_error *err = NULL;
1496 struct got_imsg_tree_object itree;
1498 itree.nentries = nentries;
1499 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1500 == -1)
1501 return got_error_from_errno("imsg_compose TREE");
1503 err = send_tree_entries(ibuf, entries, nentries);
1504 if (err)
1505 return err;
1507 return flush_imsg(ibuf);
1511 static const struct got_error *
1512 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1513 int *nentries)
1515 const struct got_error *err = NULL;
1516 struct got_imsg_tree_entries *ientries;
1517 struct got_tree_entry *te;
1518 size_t te_offset;
1519 size_t i;
1521 if (datalen <= sizeof(*ientries) ||
1522 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1523 return got_error(GOT_ERR_PRIVSEP_LEN);
1525 ientries = (struct got_imsg_tree_entries *)data;
1526 if (ientries->nentries > INT_MAX) {
1527 return got_error_msg(GOT_ERR_NO_SPACE,
1528 "too many tree entries");
1531 te_offset = sizeof(*ientries);
1532 for (i = 0; i < ientries->nentries; i++) {
1533 struct got_imsg_tree_entry ite;
1534 const char *te_name;
1535 uint8_t *buf = (uint8_t *)data + te_offset;
1537 if (te_offset >= datalen) {
1538 err = got_error(GOT_ERR_PRIVSEP_LEN);
1539 break;
1542 /* Might not be aligned, size is ~32 bytes. */
1543 memcpy(&ite, buf, sizeof(ite));
1545 if (ite.namelen >= sizeof(te->name)) {
1546 err = got_error(GOT_ERR_PRIVSEP_LEN);
1547 break;
1549 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1550 err = got_error(GOT_ERR_PRIVSEP_LEN);
1551 break;
1554 if (*nentries >= tree->nentries) {
1555 err = got_error(GOT_ERR_PRIVSEP_LEN);
1556 break;
1558 te = &tree->entries[*nentries];
1559 te_name = buf + sizeof(ite);
1560 memcpy(te->name, te_name, ite.namelen);
1561 te->name[ite.namelen] = '\0';
1562 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1563 te->mode = ite.mode;
1564 te->idx = *nentries;
1565 (*nentries)++;
1567 te_offset += sizeof(ite) + ite.namelen;
1570 return err;
1573 const struct got_error *
1574 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1576 const struct got_error *err = NULL;
1577 const size_t min_datalen =
1578 MIN(sizeof(struct got_imsg_error),
1579 sizeof(struct got_imsg_tree_object));
1580 struct got_imsg_tree_object *itree;
1581 int nentries = 0;
1583 *tree = NULL;
1585 err = read_imsg(ibuf);
1586 if (err)
1587 goto done;
1589 for (;;) {
1590 struct imsg imsg;
1591 size_t n;
1592 size_t datalen;
1594 n = imsg_get(ibuf, &imsg);
1595 if (n == 0) {
1596 if ((*tree)) {
1597 if (nentries < (*tree)->nentries) {
1598 err = read_imsg(ibuf);
1599 if (err)
1600 break;
1601 continue;
1602 } else
1603 break;
1604 } else {
1605 err = got_error(GOT_ERR_PRIVSEP_MSG);
1606 break;
1610 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1611 imsg_free(&imsg);
1612 err = got_error(GOT_ERR_PRIVSEP_LEN);
1613 break;
1616 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1618 switch (imsg.hdr.type) {
1619 case GOT_IMSG_ERROR:
1620 err = recv_imsg_error(&imsg, datalen);
1621 break;
1622 case GOT_IMSG_TREE:
1623 /* This message should only appear once. */
1624 if (*tree != NULL) {
1625 err = got_error(GOT_ERR_PRIVSEP_MSG);
1626 break;
1628 if (datalen != sizeof(*itree)) {
1629 err = got_error(GOT_ERR_PRIVSEP_LEN);
1630 break;
1632 itree = imsg.data;
1633 if (itree->nentries < 0) {
1634 err = got_error(GOT_ERR_PRIVSEP_LEN);
1635 break;
1637 *tree = malloc(sizeof(**tree));
1638 if (*tree == NULL) {
1639 err = got_error_from_errno("malloc");
1640 break;
1642 (*tree)->entries = calloc(itree->nentries,
1643 sizeof(struct got_tree_entry));
1644 if ((*tree)->entries == NULL) {
1645 err = got_error_from_errno("malloc");
1646 free(*tree);
1647 *tree = NULL;
1648 break;
1650 (*tree)->nentries = itree->nentries;
1651 (*tree)->refcnt = 0;
1652 break;
1653 case GOT_IMSG_TREE_ENTRIES:
1654 /* This message should be preceeded by GOT_IMSG_TREE. */
1655 if (*tree == NULL) {
1656 err = got_error(GOT_ERR_PRIVSEP_MSG);
1657 break;
1659 err = recv_tree_entries(imsg.data, datalen,
1660 *tree, &nentries);
1661 break;
1662 default:
1663 err = got_error(GOT_ERR_PRIVSEP_MSG);
1664 break;
1667 imsg_free(&imsg);
1668 if (err)
1669 break;
1671 done:
1672 if (*tree && (*tree)->nentries != nentries) {
1673 if (err == NULL)
1674 err = got_error(GOT_ERR_PRIVSEP_LEN);
1675 got_object_tree_close(*tree);
1676 *tree = NULL;
1679 return err;
1682 const struct got_error *
1683 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1684 const uint8_t *data)
1686 struct got_imsg_blob iblob;
1688 iblob.size = size;
1689 iblob.hdrlen = hdrlen;
1691 if (data) {
1692 uint8_t *buf;
1694 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1695 return got_error(GOT_ERR_NO_SPACE);
1697 buf = malloc(sizeof(iblob) + size);
1698 if (buf == NULL)
1699 return got_error_from_errno("malloc");
1701 memcpy(buf, &iblob, sizeof(iblob));
1702 memcpy(buf + sizeof(iblob), data, size);
1703 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1704 sizeof(iblob) + size) == -1) {
1705 free(buf);
1706 return got_error_from_errno("imsg_compose BLOB");
1708 free(buf);
1709 } else {
1710 /* Data has already been written to file descriptor. */
1711 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1712 sizeof(iblob)) == -1)
1713 return got_error_from_errno("imsg_compose BLOB");
1717 return flush_imsg(ibuf);
1720 const struct got_error *
1721 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1722 struct imsgbuf *ibuf)
1724 const struct got_error *err = NULL;
1725 struct imsg imsg;
1726 struct got_imsg_blob *iblob;
1727 size_t datalen;
1729 *outbuf = NULL;
1731 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1732 if (err)
1733 return err;
1735 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1737 switch (imsg.hdr.type) {
1738 case GOT_IMSG_BLOB:
1739 if (datalen < sizeof(*iblob)) {
1740 err = got_error(GOT_ERR_PRIVSEP_LEN);
1741 break;
1743 iblob = imsg.data;
1744 *size = iblob->size;
1745 *hdrlen = iblob->hdrlen;
1747 if (datalen == sizeof(*iblob)) {
1748 /* Data has been written to file descriptor. */
1749 break;
1752 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1753 *size > datalen + sizeof(*iblob)) {
1754 err = got_error(GOT_ERR_PRIVSEP_LEN);
1755 break;
1758 *outbuf = malloc(*size);
1759 if (*outbuf == NULL) {
1760 err = got_error_from_errno("malloc");
1761 break;
1763 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1764 break;
1765 default:
1766 err = got_error(GOT_ERR_PRIVSEP_MSG);
1767 break;
1770 imsg_free(&imsg);
1772 return err;
1775 static const struct got_error *
1776 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1778 const struct got_error *err = NULL;
1779 size_t offset, remain;
1781 offset = 0;
1782 remain = tagmsg_len;
1783 while (remain > 0) {
1784 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1786 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1787 tag->tagmsg + offset, n) == -1) {
1788 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1789 break;
1792 err = flush_imsg(ibuf);
1793 if (err)
1794 break;
1796 offset += n;
1797 remain -= n;
1800 return err;
1803 const struct got_error *
1804 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1806 const struct got_error *err = NULL;
1807 struct got_imsg_tag_object *itag;
1808 uint8_t *buf;
1809 size_t len, total;
1810 size_t tag_len = strlen(tag->tag);
1811 size_t tagger_len = strlen(tag->tagger);
1812 size_t tagmsg_len = strlen(tag->tagmsg);
1814 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1816 buf = malloc(total);
1817 if (buf == NULL)
1818 return got_error_from_errno("malloc");
1820 itag = (struct got_imsg_tag_object *)buf;
1821 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1822 itag->obj_type = tag->obj_type;
1823 itag->tag_len = tag_len;
1824 itag->tagger_len = tagger_len;
1825 itag->tagger_time = tag->tagger_time;
1826 itag->tagger_gmtoff = tag->tagger_gmtoff;
1827 itag->tagmsg_len = tagmsg_len;
1829 len = sizeof(*itag);
1830 memcpy(buf + len, tag->tag, tag_len);
1831 len += tag_len;
1832 memcpy(buf + len, tag->tagger, tagger_len);
1833 len += tagger_len;
1835 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1836 err = got_error_from_errno("imsg_compose TAG");
1837 goto done;
1840 if (tagmsg_len == 0 ||
1841 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1842 err = flush_imsg(ibuf);
1843 if (err)
1844 goto done;
1846 err = send_tagmsg(ibuf, tag, tagmsg_len);
1847 done:
1848 free(buf);
1849 return err;
1852 const struct got_error *
1853 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1855 const struct got_error *err = NULL;
1856 struct imsg imsg;
1857 struct got_imsg_tag_object *itag;
1858 size_t len, datalen;
1859 const size_t min_datalen =
1860 MIN(sizeof(struct got_imsg_error),
1861 sizeof(struct got_imsg_tag_object));
1863 *tag = NULL;
1865 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1866 if (err)
1867 return err;
1869 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1870 len = 0;
1872 switch (imsg.hdr.type) {
1873 case GOT_IMSG_TAG:
1874 if (datalen < sizeof(*itag)) {
1875 err = got_error(GOT_ERR_PRIVSEP_LEN);
1876 break;
1878 itag = imsg.data;
1879 if (datalen != sizeof(*itag) + itag->tag_len +
1880 itag->tagger_len) {
1881 err = got_error(GOT_ERR_PRIVSEP_LEN);
1882 break;
1884 len += sizeof(*itag);
1886 *tag = calloc(1, sizeof(**tag));
1887 if (*tag == NULL) {
1888 err = got_error_from_errno("calloc");
1889 break;
1892 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1894 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1895 if ((*tag)->tag == NULL) {
1896 err = got_error_from_errno("strndup");
1897 break;
1899 len += itag->tag_len;
1901 (*tag)->obj_type = itag->obj_type;
1902 (*tag)->tagger_time = itag->tagger_time;
1903 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1905 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1906 if ((*tag)->tagger == NULL) {
1907 err = got_error_from_errno("strndup");
1908 break;
1910 len += itag->tagger_len;
1912 if (itag->tagmsg_len == 0) {
1913 (*tag)->tagmsg = strdup("");
1914 if ((*tag)->tagmsg == NULL) {
1915 err = got_error_from_errno("strdup");
1916 break;
1918 } else {
1919 size_t offset = 0, remain = itag->tagmsg_len;
1921 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1922 if ((*tag)->tagmsg == NULL) {
1923 err = got_error_from_errno("malloc");
1924 break;
1926 while (remain > 0) {
1927 struct imsg imsg_log;
1928 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1929 remain);
1931 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1932 if (err)
1933 return err;
1935 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1936 return got_error(GOT_ERR_PRIVSEP_MSG);
1938 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1939 n);
1940 imsg_free(&imsg_log);
1941 offset += n;
1942 remain -= n;
1944 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1947 break;
1948 default:
1949 err = got_error(GOT_ERR_PRIVSEP_MSG);
1950 break;
1953 imsg_free(&imsg);
1955 return err;
1958 const struct got_error *
1959 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1960 struct got_packidx *packidx)
1962 const struct got_error *err = NULL;
1963 struct got_imsg_packidx ipackidx;
1964 struct got_imsg_pack ipack;
1965 int fd;
1967 ipackidx.len = packidx->len;
1968 ipackidx.packfile_size = pack->filesize;
1969 fd = dup(packidx->fd);
1970 if (fd == -1)
1971 return got_error_from_errno("dup");
1973 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1974 sizeof(ipackidx)) == -1) {
1975 err = got_error_from_errno("imsg_compose PACKIDX");
1976 close(fd);
1977 return err;
1980 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1981 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1982 return got_error(GOT_ERR_NO_SPACE);
1983 ipack.filesize = pack->filesize;
1985 fd = dup(pack->fd);
1986 if (fd == -1)
1987 return got_error_from_errno("dup");
1989 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1990 == -1) {
1991 err = got_error_from_errno("imsg_compose PACK");
1992 close(fd);
1993 return err;
1996 return flush_imsg(ibuf);
1999 const struct got_error *
2000 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2001 struct got_object_id *id)
2003 struct got_imsg_packed_object iobj;
2005 iobj.idx = idx;
2006 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2008 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2009 &iobj, sizeof(iobj)) == -1)
2010 return got_error_from_errno("imsg_compose "
2011 "PACKED_OBJECT_REQUEST");
2013 return flush_imsg(ibuf);
2016 const struct got_error *
2017 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2018 struct got_object_id *id)
2020 struct got_imsg_packed_object iobj;
2022 iobj.idx = idx;
2023 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2025 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2026 &iobj, sizeof(iobj)) == -1)
2027 return got_error_from_errno("imsg_compose "
2028 "PACKED_OBJECT_REQUEST");
2030 return flush_imsg(ibuf);
2033 const struct got_error *
2034 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2036 const struct got_error *err = NULL;
2038 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2039 NULL, 0) == -1) {
2040 err = got_error_from_errno("imsg_compose "
2041 "GITCONFIG_PARSE_REQUEST");
2042 close(fd);
2043 return err;
2046 return flush_imsg(ibuf);
2049 const struct got_error *
2050 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2052 if (imsg_compose(ibuf,
2053 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2054 NULL, 0) == -1)
2055 return got_error_from_errno("imsg_compose "
2056 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2058 return flush_imsg(ibuf);
2061 const struct got_error *
2062 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2064 if (imsg_compose(ibuf,
2065 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2066 NULL, 0) == -1)
2067 return got_error_from_errno("imsg_compose "
2068 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2070 return flush_imsg(ibuf);
2074 const struct got_error *
2075 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2077 if (imsg_compose(ibuf,
2078 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2079 return got_error_from_errno("imsg_compose "
2080 "GITCONFIG_AUTHOR_NAME_REQUEST");
2082 return flush_imsg(ibuf);
2085 const struct got_error *
2086 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2088 if (imsg_compose(ibuf,
2089 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2090 return got_error_from_errno("imsg_compose "
2091 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2093 return flush_imsg(ibuf);
2096 const struct got_error *
2097 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2099 if (imsg_compose(ibuf,
2100 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2101 return got_error_from_errno("imsg_compose "
2102 "GITCONFIG_REMOTE_REQUEST");
2104 return flush_imsg(ibuf);
2107 const struct got_error *
2108 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2110 if (imsg_compose(ibuf,
2111 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2112 return got_error_from_errno("imsg_compose "
2113 "GITCONFIG_OWNER_REQUEST");
2115 return flush_imsg(ibuf);
2118 const struct got_error *
2119 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2121 const struct got_error *err = NULL;
2122 struct imsg imsg;
2123 size_t datalen;
2125 *str = NULL;
2127 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2128 if (err)
2129 return err;
2130 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2132 switch (imsg.hdr.type) {
2133 case GOT_IMSG_GITCONFIG_STR_VAL:
2134 if (datalen == 0)
2135 break;
2136 /* datalen does not include terminating \0 */
2137 *str = malloc(datalen + 1);
2138 if (*str == NULL) {
2139 err = got_error_from_errno("malloc");
2140 break;
2142 memcpy(*str, imsg.data, datalen);
2143 (*str)[datalen] = '\0';
2144 break;
2145 default:
2146 err = got_error(GOT_ERR_PRIVSEP_MSG);
2147 break;
2150 imsg_free(&imsg);
2151 return err;
2154 const struct got_error *
2155 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2157 const struct got_error *err = NULL;
2158 struct imsg imsg;
2159 size_t datalen;
2160 const size_t min_datalen =
2161 MIN(sizeof(struct got_imsg_error), sizeof(int));
2163 *val = 0;
2165 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2166 if (err)
2167 return err;
2168 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2170 switch (imsg.hdr.type) {
2171 case GOT_IMSG_GITCONFIG_INT_VAL:
2172 if (datalen != sizeof(*val)) {
2173 err = got_error(GOT_ERR_PRIVSEP_LEN);
2174 break;
2176 memcpy(val, imsg.data, sizeof(*val));
2177 break;
2178 default:
2179 err = got_error(GOT_ERR_PRIVSEP_MSG);
2180 break;
2183 imsg_free(&imsg);
2184 return err;
2187 static void
2188 free_remote_data(struct got_remote_repo *remote)
2190 int i;
2192 free(remote->name);
2193 free(remote->fetch_url);
2194 free(remote->send_url);
2195 for (i = 0; i < remote->nfetch_branches; i++)
2196 free(remote->fetch_branches[i]);
2197 free(remote->fetch_branches);
2198 for (i = 0; i < remote->nsend_branches; i++)
2199 free(remote->send_branches[i]);
2200 free(remote->send_branches);
2201 for (i = 0; i < remote->nfetch_refs; i++)
2202 free(remote->fetch_refs[i]);
2203 free(remote->fetch_refs);
2206 const struct got_error *
2207 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2208 int *nremotes, struct imsgbuf *ibuf)
2210 const struct got_error *err = NULL;
2211 struct imsg imsg;
2212 size_t datalen;
2213 struct got_imsg_remotes iremotes;
2214 struct got_imsg_remote iremote;
2216 *remotes = NULL;
2217 *nremotes = 0;
2218 iremotes.nremotes = 0;
2220 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2221 if (err)
2222 return err;
2223 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2225 switch (imsg.hdr.type) {
2226 case GOT_IMSG_GITCONFIG_REMOTES:
2227 if (datalen != sizeof(iremotes)) {
2228 err = got_error(GOT_ERR_PRIVSEP_LEN);
2229 break;
2231 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2232 if (iremotes.nremotes == 0) {
2233 imsg_free(&imsg);
2234 return NULL;
2236 break;
2237 default:
2238 imsg_free(&imsg);
2239 return got_error(GOT_ERR_PRIVSEP_MSG);
2242 imsg_free(&imsg);
2244 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2245 if (*remotes == NULL)
2246 return got_error_from_errno("recallocarray");
2248 while (*nremotes < iremotes.nremotes) {
2249 struct got_remote_repo *remote;
2251 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2252 if (err)
2253 break;
2254 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2256 switch (imsg.hdr.type) {
2257 case GOT_IMSG_GITCONFIG_REMOTE:
2258 remote = &(*remotes)[*nremotes];
2259 memset(remote, 0, sizeof(*remote));
2260 if (datalen < sizeof(iremote)) {
2261 err = got_error(GOT_ERR_PRIVSEP_LEN);
2262 break;
2264 memcpy(&iremote, imsg.data, sizeof(iremote));
2265 if (iremote.name_len == 0 ||
2266 iremote.fetch_url_len == 0 ||
2267 iremote.send_url_len == 0 ||
2268 (sizeof(iremote) + iremote.name_len +
2269 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2270 err = got_error(GOT_ERR_PRIVSEP_LEN);
2271 break;
2273 remote->name = strndup(imsg.data + sizeof(iremote),
2274 iremote.name_len);
2275 if (remote->name == NULL) {
2276 err = got_error_from_errno("strndup");
2277 break;
2279 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2280 iremote.name_len, iremote.fetch_url_len);
2281 if (remote->fetch_url == NULL) {
2282 err = got_error_from_errno("strndup");
2283 free_remote_data(remote);
2284 break;
2286 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2287 iremote.name_len + iremote.fetch_url_len,
2288 iremote.send_url_len);
2289 if (remote->send_url == NULL) {
2290 err = got_error_from_errno("strndup");
2291 free_remote_data(remote);
2292 break;
2294 remote->mirror_references = iremote.mirror_references;
2295 remote->fetch_all_branches = iremote.fetch_all_branches;
2296 remote->nfetch_branches = 0;
2297 remote->fetch_branches = NULL;
2298 remote->nsend_branches = 0;
2299 remote->send_branches = NULL;
2300 remote->nfetch_refs = 0;
2301 remote->fetch_refs = NULL;
2302 (*nremotes)++;
2303 break;
2304 default:
2305 err = got_error(GOT_ERR_PRIVSEP_MSG);
2306 break;
2309 imsg_free(&imsg);
2310 if (err)
2311 break;
2314 if (err) {
2315 int i;
2316 for (i = 0; i < *nremotes; i++)
2317 free_remote_data(&(*remotes)[i]);
2318 free(*remotes);
2319 *remotes = NULL;
2320 *nremotes = 0;
2322 return err;
2325 const struct got_error *
2326 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2328 const struct got_error *err = NULL;
2330 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2331 NULL, 0) == -1) {
2332 err = got_error_from_errno("imsg_compose "
2333 "GOTCONFIG_PARSE_REQUEST");
2334 close(fd);
2335 return err;
2338 return flush_imsg(ibuf);
2341 const struct got_error *
2342 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2344 if (imsg_compose(ibuf,
2345 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2346 return got_error_from_errno("imsg_compose "
2347 "GOTCONFIG_AUTHOR_REQUEST");
2349 return flush_imsg(ibuf);
2352 const struct got_error *
2353 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2355 if (imsg_compose(ibuf,
2356 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2357 return got_error_from_errno("imsg_compose "
2358 "GOTCONFIG_REMOTE_REQUEST");
2360 return flush_imsg(ibuf);
2363 const struct got_error *
2364 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2366 const struct got_error *err = NULL;
2367 struct imsg imsg;
2368 size_t datalen;
2370 *str = NULL;
2372 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2373 if (err)
2374 return err;
2375 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2377 switch (imsg.hdr.type) {
2378 case GOT_IMSG_ERROR:
2379 err = recv_imsg_error(&imsg, datalen);
2380 break;
2381 case GOT_IMSG_GOTCONFIG_STR_VAL:
2382 if (datalen == 0)
2383 break;
2384 /* datalen does not include terminating \0 */
2385 *str = malloc(datalen + 1);
2386 if (*str == NULL) {
2387 err = got_error_from_errno("malloc");
2388 break;
2390 memcpy(*str, imsg.data, datalen);
2391 (*str)[datalen] = '\0';
2392 break;
2393 default:
2394 err = got_error(GOT_ERR_PRIVSEP_MSG);
2395 break;
2398 imsg_free(&imsg);
2399 return err;
2402 const struct got_error *
2403 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2404 int *nremotes, struct imsgbuf *ibuf)
2406 const struct got_error *err = NULL;
2407 struct imsg imsg;
2408 size_t datalen;
2409 struct got_imsg_remotes iremotes;
2410 struct got_imsg_remote iremote;
2411 const size_t min_datalen =
2412 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2414 *remotes = NULL;
2415 *nremotes = 0;
2416 iremotes.nremotes = 0;
2418 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2419 if (err)
2420 return err;
2421 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2423 switch (imsg.hdr.type) {
2424 case GOT_IMSG_ERROR:
2425 err = recv_imsg_error(&imsg, datalen);
2426 break;
2427 case GOT_IMSG_GOTCONFIG_REMOTES:
2428 if (datalen != sizeof(iremotes)) {
2429 err = got_error(GOT_ERR_PRIVSEP_LEN);
2430 break;
2432 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2433 if (iremotes.nremotes < 0) {
2434 err = got_error(GOT_ERR_PRIVSEP_LEN);
2435 break;
2437 if (iremotes.nremotes == 0) {
2438 imsg_free(&imsg);
2439 return NULL;
2441 break;
2442 default:
2443 imsg_free(&imsg);
2444 return got_error(GOT_ERR_PRIVSEP_MSG);
2447 imsg_free(&imsg);
2449 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2450 if (*remotes == NULL)
2451 return got_error_from_errno("recallocarray");
2453 while (*nremotes < iremotes.nremotes) {
2454 struct got_remote_repo *remote;
2455 const size_t min_datalen =
2456 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2457 int i;
2459 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2460 if (err)
2461 break;
2462 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2464 switch (imsg.hdr.type) {
2465 case GOT_IMSG_ERROR:
2466 err = recv_imsg_error(&imsg, datalen);
2467 break;
2468 case GOT_IMSG_GOTCONFIG_REMOTE:
2469 remote = &(*remotes)[*nremotes];
2470 memset(remote, 0, sizeof(*remote));
2471 if (datalen < sizeof(iremote)) {
2472 err = got_error(GOT_ERR_PRIVSEP_LEN);
2473 break;
2475 memcpy(&iremote, imsg.data, sizeof(iremote));
2476 if (iremote.name_len == 0 ||
2477 (iremote.fetch_url_len == 0 &&
2478 iremote.send_url_len == 0) ||
2479 (sizeof(iremote) + iremote.name_len +
2480 iremote.fetch_url_len + iremote.send_url_len) >
2481 datalen) {
2482 err = got_error(GOT_ERR_PRIVSEP_LEN);
2483 break;
2485 remote->name = strndup(imsg.data + sizeof(iremote),
2486 iremote.name_len);
2487 if (remote->name == NULL) {
2488 err = got_error_from_errno("strndup");
2489 break;
2491 remote->fetch_url = strndup(imsg.data +
2492 sizeof(iremote) + iremote.name_len,
2493 iremote.fetch_url_len);
2494 if (remote->fetch_url == NULL) {
2495 err = got_error_from_errno("strndup");
2496 free_remote_data(remote);
2497 break;
2499 remote->send_url = strndup(imsg.data +
2500 sizeof(iremote) + iremote.name_len +
2501 iremote.fetch_url_len, iremote.send_url_len);
2502 if (remote->send_url == NULL) {
2503 err = got_error_from_errno("strndup");
2504 free_remote_data(remote);
2505 break;
2507 remote->mirror_references = iremote.mirror_references;
2508 remote->fetch_all_branches = iremote.fetch_all_branches;
2509 if (iremote.nfetch_branches > 0) {
2510 remote->fetch_branches = recallocarray(NULL, 0,
2511 iremote.nfetch_branches, sizeof(char *));
2512 if (remote->fetch_branches == NULL) {
2513 err = got_error_from_errno("calloc");
2514 free_remote_data(remote);
2515 break;
2518 remote->nfetch_branches = 0;
2519 for (i = 0; i < iremote.nfetch_branches; i++) {
2520 char *branch;
2521 err = got_privsep_recv_gotconfig_str(&branch,
2522 ibuf);
2523 if (err) {
2524 free_remote_data(remote);
2525 goto done;
2527 remote->fetch_branches[i] = branch;
2528 remote->nfetch_branches++;
2530 if (iremote.nsend_branches > 0) {
2531 remote->send_branches = recallocarray(NULL, 0,
2532 iremote.nsend_branches, sizeof(char *));
2533 if (remote->send_branches == NULL) {
2534 err = got_error_from_errno("calloc");
2535 free_remote_data(remote);
2536 break;
2539 remote->nsend_branches = 0;
2540 for (i = 0; i < iremote.nsend_branches; i++) {
2541 char *branch;
2542 err = got_privsep_recv_gotconfig_str(&branch,
2543 ibuf);
2544 if (err) {
2545 free_remote_data(remote);
2546 goto done;
2548 remote->send_branches[i] = branch;
2549 remote->nsend_branches++;
2551 if (iremote.nfetch_refs > 0) {
2552 remote->fetch_refs = recallocarray(NULL, 0,
2553 iremote.nfetch_refs, sizeof(char *));
2554 if (remote->fetch_refs == NULL) {
2555 err = got_error_from_errno("calloc");
2556 free_remote_data(remote);
2557 break;
2560 remote->nfetch_refs = 0;
2561 for (i = 0; i < iremote.nfetch_refs; i++) {
2562 char *ref;
2563 err = got_privsep_recv_gotconfig_str(&ref,
2564 ibuf);
2565 if (err) {
2566 free_remote_data(remote);
2567 goto done;
2569 remote->fetch_refs[i] = ref;
2570 remote->nfetch_refs++;
2572 (*nremotes)++;
2573 break;
2574 default:
2575 err = got_error(GOT_ERR_PRIVSEP_MSG);
2576 break;
2579 imsg_free(&imsg);
2580 if (err)
2581 break;
2583 done:
2584 if (err) {
2585 int i;
2586 for (i = 0; i < *nremotes; i++)
2587 free_remote_data(&(*remotes)[i]);
2588 free(*remotes);
2589 *remotes = NULL;
2590 *nremotes = 0;
2592 return err;
2595 const struct got_error *
2596 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2597 struct got_object_id *id, int idx, const char *path)
2599 struct ibuf *wbuf;
2600 size_t path_len = strlen(path) + 1;
2602 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2603 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2604 if (wbuf == NULL)
2605 return got_error_from_errno(
2606 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2607 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2608 return got_error_from_errno("imsg_add "
2609 "COMMIT_TRAVERSAL_REQUEST");
2610 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2611 return got_error_from_errno("imsg_add "
2612 "COMMIT_TRAVERSAL_REQUEST");
2613 if (imsg_add(wbuf, path, path_len) == -1)
2614 return got_error_from_errno("imsg_add "
2615 "COMMIT_TRAVERSAL_REQUEST");
2617 wbuf->fd = -1;
2618 imsg_close(ibuf, wbuf);
2620 return flush_imsg(ibuf);
2623 const struct got_error *
2624 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2625 struct got_object_id **changed_commit_id,
2626 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2628 const struct got_error *err = NULL;
2629 struct imsg imsg;
2630 struct got_imsg_traversed_commits *icommits;
2631 size_t datalen;
2632 int i, done = 0;
2634 *changed_commit = NULL;
2635 *changed_commit_id = NULL;
2637 while (!done) {
2638 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2639 if (err)
2640 return err;
2642 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2643 switch (imsg.hdr.type) {
2644 case GOT_IMSG_TRAVERSED_COMMITS:
2645 icommits = imsg.data;
2646 if (datalen != sizeof(*icommits) +
2647 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2648 err = got_error(GOT_ERR_PRIVSEP_LEN);
2649 break;
2651 for (i = 0; i < icommits->ncommits; i++) {
2652 struct got_object_qid *qid;
2653 uint8_t *sha1 = (uint8_t *)imsg.data +
2654 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2655 err = got_object_qid_alloc_partial(&qid);
2656 if (err)
2657 break;
2658 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2659 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2661 /* The last commit may contain a change. */
2662 if (i == icommits->ncommits - 1) {
2663 *changed_commit_id =
2664 got_object_id_dup(&qid->id);
2665 if (*changed_commit_id == NULL) {
2666 err = got_error_from_errno(
2667 "got_object_id_dup");
2668 break;
2672 break;
2673 case GOT_IMSG_COMMIT:
2674 if (*changed_commit_id == NULL) {
2675 err = got_error(GOT_ERR_PRIVSEP_MSG);
2676 break;
2678 err = get_commit_from_imsg(changed_commit, &imsg,
2679 datalen, ibuf);
2680 break;
2681 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2682 done = 1;
2683 break;
2684 default:
2685 err = got_error(GOT_ERR_PRIVSEP_MSG);
2686 break;
2689 imsg_free(&imsg);
2690 if (err)
2691 break;
2694 if (err)
2695 got_object_id_queue_free(commit_ids);
2696 return err;
2699 const struct got_error *
2700 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2701 struct got_object_id *tree_id, const char *path,
2702 struct got_parsed_tree_entry *entries, int nentries)
2704 const struct got_error *err = NULL;
2705 struct ibuf *wbuf;
2706 size_t path_len = strlen(path);
2707 size_t msglen;
2709 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2710 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2711 if (wbuf == NULL)
2712 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2714 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2715 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2716 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2717 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2718 if (imsg_add(wbuf, path, path_len) == -1)
2719 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2721 wbuf->fd = -1;
2722 imsg_close(ibuf, wbuf);
2724 if (entries) {
2725 err = send_tree_entries(ibuf, entries, nentries);
2726 if (err)
2727 return err;
2730 return flush_imsg(ibuf);
2733 const struct got_error *
2734 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2736 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2737 0, 0, -1, NULL, 0) == -1)
2738 return got_error_from_errno("imsg_compose "
2739 "OBJECT_ENUMERATION_REQUEST");
2741 return flush_imsg(ibuf);
2744 const struct got_error *
2745 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2747 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2748 0, 0, -1, NULL, 0) == -1)
2749 return got_error_from_errno("imsg_compose "
2750 "OBJECT_ENUMERATION_DONE");
2752 return flush_imsg(ibuf);
2755 const struct got_error *
2756 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2758 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2759 0, 0, -1, NULL, 0) == -1)
2760 return got_error_from_errno("imsg_compose "
2761 "OBJECT_ENUMERATION_INCOMPLETE");
2763 return flush_imsg(ibuf);
2766 const struct got_error *
2767 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2768 struct got_object_id *id, time_t mtime)
2770 struct ibuf *wbuf;
2772 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2773 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2774 if (wbuf == NULL)
2775 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2777 /* Keep in sync with struct got_imsg_enumerated_commit! */
2778 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2779 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2780 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2781 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2783 wbuf->fd = -1;
2784 imsg_close(ibuf, wbuf);
2785 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2786 return NULL;
2789 const struct got_error *
2790 got_privsep_recv_enumerated_objects(int *found_all_objects,
2791 struct imsgbuf *ibuf,
2792 got_object_enumerate_commit_cb cb_commit,
2793 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2794 struct got_repository *repo)
2796 const struct got_error *err = NULL;
2797 struct imsg imsg;
2798 struct got_imsg_enumerated_commit *icommit = NULL;
2799 struct got_object_id commit_id;
2800 int have_commit = 0;
2801 time_t mtime = 0;
2802 struct got_tree_object tree;
2803 struct got_imsg_enumerated_tree *itree;
2804 struct got_object_id tree_id;
2805 char *path = NULL, *canon_path = NULL;
2806 size_t datalen, path_len;
2807 int nentries = -1;
2808 int done = 0;
2810 *found_all_objects = 0;
2811 memset(&tree, 0, sizeof(tree));
2813 while (!done) {
2814 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2815 if (err)
2816 break;
2818 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2819 switch (imsg.hdr.type) {
2820 case GOT_IMSG_ENUMERATED_COMMIT:
2821 if (have_commit && nentries != -1) {
2822 err = got_error(GOT_ERR_PRIVSEP_MSG);
2823 break;
2825 if (datalen != sizeof(*icommit)) {
2826 err = got_error(GOT_ERR_PRIVSEP_LEN);
2827 break;
2829 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2830 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2831 mtime = icommit->mtime;
2832 have_commit = 1;
2833 break;
2834 case GOT_IMSG_ENUMERATED_TREE:
2835 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2836 if (!have_commit) {
2837 err = got_error(GOT_ERR_PRIVSEP_MSG);
2838 break;
2840 if (datalen < sizeof(*itree)) {
2841 err = got_error(GOT_ERR_PRIVSEP_LEN);
2842 break;
2844 itree = imsg.data;
2845 path_len = datalen - sizeof(*itree);
2846 if (path_len == 0) {
2847 err = got_error(GOT_ERR_PRIVSEP_LEN);
2848 break;
2850 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2851 free(path);
2852 path = malloc(path_len + 1);
2853 if (path == NULL) {
2854 err = got_error_from_errno("malloc");
2855 break;
2857 free(canon_path);
2858 canon_path = malloc(path_len + 1);
2859 if (canon_path == NULL) {
2860 err = got_error_from_errno("malloc");
2861 break;
2863 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2864 path_len);
2865 path[path_len] = '\0';
2866 if (!got_path_is_absolute(path)) {
2867 err = got_error(GOT_ERR_BAD_PATH);
2868 break;
2870 if (got_path_is_root_dir(path)) {
2871 /* XXX check what got_canonpath() does wrong */
2872 canon_path[0] = '/';
2873 canon_path[1] = '\0';
2874 } else {
2875 err = got_canonpath(path, canon_path,
2876 path_len + 1);
2877 if (err)
2878 break;
2880 if (strcmp(path, canon_path) != 0) {
2881 err = got_error(GOT_ERR_BAD_PATH);
2882 break;
2884 if (nentries != -1) {
2885 err = got_error(GOT_ERR_PRIVSEP_MSG);
2886 break;
2888 if (itree->nentries < -1) {
2889 err = got_error(GOT_ERR_PRIVSEP_MSG);
2890 break;
2892 if (itree->nentries == -1) {
2893 /* Tree was not found in pack file. */
2894 done = 1;
2895 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2896 path, repo);
2897 break;
2899 if (itree->nentries > INT_MAX) {
2900 err = got_error(GOT_ERR_PRIVSEP_LEN);
2901 break;
2903 tree.entries = calloc(itree->nentries,
2904 sizeof(struct got_tree_entry));
2905 if (tree.entries == NULL) {
2906 err = got_error_from_errno("calloc");
2907 break;
2909 if (itree->nentries == 0) {
2910 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2911 path, repo);
2912 if (err)
2913 break;
2915 /* Prepare for next tree. */
2916 free(tree.entries);
2917 memset(&tree, 0, sizeof(tree));
2918 nentries = -1;
2919 } else {
2920 tree.nentries = itree->nentries;
2921 nentries = 0;
2923 break;
2924 case GOT_IMSG_TREE_ENTRIES:
2925 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2926 if (nentries <= -1) {
2927 err = got_error(GOT_ERR_PRIVSEP_MSG);
2928 break;
2930 err = recv_tree_entries(imsg.data, datalen,
2931 &tree, &nentries);
2932 if (err)
2933 break;
2934 if (tree.nentries == nentries) {
2935 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2936 path, repo);
2937 if (err)
2938 break;
2940 /* Prepare for next tree. */
2941 free(tree.entries);
2942 memset(&tree, 0, sizeof(tree));
2943 nentries = -1;
2945 break;
2946 case GOT_IMSG_TREE_ENUMERATION_DONE:
2947 /* All trees have been found and traversed. */
2948 if (!have_commit || path == NULL || nentries != -1) {
2949 err = got_error(GOT_ERR_PRIVSEP_MSG);
2950 break;
2952 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2953 if (err)
2954 break;
2955 have_commit = 0;
2956 break;
2957 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2958 *found_all_objects = 1;
2959 done = 1;
2960 break;
2961 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
2962 done = 1;
2963 break;
2964 default:
2965 err = got_error(GOT_ERR_PRIVSEP_MSG);
2966 break;
2969 imsg_free(&imsg);
2970 if (err)
2971 break;
2974 free(path);
2975 free(canon_path);
2976 free(tree.entries);
2977 return err;
2980 const struct got_error *
2981 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2982 struct got_object_id *id)
2984 struct got_imsg_raw_delta_request dreq;
2986 dreq.idx = idx;
2987 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2989 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2990 &dreq, sizeof(dreq)) == -1)
2991 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2993 return flush_imsg(ibuf);
2996 const struct got_error *
2997 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2999 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3002 const struct got_error *
3003 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3004 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3005 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3007 struct got_imsg_raw_delta idelta;
3008 int ret;
3010 idelta.base_size = base_size;
3011 idelta.result_size = result_size;
3012 idelta.delta_size = delta_size;
3013 idelta.delta_compressed_size = delta_compressed_size;
3014 idelta.delta_offset = delta_offset;
3015 idelta.delta_out_offset = delta_out_offset;
3016 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
3018 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3019 &idelta, sizeof(idelta));
3020 if (ret == -1)
3021 return got_error_from_errno("imsg_compose RAW_DELTA");
3023 return flush_imsg(ibuf);
3026 const struct got_error *
3027 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3028 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3029 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
3031 const struct got_error *err = NULL;
3032 struct imsg imsg;
3033 struct got_imsg_raw_delta *delta;
3034 size_t datalen;
3036 *base_size = 0;
3037 *result_size = 0;
3038 *delta_size = 0;
3039 *delta_compressed_size = 0;
3040 *delta_offset = 0;
3041 *delta_out_offset = 0;
3042 *base_id = NULL;
3044 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3045 if (err)
3046 return err;
3048 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3050 switch (imsg.hdr.type) {
3051 case GOT_IMSG_RAW_DELTA:
3052 if (datalen != sizeof(*delta)) {
3053 err = got_error(GOT_ERR_PRIVSEP_LEN);
3054 break;
3056 delta = imsg.data;
3057 *base_size = delta->base_size;
3058 *result_size = delta->result_size;
3059 *delta_size = delta->delta_size;
3060 *delta_compressed_size = delta->delta_compressed_size;
3061 *delta_offset = delta->delta_offset;
3062 *delta_out_offset = delta->delta_out_offset;
3063 *base_id = calloc(1, sizeof(**base_id));
3064 if (*base_id == NULL) {
3065 err = got_error_from_errno("malloc");
3066 break;
3068 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3069 break;
3070 default:
3071 err = got_error(GOT_ERR_PRIVSEP_MSG);
3072 break;
3075 imsg_free(&imsg);
3077 if (err) {
3078 free(*base_id);
3079 *base_id = NULL;
3081 return err;
3084 static const struct got_error *
3085 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3087 const struct got_error *err = NULL;
3088 struct got_imsg_object_idlist idlist;
3089 struct ibuf *wbuf;
3090 size_t i;
3092 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3093 return got_error(GOT_ERR_NO_SPACE);
3095 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3096 sizeof(idlist) + nids * sizeof(**ids));
3097 if (wbuf == NULL) {
3098 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3099 return err;
3102 idlist.nids = nids;
3103 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3104 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3106 for (i = 0; i < nids; i++) {
3107 struct got_object_id *id = ids[i];
3108 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3109 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3112 wbuf->fd = -1;
3113 imsg_close(ibuf, wbuf);
3115 return flush_imsg(ibuf);
3118 const struct got_error *
3119 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3120 struct got_object_id **ids, size_t nids)
3122 const struct got_error *err = NULL;
3123 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3124 int i, queued = 0;
3126 for (i = 0; i < nids; i++) {
3127 idlist[i % nitems(idlist)] = ids[i];
3128 queued++;
3129 if (queued >= nitems(idlist)) {
3130 err = send_idlist(ibuf, idlist, queued);
3131 if (err)
3132 return err;
3133 queued = 0;
3137 if (queued > 0) {
3138 err = send_idlist(ibuf, idlist, queued);
3139 if (err)
3140 return err;
3143 return NULL;
3146 const struct got_error *
3147 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3149 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3150 == -1)
3151 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3153 return flush_imsg(ibuf);
3156 const struct got_error *
3157 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3158 size_t *nids, struct imsgbuf *ibuf)
3160 const struct got_error *err = NULL;
3161 struct imsg imsg;
3162 struct got_imsg_object_idlist *idlist;
3163 size_t datalen;
3165 *ids = NULL;
3166 *done = 0;
3167 *nids = 0;
3169 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3170 if (err)
3171 return err;
3173 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3174 switch (imsg.hdr.type) {
3175 case GOT_IMSG_OBJ_ID_LIST:
3176 if (datalen < sizeof(*idlist)) {
3177 err = got_error(GOT_ERR_PRIVSEP_LEN);
3178 break;
3180 idlist = imsg.data;
3181 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3182 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3183 err = got_error(GOT_ERR_PRIVSEP_LEN);
3184 break;
3186 *nids = idlist->nids;
3187 *ids = calloc(*nids, sizeof(**ids));
3188 if (*ids == NULL) {
3189 err = got_error_from_errno("calloc");
3190 break;
3192 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3193 *nids * sizeof(**ids));
3194 break;
3195 case GOT_IMSG_OBJ_ID_LIST_DONE:
3196 *done = 1;
3197 break;
3198 default:
3199 err = got_error(GOT_ERR_PRIVSEP_MSG);
3200 break;
3203 imsg_free(&imsg);
3205 return err;
3208 const struct got_error *
3209 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3211 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3212 == -1)
3213 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3215 return flush_imsg(ibuf);
3218 const struct got_error *
3219 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3220 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3222 const struct got_error *err = NULL;
3223 struct ibuf *wbuf;
3224 struct got_imsg_reused_deltas ideltas;
3225 size_t i;
3227 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3228 return got_error(GOT_ERR_NO_SPACE);
3230 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3231 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3232 if (wbuf == NULL) {
3233 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3234 return err;
3237 ideltas.ndeltas = ndeltas;
3238 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3239 return got_error_from_errno("imsg_add REUSED_DELTAS");
3241 for (i = 0; i < ndeltas; i++) {
3242 struct got_imsg_reused_delta *delta = &deltas[i];
3243 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3244 return got_error_from_errno("imsg_add REUSED_DELTAS");
3247 wbuf->fd = -1;
3248 imsg_close(ibuf, wbuf);
3250 return flush_imsg(ibuf);
3253 const struct got_error *
3254 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3256 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3257 == -1)
3258 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3260 return flush_imsg(ibuf);
3263 const struct got_error *
3264 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3265 size_t *ndeltas, struct imsgbuf *ibuf)
3267 const struct got_error *err = NULL;
3268 struct imsg imsg;
3269 struct got_imsg_reused_deltas *ideltas;
3270 size_t datalen;
3272 *done = 0;
3273 *ndeltas = 0;
3275 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3276 if (err)
3277 return err;
3279 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3280 switch (imsg.hdr.type) {
3281 case GOT_IMSG_REUSED_DELTAS:
3282 if (datalen < sizeof(*ideltas)) {
3283 err = got_error(GOT_ERR_PRIVSEP_LEN);
3284 break;
3286 ideltas = imsg.data;
3287 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3288 ideltas->ndeltas * sizeof(*deltas) >
3289 datalen - sizeof(*ideltas)) {
3290 err = got_error(GOT_ERR_PRIVSEP_LEN);
3291 break;
3293 *ndeltas = ideltas->ndeltas;
3294 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3295 *ndeltas * sizeof(*deltas));
3296 break;
3297 case GOT_IMSG_DELTA_REUSE_DONE:
3298 *done = 1;
3299 break;
3300 default:
3301 err = got_error(GOT_ERR_PRIVSEP_MSG);
3302 break;
3305 imsg_free(&imsg);
3307 return err;
3310 const struct got_error *
3311 got_privsep_unveil_exec_helpers(void)
3313 const char *helpers[] = {
3314 GOT_PATH_PROG_READ_PACK,
3315 GOT_PATH_PROG_READ_OBJECT,
3316 GOT_PATH_PROG_READ_COMMIT,
3317 GOT_PATH_PROG_READ_TREE,
3318 GOT_PATH_PROG_READ_BLOB,
3319 GOT_PATH_PROG_READ_TAG,
3320 GOT_PATH_PROG_READ_GITCONFIG,
3321 GOT_PATH_PROG_READ_GOTCONFIG,
3322 GOT_PATH_PROG_READ_PATCH,
3323 GOT_PATH_PROG_FETCH_PACK,
3324 GOT_PATH_PROG_INDEX_PACK,
3325 GOT_PATH_PROG_SEND_PACK,
3327 size_t i;
3329 for (i = 0; i < nitems(helpers); i++) {
3330 if (unveil(helpers[i], "x") == 0)
3331 continue;
3332 return got_error_from_errno2("unveil", helpers[i]);
3335 return NULL;
3338 void
3339 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3341 if (close(imsg_fds[0]) == -1) {
3342 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3343 _exit(1);
3346 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3347 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3348 _exit(1);
3351 closefrom(GOT_IMSG_FD_CHILD + 1);
3353 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3354 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3355 strerror(errno));
3356 _exit(1);