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 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
535 iobj.type = obj->type;
536 iobj.flags = obj->flags;
537 iobj.hdrlen = obj->hdrlen;
538 iobj.size = obj->size;
539 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
540 iobj.pack_offset = obj->pack_offset;
541 iobj.pack_idx = obj->pack_idx;
544 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
545 == -1)
546 return got_error_from_errno("imsg_compose OBJECT");
548 return flush_imsg(ibuf);
551 const struct got_error *
552 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
553 struct got_pathlist_head *have_refs, int fetch_all_branches,
554 struct got_pathlist_head *wanted_branches,
555 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
557 const struct got_error *err = NULL;
558 struct ibuf *wbuf;
559 size_t len;
560 struct got_pathlist_entry *pe;
561 struct got_imsg_fetch_request fetchreq;
563 memset(&fetchreq, 0, sizeof(fetchreq));
564 fetchreq.fetch_all_branches = fetch_all_branches;
565 fetchreq.list_refs_only = list_refs_only;
566 fetchreq.verbosity = verbosity;
567 TAILQ_FOREACH(pe, have_refs, entry)
568 fetchreq.n_have_refs++;
569 TAILQ_FOREACH(pe, wanted_branches, entry)
570 fetchreq.n_wanted_branches++;
571 TAILQ_FOREACH(pe, wanted_refs, entry)
572 fetchreq.n_wanted_refs++;
573 len = sizeof(struct got_imsg_fetch_request);
574 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
575 close(fd);
576 return got_error(GOT_ERR_NO_SPACE);
579 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
580 &fetchreq, sizeof(fetchreq)) == -1)
581 return got_error_from_errno(
582 "imsg_compose FETCH_SERVER_PROGRESS");
584 err = flush_imsg(ibuf);
585 if (err) {
586 close(fd);
587 return err;
589 fd = -1;
591 TAILQ_FOREACH(pe, have_refs, entry) {
592 const char *name = pe->path;
593 size_t name_len = pe->path_len;
594 struct got_object_id *id = pe->data;
596 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
597 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
598 if (wbuf == NULL)
599 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
601 /* Keep in sync with struct got_imsg_fetch_have_ref! */
602 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
603 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
604 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
605 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
606 if (imsg_add(wbuf, name, name_len) == -1)
607 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
609 wbuf->fd = -1;
610 imsg_close(ibuf, wbuf);
611 err = flush_imsg(ibuf);
612 if (err)
613 return err;
616 TAILQ_FOREACH(pe, wanted_branches, entry) {
617 const char *name = pe->path;
618 size_t name_len = pe->path_len;
620 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
621 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
622 len);
623 if (wbuf == NULL)
624 return got_error_from_errno(
625 "imsg_create FETCH_WANTED_BRANCH");
627 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
628 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
629 return got_error_from_errno(
630 "imsg_add FETCH_WANTED_BRANCH");
631 if (imsg_add(wbuf, name, name_len) == -1)
632 return got_error_from_errno(
633 "imsg_add FETCH_WANTED_BRANCH");
635 wbuf->fd = -1;
636 imsg_close(ibuf, wbuf);
637 err = flush_imsg(ibuf);
638 if (err)
639 return err;
642 TAILQ_FOREACH(pe, wanted_refs, entry) {
643 const char *name = pe->path;
644 size_t name_len = pe->path_len;
646 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
647 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
648 len);
649 if (wbuf == NULL)
650 return got_error_from_errno(
651 "imsg_create FETCH_WANTED_REF");
653 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
654 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
655 return got_error_from_errno(
656 "imsg_add FETCH_WANTED_REF");
657 if (imsg_add(wbuf, name, name_len) == -1)
658 return got_error_from_errno(
659 "imsg_add FETCH_WANTED_REF");
661 wbuf->fd = -1;
662 imsg_close(ibuf, wbuf);
663 err = flush_imsg(ibuf);
664 if (err)
665 return err;
669 return NULL;
673 const struct got_error *
674 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
676 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
679 const struct got_error *
680 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
681 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
682 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
684 const struct got_error *err = NULL;
685 struct imsg imsg;
686 size_t datalen;
687 struct got_imsg_fetch_symrefs *isymrefs = NULL;
688 size_t n, remain;
689 off_t off;
690 int i;
692 *done = 0;
693 *id = NULL;
694 *refname = NULL;
695 *server_progress = NULL;
696 *packfile_size = 0;
697 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
699 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
700 if (err)
701 return err;
703 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
704 switch (imsg.hdr.type) {
705 case GOT_IMSG_ERROR:
706 err = recv_imsg_error(&imsg, datalen);
707 break;
708 case GOT_IMSG_FETCH_SYMREFS:
709 if (datalen < sizeof(*isymrefs)) {
710 err = got_error(GOT_ERR_PRIVSEP_LEN);
711 break;
713 if (isymrefs != NULL) {
714 err = got_error(GOT_ERR_PRIVSEP_MSG);
715 break;
717 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
718 off = sizeof(*isymrefs);
719 remain = datalen - off;
720 for (n = 0; n < isymrefs->nsymrefs; n++) {
721 struct got_imsg_fetch_symref *s;
722 char *name, *target;
723 if (remain < sizeof(struct got_imsg_fetch_symref)) {
724 err = got_error(GOT_ERR_PRIVSEP_LEN);
725 goto done;
727 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
728 off += sizeof(*s);
729 remain -= sizeof(*s);
730 if (remain < s->name_len) {
731 err = got_error(GOT_ERR_PRIVSEP_LEN);
732 goto done;
734 name = strndup(imsg.data + off, s->name_len);
735 if (name == NULL) {
736 err = got_error_from_errno("strndup");
737 goto done;
739 off += s->name_len;
740 remain -= s->name_len;
741 if (remain < s->target_len) {
742 err = got_error(GOT_ERR_PRIVSEP_LEN);
743 free(name);
744 goto done;
746 target = strndup(imsg.data + off, s->target_len);
747 if (target == NULL) {
748 err = got_error_from_errno("strndup");
749 free(name);
750 goto done;
752 off += s->target_len;
753 remain -= s->target_len;
754 err = got_pathlist_append(symrefs, name, target);
755 if (err) {
756 free(name);
757 free(target);
758 goto done;
761 break;
762 case GOT_IMSG_FETCH_REF:
763 if (datalen <= SHA1_DIGEST_LENGTH) {
764 err = got_error(GOT_ERR_PRIVSEP_MSG);
765 break;
767 *id = malloc(sizeof(**id));
768 if (*id == NULL) {
769 err = got_error_from_errno("malloc");
770 break;
772 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
773 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
774 datalen - SHA1_DIGEST_LENGTH);
775 if (*refname == NULL) {
776 err = got_error_from_errno("strndup");
777 break;
779 break;
780 case GOT_IMSG_FETCH_SERVER_PROGRESS:
781 if (datalen == 0) {
782 err = got_error(GOT_ERR_PRIVSEP_LEN);
783 break;
785 *server_progress = strndup(imsg.data, datalen);
786 if (*server_progress == NULL) {
787 err = got_error_from_errno("strndup");
788 break;
790 for (i = 0; i < datalen; i++) {
791 if (!isprint((unsigned char)(*server_progress)[i]) &&
792 !isspace((unsigned char)(*server_progress)[i])) {
793 err = got_error(GOT_ERR_PRIVSEP_MSG);
794 free(*server_progress);
795 *server_progress = NULL;
796 goto done;
799 break;
800 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
801 if (datalen < sizeof(*packfile_size)) {
802 err = got_error(GOT_ERR_PRIVSEP_MSG);
803 break;
805 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
806 break;
807 case GOT_IMSG_FETCH_DONE:
808 if (datalen != SHA1_DIGEST_LENGTH) {
809 err = got_error(GOT_ERR_PRIVSEP_MSG);
810 break;
812 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
813 *done = 1;
814 break;
815 default:
816 err = got_error(GOT_ERR_PRIVSEP_MSG);
817 break;
819 done:
820 if (err) {
821 free(*id);
822 *id = NULL;
823 free(*refname);
824 *refname = NULL;
826 imsg_free(&imsg);
827 return err;
830 static const struct got_error *
831 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
832 int delete, struct imsgbuf *ibuf)
834 size_t len;
835 struct ibuf *wbuf;
837 len = sizeof(struct got_imsg_send_ref) + name_len;
838 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
839 if (wbuf == NULL)
840 return got_error_from_errno("imsg_create SEND_REF");
842 /* Keep in sync with struct got_imsg_send_ref! */
843 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
844 return got_error_from_errno("imsg_add SEND_REF");
845 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
846 return got_error_from_errno("imsg_add SEND_REF");
847 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
848 return got_error_from_errno("imsg_add SEND_REF");
849 if (imsg_add(wbuf, name, name_len) == -1)
850 return got_error_from_errno("imsg_add SEND_REF");
852 wbuf->fd = -1;
853 imsg_close(ibuf, wbuf);
854 return flush_imsg(ibuf);
857 const struct got_error *
858 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
859 struct got_pathlist_head *have_refs,
860 struct got_pathlist_head *delete_refs,
861 int verbosity)
863 const struct got_error *err = NULL;
864 struct got_pathlist_entry *pe;
865 struct got_imsg_send_request sendreq;
866 struct got_object_id zero_id;
868 memset(&zero_id, 0, sizeof(zero_id));
869 memset(&sendreq, 0, sizeof(sendreq));
870 sendreq.verbosity = verbosity;
871 TAILQ_FOREACH(pe, have_refs, entry)
872 sendreq.nrefs++;
873 TAILQ_FOREACH(pe, delete_refs, entry)
874 sendreq.nrefs++;
875 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
876 &sendreq, sizeof(sendreq)) == -1) {
877 err = got_error_from_errno(
878 "imsg_compose FETCH_SERVER_PROGRESS");
879 goto done;
882 err = flush_imsg(ibuf);
883 if (err)
884 goto done;
885 fd = -1;
887 TAILQ_FOREACH(pe, have_refs, entry) {
888 const char *name = pe->path;
889 size_t name_len = pe->path_len;
890 struct got_object_id *id = pe->data;
891 err = send_send_ref(name, name_len, id, 0, ibuf);
892 if (err)
893 goto done;
896 TAILQ_FOREACH(pe, delete_refs, entry) {
897 const char *name = pe->path;
898 size_t name_len = pe->path_len;
899 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
900 if (err)
901 goto done;
903 done:
904 if (fd != -1 && close(fd) == -1 && err == NULL)
905 err = got_error_from_errno("close");
906 return err;
910 const struct got_error *
911 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
912 struct imsgbuf *ibuf)
914 const struct got_error *err = NULL;
915 struct imsg imsg;
916 size_t datalen;
917 int done = 0;
918 struct got_imsg_send_remote_ref iremote_ref;
919 struct got_object_id *id = NULL;
920 char *refname = NULL;
921 struct got_pathlist_entry *new;
923 while (!done) {
924 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
925 if (err)
926 return err;
927 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
928 switch (imsg.hdr.type) {
929 case GOT_IMSG_ERROR:
930 err = recv_imsg_error(&imsg, datalen);
931 goto done;
932 case GOT_IMSG_SEND_REMOTE_REF:
933 if (datalen < sizeof(iremote_ref)) {
934 err = got_error(GOT_ERR_PRIVSEP_MSG);
935 goto done;
937 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
938 if (datalen != sizeof(iremote_ref) +
939 iremote_ref.name_len) {
940 err = got_error(GOT_ERR_PRIVSEP_MSG);
941 goto done;
943 id = malloc(sizeof(*id));
944 if (id == NULL) {
945 err = got_error_from_errno("malloc");
946 goto done;
948 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
949 refname = strndup(imsg.data + sizeof(iremote_ref),
950 datalen - sizeof(iremote_ref));
951 if (refname == NULL) {
952 err = got_error_from_errno("strndup");
953 goto done;
955 err = got_pathlist_insert(&new, remote_refs,
956 refname, id);
957 if (err)
958 goto done;
959 if (new == NULL) { /* duplicate which wasn't inserted */
960 free(id);
961 free(refname);
963 id = NULL;
964 refname = NULL;
965 break;
966 case GOT_IMSG_SEND_PACK_REQUEST:
967 if (datalen != 0) {
968 err = got_error(GOT_ERR_PRIVSEP_MSG);
969 goto done;
971 /* got-send-pack is now waiting for a pack file. */
972 done = 1;
973 break;
974 default:
975 err = got_error(GOT_ERR_PRIVSEP_MSG);
976 break;
979 done:
980 free(id);
981 free(refname);
982 imsg_free(&imsg);
983 return err;
986 const struct got_error *
987 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
989 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
992 const struct got_error *
993 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
994 int *success, char **refname, struct imsgbuf *ibuf)
996 const struct got_error *err = NULL;
997 struct imsg imsg;
998 size_t datalen;
999 struct got_imsg_send_ref_status iref_status;
1001 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1002 *done = 0;
1003 *success = 0;
1004 *refname = NULL;
1006 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1007 if (err)
1008 return err;
1010 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1011 switch (imsg.hdr.type) {
1012 case GOT_IMSG_ERROR:
1013 err = recv_imsg_error(&imsg, datalen);
1014 break;
1015 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1016 if (datalen < sizeof(*bytes_sent)) {
1017 err = got_error(GOT_ERR_PRIVSEP_MSG);
1018 break;
1020 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1021 break;
1022 case GOT_IMSG_SEND_REF_STATUS:
1023 if (datalen < sizeof(iref_status)) {
1024 err = got_error(GOT_ERR_PRIVSEP_MSG);
1025 break;
1027 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1028 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1029 err = got_error(GOT_ERR_PRIVSEP_MSG);
1030 break;
1032 *success = iref_status.success;
1033 *refname = strndup(imsg.data + sizeof(iref_status),
1034 iref_status.name_len);
1035 break;
1036 case GOT_IMSG_SEND_DONE:
1037 if (datalen != 0) {
1038 err = got_error(GOT_ERR_PRIVSEP_MSG);
1039 break;
1041 *done = 1;
1042 break;
1043 default:
1044 err = got_error(GOT_ERR_PRIVSEP_MSG);
1045 break;
1048 imsg_free(&imsg);
1049 return err;
1052 const struct got_error *
1053 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1054 int fd)
1056 const struct got_error *err = NULL;
1058 /* Keep in sync with struct got_imsg_index_pack_request */
1059 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1060 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1061 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1062 close(fd);
1063 return err;
1065 return flush_imsg(ibuf);
1068 const struct got_error *
1069 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1071 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1074 const struct got_error *
1075 got_privsep_recv_index_progress(int *done, int *nobj_total,
1076 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1077 struct imsgbuf *ibuf)
1079 const struct got_error *err = NULL;
1080 struct imsg imsg;
1081 struct got_imsg_index_pack_progress *iprogress;
1082 size_t datalen;
1084 *done = 0;
1085 *nobj_total = 0;
1086 *nobj_indexed = 0;
1087 *nobj_resolved = 0;
1089 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1090 if (err)
1091 return err;
1093 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1094 switch (imsg.hdr.type) {
1095 case GOT_IMSG_ERROR:
1096 err = recv_imsg_error(&imsg, datalen);
1097 break;
1098 case GOT_IMSG_IDXPACK_PROGRESS:
1099 if (datalen < sizeof(*iprogress)) {
1100 err = got_error(GOT_ERR_PRIVSEP_LEN);
1101 break;
1103 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1104 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1105 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1106 err = got_error(GOT_ERR_RANGE);
1107 break;
1109 *nobj_total = iprogress->nobj_total;
1110 *nobj_indexed = iprogress->nobj_indexed;
1111 *nobj_loose = iprogress->nobj_loose;
1112 *nobj_resolved = iprogress->nobj_resolved;
1113 break;
1114 case GOT_IMSG_IDXPACK_DONE:
1115 if (datalen != 0) {
1116 err = got_error(GOT_ERR_PRIVSEP_LEN);
1117 break;
1119 *done = 1;
1120 break;
1121 default:
1122 err = got_error(GOT_ERR_PRIVSEP_MSG);
1123 break;
1126 imsg_free(&imsg);
1127 return err;
1130 const struct got_error *
1131 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1132 struct imsgbuf *ibuf)
1134 struct got_imsg_object *iobj;
1135 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1137 if (datalen != sizeof(*iobj))
1138 return got_error(GOT_ERR_PRIVSEP_LEN);
1139 iobj = imsg->data;
1141 if (iobj->pack_offset < 0)
1142 return got_error(GOT_ERR_PACK_OFFSET);
1144 *obj = calloc(1, sizeof(**obj));
1145 if (*obj == NULL)
1146 return got_error_from_errno("calloc");
1148 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1149 (*obj)->type = iobj->type;
1150 (*obj)->flags = iobj->flags;
1151 (*obj)->hdrlen = iobj->hdrlen;
1152 (*obj)->size = iobj->size;
1153 /* path_packfile is handled by caller */
1154 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1155 (*obj)->pack_offset = iobj->pack_offset;
1156 (*obj)->pack_idx = iobj->pack_idx;
1158 STAILQ_INIT(&(*obj)->deltas.entries);
1159 return NULL;
1162 const struct got_error *
1163 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1165 const struct got_error *err = NULL;
1166 struct imsg imsg;
1167 const size_t min_datalen =
1168 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1170 *obj = NULL;
1172 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1173 if (err)
1174 return err;
1176 switch (imsg.hdr.type) {
1177 case GOT_IMSG_OBJECT:
1178 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1179 break;
1180 default:
1181 err = got_error(GOT_ERR_PRIVSEP_MSG);
1182 break;
1185 imsg_free(&imsg);
1187 return err;
1190 static const struct got_error *
1191 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1192 size_t logmsg_len)
1194 const struct got_error *err = NULL;
1195 size_t offset, remain;
1197 offset = 0;
1198 remain = logmsg_len;
1199 while (remain > 0) {
1200 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1202 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1203 commit->logmsg + offset, n) == -1) {
1204 err = got_error_from_errno("imsg_compose "
1205 "COMMIT_LOGMSG");
1206 break;
1209 err = flush_imsg(ibuf);
1210 if (err)
1211 break;
1213 offset += n;
1214 remain -= n;
1217 return err;
1220 const struct got_error *
1221 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1223 const struct got_error *err = NULL;
1224 struct got_imsg_commit_object *icommit;
1225 uint8_t *buf;
1226 size_t len, total;
1227 struct got_object_qid *qid;
1228 size_t author_len = strlen(commit->author);
1229 size_t committer_len = strlen(commit->committer);
1230 size_t logmsg_len = strlen(commit->logmsg);
1232 total = sizeof(*icommit) + author_len + committer_len +
1233 commit->nparents * SHA1_DIGEST_LENGTH;
1235 buf = malloc(total);
1236 if (buf == NULL)
1237 return got_error_from_errno("malloc");
1239 icommit = (struct got_imsg_commit_object *)buf;
1240 memcpy(icommit->tree_id, commit->tree_id->sha1,
1241 sizeof(icommit->tree_id));
1242 icommit->author_len = author_len;
1243 icommit->author_time = commit->author_time;
1244 icommit->author_gmtoff = commit->author_gmtoff;
1245 icommit->committer_len = committer_len;
1246 icommit->committer_time = commit->committer_time;
1247 icommit->committer_gmtoff = commit->committer_gmtoff;
1248 icommit->logmsg_len = logmsg_len;
1249 icommit->nparents = commit->nparents;
1251 len = sizeof(*icommit);
1252 memcpy(buf + len, commit->author, author_len);
1253 len += author_len;
1254 memcpy(buf + len, commit->committer, committer_len);
1255 len += committer_len;
1256 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1257 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1258 len += SHA1_DIGEST_LENGTH;
1261 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1262 err = got_error_from_errno("imsg_compose COMMIT");
1263 goto done;
1266 if (logmsg_len == 0 ||
1267 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1268 err = flush_imsg(ibuf);
1269 if (err)
1270 goto done;
1272 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1273 done:
1274 free(buf);
1275 return err;
1278 static const struct got_error *
1279 get_commit_from_imsg(struct got_commit_object **commit,
1280 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1282 const struct got_error *err = NULL;
1283 struct got_imsg_commit_object *icommit;
1284 size_t len = 0;
1285 int i;
1287 if (datalen < sizeof(*icommit))
1288 return got_error(GOT_ERR_PRIVSEP_LEN);
1290 icommit = imsg->data;
1291 if (datalen != sizeof(*icommit) + icommit->author_len +
1292 icommit->committer_len +
1293 icommit->nparents * SHA1_DIGEST_LENGTH)
1294 return got_error(GOT_ERR_PRIVSEP_LEN);
1296 if (icommit->nparents < 0)
1297 return got_error(GOT_ERR_PRIVSEP_LEN);
1299 len += sizeof(*icommit);
1301 *commit = got_object_commit_alloc_partial();
1302 if (*commit == NULL)
1303 return got_error_from_errno(
1304 "got_object_commit_alloc_partial");
1306 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1307 SHA1_DIGEST_LENGTH);
1308 (*commit)->author_time = icommit->author_time;
1309 (*commit)->author_gmtoff = icommit->author_gmtoff;
1310 (*commit)->committer_time = icommit->committer_time;
1311 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1313 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1314 if ((*commit)->author == NULL) {
1315 err = got_error_from_errno("strndup");
1316 goto done;
1318 len += icommit->author_len;
1320 (*commit)->committer = strndup(imsg->data + len,
1321 icommit->committer_len);
1322 if ((*commit)->committer == NULL) {
1323 err = got_error_from_errno("strndup");
1324 goto done;
1326 len += icommit->committer_len;
1328 if (icommit->logmsg_len == 0) {
1329 (*commit)->logmsg = strdup("");
1330 if ((*commit)->logmsg == NULL) {
1331 err = got_error_from_errno("strdup");
1332 goto done;
1334 } else {
1335 size_t offset = 0, remain = icommit->logmsg_len;
1337 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1338 if ((*commit)->logmsg == NULL) {
1339 err = got_error_from_errno("malloc");
1340 goto done;
1342 while (remain > 0) {
1343 struct imsg imsg_log;
1344 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1345 remain);
1347 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1348 if (err)
1349 goto done;
1351 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1352 err = got_error(GOT_ERR_PRIVSEP_MSG);
1353 goto done;
1356 memcpy((*commit)->logmsg + offset,
1357 imsg_log.data, n);
1358 imsg_free(&imsg_log);
1359 offset += n;
1360 remain -= n;
1362 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1365 for (i = 0; i < icommit->nparents; i++) {
1366 struct got_object_qid *qid;
1368 err = got_object_qid_alloc_partial(&qid);
1369 if (err)
1370 break;
1371 memcpy(&qid->id, imsg->data + len +
1372 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1373 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1374 (*commit)->nparents++;
1376 done:
1377 if (err) {
1378 got_object_commit_close(*commit);
1379 *commit = NULL;
1381 return err;
1384 const struct got_error *
1385 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1387 const struct got_error *err = NULL;
1388 struct imsg imsg;
1389 size_t datalen;
1390 const size_t min_datalen =
1391 MIN(sizeof(struct got_imsg_error),
1392 sizeof(struct got_imsg_commit_object));
1394 *commit = NULL;
1396 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1397 if (err)
1398 return err;
1400 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1402 switch (imsg.hdr.type) {
1403 case GOT_IMSG_COMMIT:
1404 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1405 break;
1406 default:
1407 err = got_error(GOT_ERR_PRIVSEP_MSG);
1408 break;
1411 imsg_free(&imsg);
1413 return err;
1416 static const struct got_error *
1417 send_tree_entries_batch(struct imsgbuf *ibuf,
1418 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1420 struct ibuf *wbuf;
1421 struct got_imsg_tree_entries ientries;
1422 int i;
1424 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1425 if (wbuf == NULL)
1426 return got_error_from_errno("imsg_create TREE_ENTRY");
1428 ientries.nentries = idxN - idx0 + 1;
1429 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1430 return got_error_from_errno("imsg_add TREE_ENTRY");
1432 for (i = idx0; i <= idxN; i++) {
1433 struct got_parsed_tree_entry *pte = &entries[i];
1435 /* Keep in sync with struct got_imsg_tree_object definition! */
1436 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1437 return got_error_from_errno("imsg_add TREE_ENTRY");
1438 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1439 return got_error_from_errno("imsg_add TREE_ENTRY");
1440 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1441 return got_error_from_errno("imsg_add TREE_ENTRY");
1443 /* Remaining bytes are the entry's name. */
1444 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1445 return got_error_from_errno("imsg_add TREE_ENTRY");
1448 wbuf->fd = -1;
1449 imsg_close(ibuf, wbuf);
1450 return NULL;
1453 static const struct got_error *
1454 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1455 int nentries)
1457 const struct got_error *err = NULL;
1458 int i, j;
1459 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1461 i = 0;
1462 for (j = 0; j < nentries; j++) {
1463 struct got_parsed_tree_entry *pte = &entries[j];
1464 size_t len = sizeof(*pte) + pte->namelen;
1466 if (j > 0 &&
1467 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1468 err = send_tree_entries_batch(ibuf, entries,
1469 i, j - 1, entries_len);
1470 if (err)
1471 return err;
1472 i = j;
1473 entries_len = sizeof(struct got_imsg_tree_entries);
1476 entries_len += len;
1479 if (j > 0) {
1480 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1481 entries_len);
1482 if (err)
1483 return err;
1486 return NULL;
1489 const struct got_error *
1490 got_privsep_send_tree(struct imsgbuf *ibuf,
1491 struct got_parsed_tree_entry *entries, int nentries)
1493 const struct got_error *err = NULL;
1494 struct got_imsg_tree_object 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 err = read_imsg(ibuf);
1584 if (err)
1585 goto done;
1587 for (;;) {
1588 struct imsg imsg;
1589 size_t n;
1590 size_t datalen;
1592 n = imsg_get(ibuf, &imsg);
1593 if (n == 0) {
1594 if ((*tree)) {
1595 if (nentries < (*tree)->nentries) {
1596 err = read_imsg(ibuf);
1597 if (err)
1598 break;
1599 continue;
1600 } else
1601 break;
1602 } else {
1603 err = got_error(GOT_ERR_PRIVSEP_MSG);
1604 break;
1608 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1609 imsg_free(&imsg);
1610 err = got_error(GOT_ERR_PRIVSEP_LEN);
1611 break;
1614 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1616 switch (imsg.hdr.type) {
1617 case GOT_IMSG_ERROR:
1618 err = recv_imsg_error(&imsg, datalen);
1619 break;
1620 case GOT_IMSG_TREE:
1621 /* This message should only appear once. */
1622 if (*tree != NULL) {
1623 err = got_error(GOT_ERR_PRIVSEP_MSG);
1624 break;
1626 if (datalen != sizeof(*itree)) {
1627 err = got_error(GOT_ERR_PRIVSEP_LEN);
1628 break;
1630 itree = imsg.data;
1631 if (itree->nentries < 0) {
1632 err = got_error(GOT_ERR_PRIVSEP_LEN);
1633 break;
1635 *tree = malloc(sizeof(**tree));
1636 if (*tree == NULL) {
1637 err = got_error_from_errno("malloc");
1638 break;
1640 (*tree)->entries = calloc(itree->nentries,
1641 sizeof(struct got_tree_entry));
1642 if ((*tree)->entries == NULL) {
1643 err = got_error_from_errno("malloc");
1644 free(*tree);
1645 *tree = NULL;
1646 break;
1648 (*tree)->nentries = itree->nentries;
1649 (*tree)->refcnt = 0;
1650 break;
1651 case GOT_IMSG_TREE_ENTRIES:
1652 /* This message should be preceeded by GOT_IMSG_TREE. */
1653 if (*tree == NULL) {
1654 err = got_error(GOT_ERR_PRIVSEP_MSG);
1655 break;
1657 err = recv_tree_entries(imsg.data, datalen,
1658 *tree, &nentries);
1659 break;
1660 default:
1661 err = got_error(GOT_ERR_PRIVSEP_MSG);
1662 break;
1665 imsg_free(&imsg);
1666 if (err)
1667 break;
1669 done:
1670 if (*tree && (*tree)->nentries != nentries) {
1671 if (err == NULL)
1672 err = got_error(GOT_ERR_PRIVSEP_LEN);
1673 got_object_tree_close(*tree);
1674 *tree = NULL;
1677 return err;
1680 const struct got_error *
1681 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1682 const uint8_t *data)
1684 struct got_imsg_blob iblob;
1686 iblob.size = size;
1687 iblob.hdrlen = hdrlen;
1689 if (data) {
1690 uint8_t *buf;
1692 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1693 return got_error(GOT_ERR_NO_SPACE);
1695 buf = malloc(sizeof(iblob) + size);
1696 if (buf == NULL)
1697 return got_error_from_errno("malloc");
1699 memcpy(buf, &iblob, sizeof(iblob));
1700 memcpy(buf + sizeof(iblob), data, size);
1701 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1702 sizeof(iblob) + size) == -1) {
1703 free(buf);
1704 return got_error_from_errno("imsg_compose BLOB");
1706 free(buf);
1707 } else {
1708 /* Data has already been written to file descriptor. */
1709 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1710 sizeof(iblob)) == -1)
1711 return got_error_from_errno("imsg_compose BLOB");
1715 return flush_imsg(ibuf);
1718 const struct got_error *
1719 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1720 struct imsgbuf *ibuf)
1722 const struct got_error *err = NULL;
1723 struct imsg imsg;
1724 struct got_imsg_blob *iblob;
1725 size_t datalen;
1727 *outbuf = NULL;
1729 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1730 if (err)
1731 return err;
1733 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1735 switch (imsg.hdr.type) {
1736 case GOT_IMSG_BLOB:
1737 if (datalen < sizeof(*iblob)) {
1738 err = got_error(GOT_ERR_PRIVSEP_LEN);
1739 break;
1741 iblob = imsg.data;
1742 *size = iblob->size;
1743 *hdrlen = iblob->hdrlen;
1745 if (datalen == sizeof(*iblob)) {
1746 /* Data has been written to file descriptor. */
1747 break;
1750 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1751 *size > datalen + sizeof(*iblob)) {
1752 err = got_error(GOT_ERR_PRIVSEP_LEN);
1753 break;
1756 *outbuf = malloc(*size);
1757 if (*outbuf == NULL) {
1758 err = got_error_from_errno("malloc");
1759 break;
1761 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1762 break;
1763 default:
1764 err = got_error(GOT_ERR_PRIVSEP_MSG);
1765 break;
1768 imsg_free(&imsg);
1770 return err;
1773 static const struct got_error *
1774 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1776 const struct got_error *err = NULL;
1777 size_t offset, remain;
1779 offset = 0;
1780 remain = tagmsg_len;
1781 while (remain > 0) {
1782 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1784 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1785 tag->tagmsg + offset, n) == -1) {
1786 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1787 break;
1790 err = flush_imsg(ibuf);
1791 if (err)
1792 break;
1794 offset += n;
1795 remain -= n;
1798 return err;
1801 const struct got_error *
1802 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1804 const struct got_error *err = NULL;
1805 struct got_imsg_tag_object *itag;
1806 uint8_t *buf;
1807 size_t len, total;
1808 size_t tag_len = strlen(tag->tag);
1809 size_t tagger_len = strlen(tag->tagger);
1810 size_t tagmsg_len = strlen(tag->tagmsg);
1812 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1814 buf = malloc(total);
1815 if (buf == NULL)
1816 return got_error_from_errno("malloc");
1818 itag = (struct got_imsg_tag_object *)buf;
1819 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1820 itag->obj_type = tag->obj_type;
1821 itag->tag_len = tag_len;
1822 itag->tagger_len = tagger_len;
1823 itag->tagger_time = tag->tagger_time;
1824 itag->tagger_gmtoff = tag->tagger_gmtoff;
1825 itag->tagmsg_len = tagmsg_len;
1827 len = sizeof(*itag);
1828 memcpy(buf + len, tag->tag, tag_len);
1829 len += tag_len;
1830 memcpy(buf + len, tag->tagger, tagger_len);
1831 len += tagger_len;
1833 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1834 err = got_error_from_errno("imsg_compose TAG");
1835 goto done;
1838 if (tagmsg_len == 0 ||
1839 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1840 err = flush_imsg(ibuf);
1841 if (err)
1842 goto done;
1844 err = send_tagmsg(ibuf, tag, tagmsg_len);
1845 done:
1846 free(buf);
1847 return err;
1850 const struct got_error *
1851 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1853 const struct got_error *err = NULL;
1854 struct imsg imsg;
1855 struct got_imsg_tag_object *itag;
1856 size_t len, datalen;
1857 const size_t min_datalen =
1858 MIN(sizeof(struct got_imsg_error),
1859 sizeof(struct got_imsg_tag_object));
1861 *tag = NULL;
1863 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1864 if (err)
1865 return err;
1867 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1868 len = 0;
1870 switch (imsg.hdr.type) {
1871 case GOT_IMSG_TAG:
1872 if (datalen < sizeof(*itag)) {
1873 err = got_error(GOT_ERR_PRIVSEP_LEN);
1874 break;
1876 itag = imsg.data;
1877 if (datalen != sizeof(*itag) + itag->tag_len +
1878 itag->tagger_len) {
1879 err = got_error(GOT_ERR_PRIVSEP_LEN);
1880 break;
1882 len += sizeof(*itag);
1884 *tag = calloc(1, sizeof(**tag));
1885 if (*tag == NULL) {
1886 err = got_error_from_errno("calloc");
1887 break;
1890 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1892 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1893 if ((*tag)->tag == NULL) {
1894 err = got_error_from_errno("strndup");
1895 break;
1897 len += itag->tag_len;
1899 (*tag)->obj_type = itag->obj_type;
1900 (*tag)->tagger_time = itag->tagger_time;
1901 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1903 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1904 if ((*tag)->tagger == NULL) {
1905 err = got_error_from_errno("strndup");
1906 break;
1908 len += itag->tagger_len;
1910 if (itag->tagmsg_len == 0) {
1911 (*tag)->tagmsg = strdup("");
1912 if ((*tag)->tagmsg == NULL) {
1913 err = got_error_from_errno("strdup");
1914 break;
1916 } else {
1917 size_t offset = 0, remain = itag->tagmsg_len;
1919 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1920 if ((*tag)->tagmsg == NULL) {
1921 err = got_error_from_errno("malloc");
1922 break;
1924 while (remain > 0) {
1925 struct imsg imsg_log;
1926 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1927 remain);
1929 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1930 if (err)
1931 return err;
1933 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1934 return got_error(GOT_ERR_PRIVSEP_MSG);
1936 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1937 n);
1938 imsg_free(&imsg_log);
1939 offset += n;
1940 remain -= n;
1942 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1945 break;
1946 default:
1947 err = got_error(GOT_ERR_PRIVSEP_MSG);
1948 break;
1951 imsg_free(&imsg);
1953 return err;
1956 const struct got_error *
1957 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1958 struct got_packidx *packidx)
1960 const struct got_error *err = NULL;
1961 struct got_imsg_packidx ipackidx;
1962 struct got_imsg_pack ipack;
1963 int fd;
1965 ipackidx.len = packidx->len;
1966 ipackidx.packfile_size = pack->filesize;
1967 fd = dup(packidx->fd);
1968 if (fd == -1)
1969 return got_error_from_errno("dup");
1971 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1972 sizeof(ipackidx)) == -1) {
1973 err = got_error_from_errno("imsg_compose PACKIDX");
1974 close(fd);
1975 return err;
1978 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1979 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1980 return got_error(GOT_ERR_NO_SPACE);
1981 ipack.filesize = pack->filesize;
1983 fd = dup(pack->fd);
1984 if (fd == -1)
1985 return got_error_from_errno("dup");
1987 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1988 == -1) {
1989 err = got_error_from_errno("imsg_compose PACK");
1990 close(fd);
1991 return err;
1994 return flush_imsg(ibuf);
1997 const struct got_error *
1998 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1999 struct got_object_id *id)
2001 struct got_imsg_packed_object iobj;
2003 iobj.idx = idx;
2004 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2006 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2007 &iobj, sizeof(iobj)) == -1)
2008 return got_error_from_errno("imsg_compose "
2009 "PACKED_OBJECT_REQUEST");
2011 return flush_imsg(ibuf);
2014 const struct got_error *
2015 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2016 struct got_object_id *id)
2018 struct got_imsg_packed_object iobj;
2020 iobj.idx = idx;
2021 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2023 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2024 &iobj, sizeof(iobj)) == -1)
2025 return got_error_from_errno("imsg_compose "
2026 "PACKED_OBJECT_REQUEST");
2028 return flush_imsg(ibuf);
2031 const struct got_error *
2032 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2034 const struct got_error *err = NULL;
2036 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2037 NULL, 0) == -1) {
2038 err = got_error_from_errno("imsg_compose "
2039 "GITCONFIG_PARSE_REQUEST");
2040 close(fd);
2041 return err;
2044 return flush_imsg(ibuf);
2047 const struct got_error *
2048 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2050 if (imsg_compose(ibuf,
2051 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2052 NULL, 0) == -1)
2053 return got_error_from_errno("imsg_compose "
2054 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2056 return flush_imsg(ibuf);
2059 const struct got_error *
2060 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2062 if (imsg_compose(ibuf,
2063 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2064 NULL, 0) == -1)
2065 return got_error_from_errno("imsg_compose "
2066 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2068 return flush_imsg(ibuf);
2072 const struct got_error *
2073 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2075 if (imsg_compose(ibuf,
2076 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2077 return got_error_from_errno("imsg_compose "
2078 "GITCONFIG_AUTHOR_NAME_REQUEST");
2080 return flush_imsg(ibuf);
2083 const struct got_error *
2084 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2086 if (imsg_compose(ibuf,
2087 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2088 return got_error_from_errno("imsg_compose "
2089 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2091 return flush_imsg(ibuf);
2094 const struct got_error *
2095 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2097 if (imsg_compose(ibuf,
2098 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2099 return got_error_from_errno("imsg_compose "
2100 "GITCONFIG_REMOTE_REQUEST");
2102 return flush_imsg(ibuf);
2105 const struct got_error *
2106 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2108 if (imsg_compose(ibuf,
2109 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2110 return got_error_from_errno("imsg_compose "
2111 "GITCONFIG_OWNER_REQUEST");
2113 return flush_imsg(ibuf);
2116 const struct got_error *
2117 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2119 const struct got_error *err = NULL;
2120 struct imsg imsg;
2121 size_t datalen;
2123 *str = NULL;
2125 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2126 if (err)
2127 return err;
2128 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2130 switch (imsg.hdr.type) {
2131 case GOT_IMSG_GITCONFIG_STR_VAL:
2132 if (datalen == 0)
2133 break;
2134 /* datalen does not include terminating \0 */
2135 *str = malloc(datalen + 1);
2136 if (*str == NULL) {
2137 err = got_error_from_errno("malloc");
2138 break;
2140 memcpy(*str, imsg.data, datalen);
2141 (*str)[datalen] = '\0';
2142 break;
2143 default:
2144 err = got_error(GOT_ERR_PRIVSEP_MSG);
2145 break;
2148 imsg_free(&imsg);
2149 return err;
2152 const struct got_error *
2153 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2155 const struct got_error *err = NULL;
2156 struct imsg imsg;
2157 size_t datalen;
2158 const size_t min_datalen =
2159 MIN(sizeof(struct got_imsg_error), sizeof(int));
2161 *val = 0;
2163 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2164 if (err)
2165 return err;
2166 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2168 switch (imsg.hdr.type) {
2169 case GOT_IMSG_GITCONFIG_INT_VAL:
2170 if (datalen != sizeof(*val)) {
2171 err = got_error(GOT_ERR_PRIVSEP_LEN);
2172 break;
2174 memcpy(val, imsg.data, sizeof(*val));
2175 break;
2176 default:
2177 err = got_error(GOT_ERR_PRIVSEP_MSG);
2178 break;
2181 imsg_free(&imsg);
2182 return err;
2185 static void
2186 free_remote_data(struct got_remote_repo *remote)
2188 int i;
2190 free(remote->name);
2191 free(remote->fetch_url);
2192 free(remote->send_url);
2193 for (i = 0; i < remote->nfetch_branches; i++)
2194 free(remote->fetch_branches[i]);
2195 free(remote->fetch_branches);
2196 for (i = 0; i < remote->nsend_branches; i++)
2197 free(remote->send_branches[i]);
2198 free(remote->send_branches);
2199 for (i = 0; i < remote->nfetch_refs; i++)
2200 free(remote->fetch_refs[i]);
2201 free(remote->fetch_refs);
2204 const struct got_error *
2205 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2206 int *nremotes, struct imsgbuf *ibuf)
2208 const struct got_error *err = NULL;
2209 struct imsg imsg;
2210 size_t datalen;
2211 struct got_imsg_remotes iremotes;
2212 struct got_imsg_remote iremote;
2214 *remotes = NULL;
2215 *nremotes = 0;
2216 iremotes.nremotes = 0;
2218 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2219 if (err)
2220 return err;
2221 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2223 switch (imsg.hdr.type) {
2224 case GOT_IMSG_GITCONFIG_REMOTES:
2225 if (datalen != sizeof(iremotes)) {
2226 err = got_error(GOT_ERR_PRIVSEP_LEN);
2227 break;
2229 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2230 if (iremotes.nremotes == 0) {
2231 imsg_free(&imsg);
2232 return NULL;
2234 break;
2235 default:
2236 imsg_free(&imsg);
2237 return got_error(GOT_ERR_PRIVSEP_MSG);
2240 imsg_free(&imsg);
2242 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2243 if (*remotes == NULL)
2244 return got_error_from_errno("recallocarray");
2246 while (*nremotes < iremotes.nremotes) {
2247 struct got_remote_repo *remote;
2249 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2250 if (err)
2251 break;
2252 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2254 switch (imsg.hdr.type) {
2255 case GOT_IMSG_GITCONFIG_REMOTE:
2256 remote = &(*remotes)[*nremotes];
2257 memset(remote, 0, sizeof(*remote));
2258 if (datalen < sizeof(iremote)) {
2259 err = got_error(GOT_ERR_PRIVSEP_LEN);
2260 break;
2262 memcpy(&iremote, imsg.data, sizeof(iremote));
2263 if (iremote.name_len == 0 ||
2264 iremote.fetch_url_len == 0 ||
2265 iremote.send_url_len == 0 ||
2266 (sizeof(iremote) + iremote.name_len +
2267 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2268 err = got_error(GOT_ERR_PRIVSEP_LEN);
2269 break;
2271 remote->name = strndup(imsg.data + sizeof(iremote),
2272 iremote.name_len);
2273 if (remote->name == NULL) {
2274 err = got_error_from_errno("strndup");
2275 break;
2277 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2278 iremote.name_len, iremote.fetch_url_len);
2279 if (remote->fetch_url == NULL) {
2280 err = got_error_from_errno("strndup");
2281 free_remote_data(remote);
2282 break;
2284 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2285 iremote.name_len + iremote.fetch_url_len,
2286 iremote.send_url_len);
2287 if (remote->send_url == NULL) {
2288 err = got_error_from_errno("strndup");
2289 free_remote_data(remote);
2290 break;
2292 remote->mirror_references = iremote.mirror_references;
2293 remote->fetch_all_branches = iremote.fetch_all_branches;
2294 remote->nfetch_branches = 0;
2295 remote->fetch_branches = NULL;
2296 remote->nsend_branches = 0;
2297 remote->send_branches = NULL;
2298 remote->nfetch_refs = 0;
2299 remote->fetch_refs = NULL;
2300 (*nremotes)++;
2301 break;
2302 default:
2303 err = got_error(GOT_ERR_PRIVSEP_MSG);
2304 break;
2307 imsg_free(&imsg);
2308 if (err)
2309 break;
2312 if (err) {
2313 int i;
2314 for (i = 0; i < *nremotes; i++)
2315 free_remote_data(&(*remotes)[i]);
2316 free(*remotes);
2317 *remotes = NULL;
2318 *nremotes = 0;
2320 return err;
2323 const struct got_error *
2324 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2326 const struct got_error *err = NULL;
2328 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2329 NULL, 0) == -1) {
2330 err = got_error_from_errno("imsg_compose "
2331 "GOTCONFIG_PARSE_REQUEST");
2332 close(fd);
2333 return err;
2336 return flush_imsg(ibuf);
2339 const struct got_error *
2340 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2342 if (imsg_compose(ibuf,
2343 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2344 return got_error_from_errno("imsg_compose "
2345 "GOTCONFIG_AUTHOR_REQUEST");
2347 return flush_imsg(ibuf);
2350 const struct got_error *
2351 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2353 if (imsg_compose(ibuf,
2354 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2355 return got_error_from_errno("imsg_compose "
2356 "GOTCONFIG_REMOTE_REQUEST");
2358 return flush_imsg(ibuf);
2361 const struct got_error *
2362 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2364 const struct got_error *err = NULL;
2365 struct imsg imsg;
2366 size_t datalen;
2368 *str = NULL;
2370 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2371 if (err)
2372 return err;
2373 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2375 switch (imsg.hdr.type) {
2376 case GOT_IMSG_ERROR:
2377 err = recv_imsg_error(&imsg, datalen);
2378 break;
2379 case GOT_IMSG_GOTCONFIG_STR_VAL:
2380 if (datalen == 0)
2381 break;
2382 /* datalen does not include terminating \0 */
2383 *str = malloc(datalen + 1);
2384 if (*str == NULL) {
2385 err = got_error_from_errno("malloc");
2386 break;
2388 memcpy(*str, imsg.data, datalen);
2389 (*str)[datalen] = '\0';
2390 break;
2391 default:
2392 err = got_error(GOT_ERR_PRIVSEP_MSG);
2393 break;
2396 imsg_free(&imsg);
2397 return err;
2400 const struct got_error *
2401 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2402 int *nremotes, struct imsgbuf *ibuf)
2404 const struct got_error *err = NULL;
2405 struct imsg imsg;
2406 size_t datalen;
2407 struct got_imsg_remotes iremotes;
2408 struct got_imsg_remote iremote;
2409 const size_t min_datalen =
2410 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2412 *remotes = NULL;
2413 *nremotes = 0;
2414 iremotes.nremotes = 0;
2416 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2417 if (err)
2418 return err;
2419 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2421 switch (imsg.hdr.type) {
2422 case GOT_IMSG_ERROR:
2423 err = recv_imsg_error(&imsg, datalen);
2424 break;
2425 case GOT_IMSG_GOTCONFIG_REMOTES:
2426 if (datalen != sizeof(iremotes)) {
2427 err = got_error(GOT_ERR_PRIVSEP_LEN);
2428 break;
2430 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2431 if (iremotes.nremotes < 0) {
2432 err = got_error(GOT_ERR_PRIVSEP_LEN);
2433 break;
2435 if (iremotes.nremotes == 0) {
2436 imsg_free(&imsg);
2437 return NULL;
2439 break;
2440 default:
2441 imsg_free(&imsg);
2442 return got_error(GOT_ERR_PRIVSEP_MSG);
2445 imsg_free(&imsg);
2447 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2448 if (*remotes == NULL)
2449 return got_error_from_errno("recallocarray");
2451 while (*nremotes < iremotes.nremotes) {
2452 struct got_remote_repo *remote;
2453 const size_t min_datalen =
2454 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2455 int i;
2457 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2458 if (err)
2459 break;
2460 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2462 switch (imsg.hdr.type) {
2463 case GOT_IMSG_ERROR:
2464 err = recv_imsg_error(&imsg, datalen);
2465 break;
2466 case GOT_IMSG_GOTCONFIG_REMOTE:
2467 remote = &(*remotes)[*nremotes];
2468 memset(remote, 0, sizeof(*remote));
2469 if (datalen < sizeof(iremote)) {
2470 err = got_error(GOT_ERR_PRIVSEP_LEN);
2471 break;
2473 memcpy(&iremote, imsg.data, sizeof(iremote));
2474 if (iremote.name_len == 0 ||
2475 (iremote.fetch_url_len == 0 &&
2476 iremote.send_url_len == 0) ||
2477 (sizeof(iremote) + iremote.name_len +
2478 iremote.fetch_url_len + iremote.send_url_len) >
2479 datalen) {
2480 err = got_error(GOT_ERR_PRIVSEP_LEN);
2481 break;
2483 remote->name = strndup(imsg.data + sizeof(iremote),
2484 iremote.name_len);
2485 if (remote->name == NULL) {
2486 err = got_error_from_errno("strndup");
2487 break;
2489 remote->fetch_url = strndup(imsg.data +
2490 sizeof(iremote) + iremote.name_len,
2491 iremote.fetch_url_len);
2492 if (remote->fetch_url == NULL) {
2493 err = got_error_from_errno("strndup");
2494 free_remote_data(remote);
2495 break;
2497 remote->send_url = strndup(imsg.data +
2498 sizeof(iremote) + iremote.name_len +
2499 iremote.fetch_url_len, iremote.send_url_len);
2500 if (remote->send_url == NULL) {
2501 err = got_error_from_errno("strndup");
2502 free_remote_data(remote);
2503 break;
2505 remote->mirror_references = iremote.mirror_references;
2506 remote->fetch_all_branches = iremote.fetch_all_branches;
2507 if (iremote.nfetch_branches > 0) {
2508 remote->fetch_branches = recallocarray(NULL, 0,
2509 iremote.nfetch_branches, sizeof(char *));
2510 if (remote->fetch_branches == NULL) {
2511 err = got_error_from_errno("calloc");
2512 free_remote_data(remote);
2513 break;
2516 remote->nfetch_branches = 0;
2517 for (i = 0; i < iremote.nfetch_branches; i++) {
2518 char *branch;
2519 err = got_privsep_recv_gotconfig_str(&branch,
2520 ibuf);
2521 if (err) {
2522 free_remote_data(remote);
2523 goto done;
2525 remote->fetch_branches[i] = branch;
2526 remote->nfetch_branches++;
2528 if (iremote.nsend_branches > 0) {
2529 remote->send_branches = recallocarray(NULL, 0,
2530 iremote.nsend_branches, sizeof(char *));
2531 if (remote->send_branches == NULL) {
2532 err = got_error_from_errno("calloc");
2533 free_remote_data(remote);
2534 break;
2537 remote->nsend_branches = 0;
2538 for (i = 0; i < iremote.nsend_branches; i++) {
2539 char *branch;
2540 err = got_privsep_recv_gotconfig_str(&branch,
2541 ibuf);
2542 if (err) {
2543 free_remote_data(remote);
2544 goto done;
2546 remote->send_branches[i] = branch;
2547 remote->nsend_branches++;
2549 if (iremote.nfetch_refs > 0) {
2550 remote->fetch_refs = recallocarray(NULL, 0,
2551 iremote.nfetch_refs, sizeof(char *));
2552 if (remote->fetch_refs == NULL) {
2553 err = got_error_from_errno("calloc");
2554 free_remote_data(remote);
2555 break;
2558 remote->nfetch_refs = 0;
2559 for (i = 0; i < iremote.nfetch_refs; i++) {
2560 char *ref;
2561 err = got_privsep_recv_gotconfig_str(&ref,
2562 ibuf);
2563 if (err) {
2564 free_remote_data(remote);
2565 goto done;
2567 remote->fetch_refs[i] = ref;
2568 remote->nfetch_refs++;
2570 (*nremotes)++;
2571 break;
2572 default:
2573 err = got_error(GOT_ERR_PRIVSEP_MSG);
2574 break;
2577 imsg_free(&imsg);
2578 if (err)
2579 break;
2581 done:
2582 if (err) {
2583 int i;
2584 for (i = 0; i < *nremotes; i++)
2585 free_remote_data(&(*remotes)[i]);
2586 free(*remotes);
2587 *remotes = NULL;
2588 *nremotes = 0;
2590 return err;
2593 const struct got_error *
2594 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2595 struct got_object_id *id, int idx, const char *path)
2597 struct ibuf *wbuf;
2598 size_t path_len = strlen(path) + 1;
2600 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2601 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2602 if (wbuf == NULL)
2603 return got_error_from_errno(
2604 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2605 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2606 return got_error_from_errno("imsg_add "
2607 "COMMIT_TRAVERSAL_REQUEST");
2608 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2609 return got_error_from_errno("imsg_add "
2610 "COMMIT_TRAVERSAL_REQUEST");
2611 if (imsg_add(wbuf, path, path_len) == -1)
2612 return got_error_from_errno("imsg_add "
2613 "COMMIT_TRAVERSAL_REQUEST");
2615 wbuf->fd = -1;
2616 imsg_close(ibuf, wbuf);
2618 return flush_imsg(ibuf);
2621 const struct got_error *
2622 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2623 struct got_object_id **changed_commit_id,
2624 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2626 const struct got_error *err = NULL;
2627 struct imsg imsg;
2628 struct got_imsg_traversed_commits *icommits;
2629 size_t datalen;
2630 int i, done = 0;
2632 *changed_commit = NULL;
2633 *changed_commit_id = NULL;
2635 while (!done) {
2636 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2637 if (err)
2638 return err;
2640 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2641 switch (imsg.hdr.type) {
2642 case GOT_IMSG_TRAVERSED_COMMITS:
2643 icommits = imsg.data;
2644 if (datalen != sizeof(*icommits) +
2645 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2646 err = got_error(GOT_ERR_PRIVSEP_LEN);
2647 break;
2649 for (i = 0; i < icommits->ncommits; i++) {
2650 struct got_object_qid *qid;
2651 uint8_t *sha1 = (uint8_t *)imsg.data +
2652 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2653 err = got_object_qid_alloc_partial(&qid);
2654 if (err)
2655 break;
2656 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2657 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2659 /* The last commit may contain a change. */
2660 if (i == icommits->ncommits - 1) {
2661 *changed_commit_id =
2662 got_object_id_dup(&qid->id);
2663 if (*changed_commit_id == NULL) {
2664 err = got_error_from_errno(
2665 "got_object_id_dup");
2666 break;
2670 break;
2671 case GOT_IMSG_COMMIT:
2672 if (*changed_commit_id == NULL) {
2673 err = got_error(GOT_ERR_PRIVSEP_MSG);
2674 break;
2676 err = get_commit_from_imsg(changed_commit, &imsg,
2677 datalen, ibuf);
2678 break;
2679 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2680 done = 1;
2681 break;
2682 default:
2683 err = got_error(GOT_ERR_PRIVSEP_MSG);
2684 break;
2687 imsg_free(&imsg);
2688 if (err)
2689 break;
2692 if (err)
2693 got_object_id_queue_free(commit_ids);
2694 return err;
2697 const struct got_error *
2698 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2699 struct got_object_id *tree_id, const char *path,
2700 struct got_parsed_tree_entry *entries, int nentries)
2702 const struct got_error *err = NULL;
2703 struct ibuf *wbuf;
2704 size_t path_len = strlen(path);
2705 size_t msglen;
2707 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2708 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2709 if (wbuf == NULL)
2710 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2712 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2713 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2714 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2715 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2716 if (imsg_add(wbuf, path, path_len) == -1)
2717 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2719 wbuf->fd = -1;
2720 imsg_close(ibuf, wbuf);
2722 if (entries) {
2723 err = send_tree_entries(ibuf, entries, nentries);
2724 if (err)
2725 return err;
2728 return flush_imsg(ibuf);
2731 const struct got_error *
2732 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2734 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2735 0, 0, -1, NULL, 0) == -1)
2736 return got_error_from_errno("imsg_compose "
2737 "OBJECT_ENUMERATION_REQUEST");
2739 return flush_imsg(ibuf);
2742 const struct got_error *
2743 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2745 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2746 0, 0, -1, NULL, 0) == -1)
2747 return got_error_from_errno("imsg_compose "
2748 "OBJECT_ENUMERATION_DONE");
2750 return flush_imsg(ibuf);
2753 const struct got_error *
2754 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2756 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2757 0, 0, -1, NULL, 0) == -1)
2758 return got_error_from_errno("imsg_compose "
2759 "OBJECT_ENUMERATION_INCOMPLETE");
2761 return flush_imsg(ibuf);
2764 const struct got_error *
2765 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2766 struct got_object_id *id, time_t mtime)
2768 struct ibuf *wbuf;
2770 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2771 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2772 if (wbuf == NULL)
2773 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2775 /* Keep in sync with struct got_imsg_enumerated_commit! */
2776 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2777 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2778 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2779 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2781 wbuf->fd = -1;
2782 imsg_close(ibuf, wbuf);
2783 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2784 return NULL;
2787 const struct got_error *
2788 got_privsep_recv_enumerated_objects(int *found_all_objects,
2789 struct imsgbuf *ibuf,
2790 got_object_enumerate_commit_cb cb_commit,
2791 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2792 struct got_repository *repo)
2794 const struct got_error *err = NULL;
2795 struct imsg imsg;
2796 struct got_imsg_enumerated_commit *icommit = NULL;
2797 struct got_object_id commit_id;
2798 int have_commit = 0;
2799 time_t mtime = 0;
2800 struct got_tree_object tree;
2801 struct got_imsg_enumerated_tree *itree;
2802 struct got_object_id tree_id;
2803 char *path = NULL, *canon_path = NULL;
2804 size_t datalen, path_len;
2805 int nentries = -1;
2806 int done = 0;
2808 *found_all_objects = 0;
2809 memset(&tree, 0, sizeof(tree));
2811 while (!done) {
2812 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2813 if (err)
2814 break;
2816 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2817 switch (imsg.hdr.type) {
2818 case GOT_IMSG_ENUMERATED_COMMIT:
2819 if (have_commit && nentries != -1) {
2820 err = got_error(GOT_ERR_PRIVSEP_MSG);
2821 break;
2823 if (datalen != sizeof(*icommit)) {
2824 err = got_error(GOT_ERR_PRIVSEP_LEN);
2825 break;
2827 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2828 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2829 mtime = icommit->mtime;
2830 have_commit = 1;
2831 break;
2832 case GOT_IMSG_ENUMERATED_TREE:
2833 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2834 if (!have_commit) {
2835 err = got_error(GOT_ERR_PRIVSEP_MSG);
2836 break;
2838 if (datalen < sizeof(*itree)) {
2839 err = got_error(GOT_ERR_PRIVSEP_LEN);
2840 break;
2842 itree = imsg.data;
2843 path_len = datalen - sizeof(*itree);
2844 if (path_len == 0) {
2845 err = got_error(GOT_ERR_PRIVSEP_LEN);
2846 break;
2848 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2849 free(path);
2850 path = malloc(path_len + 1);
2851 if (path == NULL) {
2852 err = got_error_from_errno("malloc");
2853 break;
2855 free(canon_path);
2856 canon_path = malloc(path_len + 1);
2857 if (canon_path == NULL) {
2858 err = got_error_from_errno("malloc");
2859 break;
2861 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2862 path_len);
2863 path[path_len] = '\0';
2864 if (!got_path_is_absolute(path)) {
2865 err = got_error(GOT_ERR_BAD_PATH);
2866 break;
2868 if (got_path_is_root_dir(path)) {
2869 /* XXX check what got_canonpath() does wrong */
2870 canon_path[0] = '/';
2871 canon_path[1] = '\0';
2872 } else {
2873 err = got_canonpath(path, canon_path,
2874 path_len + 1);
2875 if (err)
2876 break;
2878 if (strcmp(path, canon_path) != 0) {
2879 err = got_error(GOT_ERR_BAD_PATH);
2880 break;
2882 if (nentries != -1) {
2883 err = got_error(GOT_ERR_PRIVSEP_MSG);
2884 break;
2886 if (itree->nentries < -1) {
2887 err = got_error(GOT_ERR_PRIVSEP_MSG);
2888 break;
2890 if (itree->nentries == -1) {
2891 /* Tree was not found in pack file. */
2892 done = 1;
2893 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2894 path, repo);
2895 break;
2897 if (itree->nentries > INT_MAX) {
2898 err = got_error(GOT_ERR_PRIVSEP_LEN);
2899 break;
2901 tree.entries = calloc(itree->nentries,
2902 sizeof(struct got_tree_entry));
2903 if (tree.entries == NULL) {
2904 err = got_error_from_errno("calloc");
2905 break;
2907 if (itree->nentries == 0) {
2908 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2909 path, repo);
2910 if (err)
2911 break;
2913 /* Prepare for next tree. */
2914 free(tree.entries);
2915 memset(&tree, 0, sizeof(tree));
2916 nentries = -1;
2917 } else {
2918 tree.nentries = itree->nentries;
2919 nentries = 0;
2921 break;
2922 case GOT_IMSG_TREE_ENTRIES:
2923 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2924 if (nentries <= -1) {
2925 err = got_error(GOT_ERR_PRIVSEP_MSG);
2926 break;
2928 err = recv_tree_entries(imsg.data, datalen,
2929 &tree, &nentries);
2930 if (err)
2931 break;
2932 if (tree.nentries == nentries) {
2933 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2934 path, repo);
2935 if (err)
2936 break;
2938 /* Prepare for next tree. */
2939 free(tree.entries);
2940 memset(&tree, 0, sizeof(tree));
2941 nentries = -1;
2943 break;
2944 case GOT_IMSG_TREE_ENUMERATION_DONE:
2945 /* All trees have been found and traversed. */
2946 if (!have_commit || path == NULL || nentries != -1) {
2947 err = got_error(GOT_ERR_PRIVSEP_MSG);
2948 break;
2950 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2951 if (err)
2952 break;
2953 have_commit = 0;
2954 break;
2955 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2956 *found_all_objects = 1;
2957 done = 1;
2958 break;
2959 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
2960 done = 1;
2961 break;
2962 default:
2963 err = got_error(GOT_ERR_PRIVSEP_MSG);
2964 break;
2967 imsg_free(&imsg);
2968 if (err)
2969 break;
2972 free(path);
2973 free(canon_path);
2974 free(tree.entries);
2975 return err;
2978 const struct got_error *
2979 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2980 struct got_object_id *id)
2982 struct got_imsg_raw_delta_request dreq;
2984 dreq.idx = idx;
2985 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2987 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2988 &dreq, sizeof(dreq)) == -1)
2989 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2991 return flush_imsg(ibuf);
2994 const struct got_error *
2995 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2997 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
3000 const struct got_error *
3001 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3002 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3003 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3005 struct got_imsg_raw_delta idelta;
3006 int ret;
3008 idelta.base_size = base_size;
3009 idelta.result_size = result_size;
3010 idelta.delta_size = delta_size;
3011 idelta.delta_compressed_size = delta_compressed_size;
3012 idelta.delta_offset = delta_offset;
3013 idelta.delta_out_offset = delta_out_offset;
3014 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
3016 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3017 &idelta, sizeof(idelta));
3018 if (ret == -1)
3019 return got_error_from_errno("imsg_compose RAW_DELTA");
3021 return flush_imsg(ibuf);
3024 const struct got_error *
3025 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3026 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3027 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
3029 const struct got_error *err = NULL;
3030 struct imsg imsg;
3031 struct got_imsg_raw_delta *delta;
3032 size_t datalen;
3034 *base_size = 0;
3035 *result_size = 0;
3036 *delta_size = 0;
3037 *delta_compressed_size = 0;
3038 *delta_offset = 0;
3039 *delta_out_offset = 0;
3040 *base_id = NULL;
3042 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3043 if (err)
3044 return err;
3046 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3048 switch (imsg.hdr.type) {
3049 case GOT_IMSG_RAW_DELTA:
3050 if (datalen != sizeof(*delta)) {
3051 err = got_error(GOT_ERR_PRIVSEP_LEN);
3052 break;
3054 delta = imsg.data;
3055 *base_size = delta->base_size;
3056 *result_size = delta->result_size;
3057 *delta_size = delta->delta_size;
3058 *delta_compressed_size = delta->delta_compressed_size;
3059 *delta_offset = delta->delta_offset;
3060 *delta_out_offset = delta->delta_out_offset;
3061 *base_id = calloc(1, sizeof(**base_id));
3062 if (*base_id == NULL) {
3063 err = got_error_from_errno("malloc");
3064 break;
3066 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3067 break;
3068 default:
3069 err = got_error(GOT_ERR_PRIVSEP_MSG);
3070 break;
3073 imsg_free(&imsg);
3075 if (err) {
3076 free(*base_id);
3077 *base_id = NULL;
3079 return err;
3082 static const struct got_error *
3083 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3085 const struct got_error *err = NULL;
3086 struct got_imsg_object_idlist idlist;
3087 struct ibuf *wbuf;
3088 size_t i;
3090 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3091 return got_error(GOT_ERR_NO_SPACE);
3093 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3094 sizeof(idlist) + nids * sizeof(**ids));
3095 if (wbuf == NULL) {
3096 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3097 return err;
3100 idlist.nids = nids;
3101 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3102 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3104 for (i = 0; i < nids; i++) {
3105 struct got_object_id *id = ids[i];
3106 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3107 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3110 wbuf->fd = -1;
3111 imsg_close(ibuf, wbuf);
3113 return flush_imsg(ibuf);
3116 const struct got_error *
3117 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3118 struct got_object_id **ids, size_t nids)
3120 const struct got_error *err = NULL;
3121 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3122 int i, queued = 0;
3124 for (i = 0; i < nids; i++) {
3125 idlist[i % nitems(idlist)] = ids[i];
3126 queued++;
3127 if (queued >= nitems(idlist)) {
3128 err = send_idlist(ibuf, idlist, queued);
3129 if (err)
3130 return err;
3131 queued = 0;
3135 if (queued > 0) {
3136 err = send_idlist(ibuf, idlist, queued);
3137 if (err)
3138 return err;
3141 return NULL;
3144 const struct got_error *
3145 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3147 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3148 == -1)
3149 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3151 return flush_imsg(ibuf);
3154 const struct got_error *
3155 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3156 size_t *nids, struct imsgbuf *ibuf)
3158 const struct got_error *err = NULL;
3159 struct imsg imsg;
3160 struct got_imsg_object_idlist *idlist;
3161 size_t datalen;
3163 *ids = NULL;
3164 *done = 0;
3165 *nids = 0;
3167 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3168 if (err)
3169 return err;
3171 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3172 switch (imsg.hdr.type) {
3173 case GOT_IMSG_OBJ_ID_LIST:
3174 if (datalen < sizeof(*idlist)) {
3175 err = got_error(GOT_ERR_PRIVSEP_LEN);
3176 break;
3178 idlist = imsg.data;
3179 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3180 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3181 err = got_error(GOT_ERR_PRIVSEP_LEN);
3182 break;
3184 *nids = idlist->nids;
3185 *ids = calloc(*nids, sizeof(**ids));
3186 if (*ids == NULL) {
3187 err = got_error_from_errno("calloc");
3188 break;
3190 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3191 *nids * sizeof(**ids));
3192 break;
3193 case GOT_IMSG_OBJ_ID_LIST_DONE:
3194 *done = 1;
3195 break;
3196 default:
3197 err = got_error(GOT_ERR_PRIVSEP_MSG);
3198 break;
3201 imsg_free(&imsg);
3203 return err;
3206 const struct got_error *
3207 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3209 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3210 == -1)
3211 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3213 return flush_imsg(ibuf);
3216 const struct got_error *
3217 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3218 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3220 const struct got_error *err = NULL;
3221 struct ibuf *wbuf;
3222 struct got_imsg_reused_deltas ideltas;
3223 size_t i;
3225 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3226 return got_error(GOT_ERR_NO_SPACE);
3228 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3229 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3230 if (wbuf == NULL) {
3231 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3232 return err;
3235 ideltas.ndeltas = ndeltas;
3236 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3237 return got_error_from_errno("imsg_add REUSED_DELTAS");
3239 for (i = 0; i < ndeltas; i++) {
3240 struct got_imsg_reused_delta *delta = &deltas[i];
3241 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3242 return got_error_from_errno("imsg_add REUSED_DELTAS");
3245 wbuf->fd = -1;
3246 imsg_close(ibuf, wbuf);
3248 return flush_imsg(ibuf);
3251 const struct got_error *
3252 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3254 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3255 == -1)
3256 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3258 return flush_imsg(ibuf);
3261 const struct got_error *
3262 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3263 size_t *ndeltas, struct imsgbuf *ibuf)
3265 const struct got_error *err = NULL;
3266 struct imsg imsg;
3267 struct got_imsg_reused_deltas *ideltas;
3268 size_t datalen;
3270 *done = 0;
3271 *ndeltas = 0;
3273 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3274 if (err)
3275 return err;
3277 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3278 switch (imsg.hdr.type) {
3279 case GOT_IMSG_REUSED_DELTAS:
3280 if (datalen < sizeof(*ideltas)) {
3281 err = got_error(GOT_ERR_PRIVSEP_LEN);
3282 break;
3284 ideltas = imsg.data;
3285 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3286 ideltas->ndeltas * sizeof(*deltas) >
3287 datalen - sizeof(*ideltas)) {
3288 err = got_error(GOT_ERR_PRIVSEP_LEN);
3289 break;
3291 *ndeltas = ideltas->ndeltas;
3292 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3293 *ndeltas * sizeof(*deltas));
3294 break;
3295 case GOT_IMSG_DELTA_REUSE_DONE:
3296 *done = 1;
3297 break;
3298 default:
3299 err = got_error(GOT_ERR_PRIVSEP_MSG);
3300 break;
3303 imsg_free(&imsg);
3305 return err;
3308 const struct got_error *
3309 got_privsep_unveil_exec_helpers(void)
3311 const char *helpers[] = {
3312 GOT_PATH_PROG_READ_PACK,
3313 GOT_PATH_PROG_READ_OBJECT,
3314 GOT_PATH_PROG_READ_COMMIT,
3315 GOT_PATH_PROG_READ_TREE,
3316 GOT_PATH_PROG_READ_BLOB,
3317 GOT_PATH_PROG_READ_TAG,
3318 GOT_PATH_PROG_READ_GITCONFIG,
3319 GOT_PATH_PROG_READ_GOTCONFIG,
3320 GOT_PATH_PROG_READ_PATCH,
3321 GOT_PATH_PROG_FETCH_PACK,
3322 GOT_PATH_PROG_INDEX_PACK,
3323 GOT_PATH_PROG_SEND_PACK,
3325 size_t i;
3327 for (i = 0; i < nitems(helpers); i++) {
3328 if (unveil(helpers[i], "x") == 0)
3329 continue;
3330 return got_error_from_errno2("unveil", helpers[i]);
3333 return NULL;
3336 void
3337 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3339 if (close(imsg_fds[0]) == -1) {
3340 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3341 _exit(1);
3344 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3345 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3346 _exit(1);
3349 closefrom(GOT_IMSG_FD_CHILD + 1);
3351 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3352 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3353 strerror(errno));
3354 _exit(1);