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/uio.h>
20 #include <sys/wait.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <stdint.h>
30 #include <poll.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <time.h>
35 #include "got_compat.h"
37 #include "got_object.h"
38 #include "got_error.h"
39 #include "got_path.h"
40 #include "got_repository.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
50 #ifndef MIN
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 #endif
54 #ifndef nitems
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 #endif
58 static const struct got_error *
59 poll_fd(int fd, int events, int timeout)
60 {
61 struct pollfd pfd[1];
62 struct timespec ts;
63 sigset_t sigset;
64 int n;
66 pfd[0].fd = fd;
67 pfd[0].events = events;
69 ts.tv_sec = timeout;
70 ts.tv_nsec = 0;
72 if (sigemptyset(&sigset) == -1)
73 return got_error_from_errno("sigemptyset");
74 if (sigaddset(&sigset, SIGWINCH) == -1)
75 return got_error_from_errno("sigaddset");
77 n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
78 if (n == -1)
79 return got_error_from_errno("ppoll");
80 if (n == 0)
81 return got_error(GOT_ERR_TIMEOUT);
82 if (pfd[0].revents & (POLLERR | POLLNVAL))
83 return got_error_from_errno("poll error");
84 if (pfd[0].revents & (events | POLLHUP))
85 return NULL;
87 return got_error(GOT_ERR_INTERRUPT);
88 }
90 static const struct got_error *
91 read_imsg(struct imsgbuf *ibuf)
92 {
93 const struct got_error *err;
94 size_t n;
96 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
97 if (err)
98 return err;
100 n = imsg_read(ibuf);
101 if (n == -1) {
102 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
103 return got_error(GOT_ERR_PRIVSEP_NO_FD);
104 return got_error(GOT_ERR_PRIVSEP_READ);
106 if (n == 0)
107 return got_error(GOT_ERR_PRIVSEP_PIPE);
109 return NULL;
112 const struct got_error *
113 got_privsep_wait_for_child(pid_t pid)
115 int child_status;
117 if (waitpid(pid, &child_status, 0) == -1)
118 return got_error_from_errno("waitpid");
120 if (!WIFEXITED(child_status))
121 return got_error(GOT_ERR_PRIVSEP_DIED);
123 if (WEXITSTATUS(child_status) != 0)
124 return got_error(GOT_ERR_PRIVSEP_EXIT);
126 return NULL;
129 static const struct got_error *
130 recv_imsg_error(struct imsg *imsg, size_t datalen)
132 struct got_imsg_error *ierr;
134 if (datalen != sizeof(*ierr))
135 return got_error(GOT_ERR_PRIVSEP_LEN);
137 ierr = imsg->data;
138 if (ierr->code == GOT_ERR_ERRNO) {
139 static struct got_error serr;
140 serr.code = GOT_ERR_ERRNO;
141 serr.msg = strerror(ierr->errno_code);
142 return &serr;
145 return got_error(ierr->code);
148 const struct got_error *
149 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
150 size_t min_datalen)
152 const struct got_error *err;
153 ssize_t n;
155 n = imsg_get(ibuf, imsg);
156 if (n == -1)
157 return got_error_from_errno("imsg_get");
159 while (n == 0) {
160 err = read_imsg(ibuf);
161 if (err)
162 return err;
163 n = imsg_get(ibuf, imsg);
164 if (n == -1)
165 return got_error_from_errno("imsg_get");
168 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
169 return got_error(GOT_ERR_PRIVSEP_LEN);
171 if (imsg->hdr.type == GOT_IMSG_ERROR) {
172 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
173 return recv_imsg_error(imsg, datalen);
176 return NULL;
179 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
180 void
181 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
183 const struct got_error *poll_err;
184 struct got_imsg_error ierr;
185 int ret;
187 ierr.code = err->code;
188 if (err->code == GOT_ERR_ERRNO)
189 ierr.errno_code = errno;
190 else
191 ierr.errno_code = 0;
192 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
193 if (ret == -1) {
194 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
195 getprogname(), err->code, err->msg, strerror(errno));
196 return;
199 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
200 if (poll_err) {
201 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
202 getprogname(), err->code, err->msg, poll_err->msg);
203 return;
206 ret = imsg_flush(ibuf);
207 if (ret == -1) {
208 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
209 getprogname(), err->code, err->msg, strerror(errno));
210 imsg_clear(ibuf);
211 return;
215 static const struct got_error *
216 flush_imsg(struct imsgbuf *ibuf)
218 const struct got_error *err;
220 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
221 if (err)
222 return err;
224 if (imsg_flush(ibuf) == -1) {
225 imsg_clear(ibuf);
226 return got_error_from_errno("imsg_flush");
229 return NULL;
232 const struct got_error *
233 got_privsep_flush_imsg(struct imsgbuf *ibuf)
235 return flush_imsg(ibuf);
238 const struct got_error *
239 got_privsep_send_stop(int fd)
241 const struct got_error *err = NULL;
242 struct imsgbuf ibuf;
244 imsg_init(&ibuf, fd);
246 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
247 return got_error_from_errno("imsg_compose STOP");
249 err = flush_imsg(&ibuf);
250 return err;
253 const struct got_error *
254 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
255 struct got_object_id *id)
257 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
258 id, sizeof(*id)) == -1)
259 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
261 return flush_imsg(ibuf);
264 const struct got_error *
265 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
266 struct got_object_id *id)
268 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
269 id, sizeof(*id)) == -1)
270 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
272 return flush_imsg(ibuf);
275 const struct got_error *
276 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
278 const struct got_error *err = NULL;
280 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
281 == -1) {
282 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
283 close(outfd);
284 return err;
287 return flush_imsg(ibuf);
290 const struct got_error *
291 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
292 uint8_t *data)
294 const struct got_error *err = NULL;
295 struct got_imsg_raw_obj iobj;
296 size_t len = sizeof(iobj);
297 struct ibuf *wbuf;
299 iobj.hdrlen = hdrlen;
300 iobj.size = size;
302 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
303 len += (size_t)size + hdrlen;
305 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
306 if (wbuf == NULL) {
307 err = got_error_from_errno("imsg_create RAW_OBJECT");
308 return err;
311 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
312 return got_error_from_errno("imsg_add RAW_OBJECT");
314 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
315 if (imsg_add(wbuf, data, size + hdrlen) == -1)
316 return got_error_from_errno("imsg_add RAW_OBJECT");
319 wbuf->fd = -1;
320 imsg_close(ibuf, wbuf);
322 return flush_imsg(ibuf);
325 const struct got_error *
326 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
327 struct imsgbuf *ibuf)
329 const struct got_error *err = NULL;
330 struct imsg imsg;
331 struct got_imsg_raw_obj *iobj;
332 size_t datalen;
334 *outbuf = NULL;
336 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
337 if (err)
338 return err;
340 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
342 switch (imsg.hdr.type) {
343 case GOT_IMSG_RAW_OBJECT:
344 if (datalen < sizeof(*iobj)) {
345 err = got_error(GOT_ERR_PRIVSEP_LEN);
346 break;
348 iobj = imsg.data;
349 *size = iobj->size;
350 *hdrlen = iobj->hdrlen;
352 if (datalen == sizeof(*iobj)) {
353 /* Data has been written to file descriptor. */
354 break;
357 if (*size < 0 ||
358 *size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
359 err = got_error(GOT_ERR_PRIVSEP_LEN);
360 break;
363 *outbuf = malloc(*size + *hdrlen);
364 if (*outbuf == NULL) {
365 err = got_error_from_errno("malloc");
366 break;
368 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
369 break;
370 default:
371 err = got_error(GOT_ERR_PRIVSEP_MSG);
372 break;
375 imsg_free(&imsg);
377 return err;
380 const struct got_error *
381 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
382 struct got_object_id *id, int pack_idx)
384 const struct got_error *err = NULL;
385 struct got_imsg_packed_object iobj;
386 void *data;
387 size_t len;
389 if (pack_idx != -1) { /* commit is packed */
390 iobj.idx = pack_idx;
391 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
392 data = &iobj;
393 len = sizeof(iobj);
394 } else {
395 data = id;
396 len = sizeof(*id);
399 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
400 == -1) {
401 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
402 close(fd);
403 return err;
406 return flush_imsg(ibuf);
409 const struct got_error *
410 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
411 struct got_object_id *id, int pack_idx)
413 struct ibuf *wbuf;
414 size_t len;
416 if (pack_idx != -1)
417 len = sizeof(struct got_imsg_packed_object);
418 else
419 len = sizeof(*id);
421 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
422 if (wbuf == NULL)
423 return got_error_from_errno("imsg_create TREE_REQUEST");
425 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
426 return got_error_from_errno("imsg_add TREE_REQUEST");
428 if (pack_idx != -1) { /* tree is packed */
429 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
430 return got_error_from_errno("imsg_add TREE_REQUEST");
433 wbuf->fd = fd;
434 imsg_close(ibuf, wbuf);
436 return flush_imsg(ibuf);
439 const struct got_error *
440 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
441 struct got_object_id *id, int pack_idx)
443 struct got_imsg_packed_object iobj;
444 void *data;
445 size_t len;
447 if (pack_idx != -1) { /* tag is packed */
448 iobj.idx = pack_idx;
449 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
450 data = &iobj;
451 len = sizeof(iobj);
452 } else {
453 data = id;
454 len = sizeof(*id);
457 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
458 == -1)
459 return got_error_from_errno("imsg_compose TAG_REQUEST");
461 return flush_imsg(ibuf);
464 const struct got_error *
465 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
466 struct got_object_id *id, int pack_idx)
468 const struct got_error *err = NULL;
469 struct got_imsg_packed_object iobj;
470 void *data;
471 size_t len;
473 if (pack_idx != -1) { /* blob is packed */
474 iobj.idx = pack_idx;
475 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
476 data = &iobj;
477 len = sizeof(iobj);
478 } else {
479 data = id;
480 len = sizeof(*id);
483 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
484 == -1) {
485 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
486 close(infd);
487 return err;
490 return flush_imsg(ibuf);
493 const struct got_error *
494 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
496 const struct got_error *err = NULL;
498 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
499 == -1) {
500 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
501 close(outfd);
502 return err;
505 return flush_imsg(ibuf);
508 static const struct got_error *
509 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
511 const struct got_error *err = NULL;
513 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
514 err = got_error_from_errno("imsg_compose TMPFD");
515 close(fd);
516 return err;
519 return flush_imsg(ibuf);
522 const struct got_error *
523 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
525 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
528 const struct got_error *
529 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
531 struct got_imsg_object iobj;
533 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
534 iobj.type = obj->type;
535 iobj.flags = obj->flags;
536 iobj.hdrlen = obj->hdrlen;
537 iobj.size = obj->size;
538 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
539 iobj.pack_offset = obj->pack_offset;
540 iobj.pack_idx = obj->pack_idx;
543 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
544 == -1)
545 return got_error_from_errno("imsg_compose OBJECT");
547 return flush_imsg(ibuf);
550 const struct got_error *
551 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
552 struct got_pathlist_head *have_refs, int fetch_all_branches,
553 struct got_pathlist_head *wanted_branches,
554 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
556 const struct got_error *err = NULL;
557 struct ibuf *wbuf;
558 size_t len;
559 struct got_pathlist_entry *pe;
560 struct got_imsg_fetch_request fetchreq;
562 memset(&fetchreq, 0, sizeof(fetchreq));
563 fetchreq.fetch_all_branches = fetch_all_branches;
564 fetchreq.list_refs_only = list_refs_only;
565 fetchreq.verbosity = verbosity;
566 TAILQ_FOREACH(pe, have_refs, entry)
567 fetchreq.n_have_refs++;
568 TAILQ_FOREACH(pe, wanted_branches, entry)
569 fetchreq.n_wanted_branches++;
570 TAILQ_FOREACH(pe, wanted_refs, entry)
571 fetchreq.n_wanted_refs++;
572 len = sizeof(struct got_imsg_fetch_request);
573 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
574 close(fd);
575 return got_error(GOT_ERR_NO_SPACE);
578 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
579 &fetchreq, sizeof(fetchreq)) == -1)
580 return got_error_from_errno(
581 "imsg_compose FETCH_SERVER_PROGRESS");
583 err = flush_imsg(ibuf);
584 if (err) {
585 close(fd);
586 return err;
588 fd = -1;
590 TAILQ_FOREACH(pe, have_refs, entry) {
591 const char *name = pe->path;
592 size_t name_len = pe->path_len;
593 struct got_object_id *id = pe->data;
595 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
596 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
597 if (wbuf == NULL)
598 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
600 /* Keep in sync with struct got_imsg_fetch_have_ref! */
601 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
602 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
603 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
604 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
605 if (imsg_add(wbuf, name, name_len) == -1)
606 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
608 wbuf->fd = -1;
609 imsg_close(ibuf, wbuf);
610 err = flush_imsg(ibuf);
611 if (err)
612 return err;
615 TAILQ_FOREACH(pe, wanted_branches, entry) {
616 const char *name = pe->path;
617 size_t name_len = pe->path_len;
619 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
620 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
621 len);
622 if (wbuf == NULL)
623 return got_error_from_errno(
624 "imsg_create FETCH_WANTED_BRANCH");
626 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
627 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
628 return got_error_from_errno(
629 "imsg_add FETCH_WANTED_BRANCH");
630 if (imsg_add(wbuf, name, name_len) == -1)
631 return got_error_from_errno(
632 "imsg_add FETCH_WANTED_BRANCH");
634 wbuf->fd = -1;
635 imsg_close(ibuf, wbuf);
636 err = flush_imsg(ibuf);
637 if (err)
638 return err;
641 TAILQ_FOREACH(pe, wanted_refs, entry) {
642 const char *name = pe->path;
643 size_t name_len = pe->path_len;
645 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
646 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
647 len);
648 if (wbuf == NULL)
649 return got_error_from_errno(
650 "imsg_create FETCH_WANTED_REF");
652 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
653 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
654 return got_error_from_errno(
655 "imsg_add FETCH_WANTED_REF");
656 if (imsg_add(wbuf, name, name_len) == -1)
657 return got_error_from_errno(
658 "imsg_add FETCH_WANTED_REF");
660 wbuf->fd = -1;
661 imsg_close(ibuf, wbuf);
662 err = flush_imsg(ibuf);
663 if (err)
664 return err;
668 return NULL;
672 const struct got_error *
673 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
675 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
678 const struct got_error *
679 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
680 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
681 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
683 const struct got_error *err = NULL;
684 struct imsg imsg;
685 size_t datalen;
686 struct got_imsg_fetch_symrefs *isymrefs = NULL;
687 size_t n, remain;
688 off_t off;
689 int i;
691 *done = 0;
692 *id = NULL;
693 *refname = NULL;
694 *server_progress = NULL;
695 *packfile_size = 0;
696 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
698 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
699 if (err)
700 return err;
702 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
703 switch (imsg.hdr.type) {
704 case GOT_IMSG_ERROR:
705 err = recv_imsg_error(&imsg, datalen);
706 break;
707 case GOT_IMSG_FETCH_SYMREFS:
708 if (datalen < sizeof(*isymrefs)) {
709 err = got_error(GOT_ERR_PRIVSEP_LEN);
710 break;
712 if (isymrefs != NULL) {
713 err = got_error(GOT_ERR_PRIVSEP_MSG);
714 break;
716 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
717 off = sizeof(*isymrefs);
718 remain = datalen - off;
719 for (n = 0; n < isymrefs->nsymrefs; n++) {
720 struct got_imsg_fetch_symref *s;
721 char *name, *target;
722 if (remain < sizeof(struct got_imsg_fetch_symref)) {
723 err = got_error(GOT_ERR_PRIVSEP_LEN);
724 goto done;
726 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
727 off += sizeof(*s);
728 remain -= sizeof(*s);
729 if (remain < s->name_len) {
730 err = got_error(GOT_ERR_PRIVSEP_LEN);
731 goto done;
733 name = strndup(imsg.data + off, s->name_len);
734 if (name == NULL) {
735 err = got_error_from_errno("strndup");
736 goto done;
738 off += s->name_len;
739 remain -= s->name_len;
740 if (remain < s->target_len) {
741 err = got_error(GOT_ERR_PRIVSEP_LEN);
742 free(name);
743 goto done;
745 target = strndup(imsg.data + off, s->target_len);
746 if (target == NULL) {
747 err = got_error_from_errno("strndup");
748 free(name);
749 goto done;
751 off += s->target_len;
752 remain -= s->target_len;
753 err = got_pathlist_append(symrefs, name, target);
754 if (err) {
755 free(name);
756 free(target);
757 goto done;
760 break;
761 case GOT_IMSG_FETCH_REF:
762 if (datalen <= SHA1_DIGEST_LENGTH) {
763 err = got_error(GOT_ERR_PRIVSEP_MSG);
764 break;
766 *id = malloc(sizeof(**id));
767 if (*id == NULL) {
768 err = got_error_from_errno("malloc");
769 break;
771 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
772 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
773 datalen - SHA1_DIGEST_LENGTH);
774 if (*refname == NULL) {
775 err = got_error_from_errno("strndup");
776 break;
778 break;
779 case GOT_IMSG_FETCH_SERVER_PROGRESS:
780 if (datalen == 0) {
781 err = got_error(GOT_ERR_PRIVSEP_LEN);
782 break;
784 *server_progress = strndup(imsg.data, datalen);
785 if (*server_progress == NULL) {
786 err = got_error_from_errno("strndup");
787 break;
789 for (i = 0; i < datalen; i++) {
790 if (!isprint((unsigned char)(*server_progress)[i]) &&
791 !isspace((unsigned char)(*server_progress)[i])) {
792 err = got_error(GOT_ERR_PRIVSEP_MSG);
793 free(*server_progress);
794 *server_progress = NULL;
795 goto done;
798 break;
799 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
800 if (datalen < sizeof(*packfile_size)) {
801 err = got_error(GOT_ERR_PRIVSEP_MSG);
802 break;
804 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
805 break;
806 case GOT_IMSG_FETCH_DONE:
807 if (datalen != SHA1_DIGEST_LENGTH) {
808 err = got_error(GOT_ERR_PRIVSEP_MSG);
809 break;
811 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
812 *done = 1;
813 break;
814 default:
815 err = got_error(GOT_ERR_PRIVSEP_MSG);
816 break;
818 done:
819 if (err) {
820 free(*id);
821 *id = NULL;
822 free(*refname);
823 *refname = NULL;
825 imsg_free(&imsg);
826 return err;
829 static const struct got_error *
830 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
831 int delete, struct imsgbuf *ibuf)
833 size_t len;
834 struct ibuf *wbuf;
836 len = sizeof(struct got_imsg_send_ref) + name_len;
837 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
838 if (wbuf == NULL)
839 return got_error_from_errno("imsg_create SEND_REF");
841 /* Keep in sync with struct got_imsg_send_ref! */
842 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
843 return got_error_from_errno("imsg_add SEND_REF");
844 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
845 return got_error_from_errno("imsg_add SEND_REF");
846 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
847 return got_error_from_errno("imsg_add SEND_REF");
848 if (imsg_add(wbuf, name, name_len) == -1)
849 return got_error_from_errno("imsg_add SEND_REF");
851 wbuf->fd = -1;
852 imsg_close(ibuf, wbuf);
853 return flush_imsg(ibuf);
856 const struct got_error *
857 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
858 struct got_pathlist_head *have_refs,
859 struct got_pathlist_head *delete_refs,
860 int verbosity)
862 const struct got_error *err = NULL;
863 struct got_pathlist_entry *pe;
864 struct got_imsg_send_request sendreq;
865 struct got_object_id zero_id;
867 memset(&zero_id, 0, sizeof(zero_id));
868 memset(&sendreq, 0, sizeof(sendreq));
869 sendreq.verbosity = verbosity;
870 TAILQ_FOREACH(pe, have_refs, entry)
871 sendreq.nrefs++;
872 TAILQ_FOREACH(pe, delete_refs, entry)
873 sendreq.nrefs++;
874 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
875 &sendreq, sizeof(sendreq)) == -1) {
876 err = got_error_from_errno(
877 "imsg_compose FETCH_SERVER_PROGRESS");
878 goto done;
881 err = flush_imsg(ibuf);
882 if (err)
883 goto done;
884 fd = -1;
886 TAILQ_FOREACH(pe, have_refs, entry) {
887 const char *name = pe->path;
888 size_t name_len = pe->path_len;
889 struct got_object_id *id = pe->data;
890 err = send_send_ref(name, name_len, id, 0, ibuf);
891 if (err)
892 goto done;
895 TAILQ_FOREACH(pe, delete_refs, entry) {
896 const char *name = pe->path;
897 size_t name_len = pe->path_len;
898 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
899 if (err)
900 goto done;
902 done:
903 if (fd != -1 && close(fd) == -1 && err == NULL)
904 err = got_error_from_errno("close");
905 return err;
909 const struct got_error *
910 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
911 struct imsgbuf *ibuf)
913 const struct got_error *err = NULL;
914 struct imsg imsg;
915 size_t datalen;
916 int done = 0;
917 struct got_imsg_send_remote_ref iremote_ref;
918 struct got_object_id *id = NULL;
919 char *refname = NULL;
920 struct got_pathlist_entry *new;
922 while (!done) {
923 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
924 if (err)
925 return err;
926 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
927 switch (imsg.hdr.type) {
928 case GOT_IMSG_ERROR:
929 err = recv_imsg_error(&imsg, datalen);
930 goto done;
931 case GOT_IMSG_SEND_REMOTE_REF:
932 if (datalen < sizeof(iremote_ref)) {
933 err = got_error(GOT_ERR_PRIVSEP_MSG);
934 goto done;
936 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
937 if (datalen != sizeof(iremote_ref) +
938 iremote_ref.name_len) {
939 err = got_error(GOT_ERR_PRIVSEP_MSG);
940 goto done;
942 id = malloc(sizeof(*id));
943 if (id == NULL) {
944 err = got_error_from_errno("malloc");
945 goto done;
947 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
948 refname = strndup(imsg.data + sizeof(iremote_ref),
949 datalen - sizeof(iremote_ref));
950 if (refname == NULL) {
951 err = got_error_from_errno("strndup");
952 goto done;
954 err = got_pathlist_insert(&new, remote_refs,
955 refname, id);
956 if (err)
957 goto done;
958 if (new == NULL) { /* duplicate which wasn't inserted */
959 free(id);
960 free(refname);
962 id = NULL;
963 refname = NULL;
964 break;
965 case GOT_IMSG_SEND_PACK_REQUEST:
966 if (datalen != 0) {
967 err = got_error(GOT_ERR_PRIVSEP_MSG);
968 goto done;
970 /* got-send-pack is now waiting for a pack file. */
971 done = 1;
972 break;
973 default:
974 err = got_error(GOT_ERR_PRIVSEP_MSG);
975 break;
978 done:
979 free(id);
980 free(refname);
981 imsg_free(&imsg);
982 return err;
985 const struct got_error *
986 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
988 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
991 const struct got_error *
992 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
993 int *success, char **refname, struct imsgbuf *ibuf)
995 const struct got_error *err = NULL;
996 struct imsg imsg;
997 size_t datalen;
998 struct got_imsg_send_ref_status iref_status;
1000 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1001 *done = 0;
1002 *success = 0;
1003 *refname = NULL;
1005 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1006 if (err)
1007 return err;
1009 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1010 switch (imsg.hdr.type) {
1011 case GOT_IMSG_ERROR:
1012 err = recv_imsg_error(&imsg, datalen);
1013 break;
1014 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1015 if (datalen < sizeof(*bytes_sent)) {
1016 err = got_error(GOT_ERR_PRIVSEP_MSG);
1017 break;
1019 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1020 break;
1021 case GOT_IMSG_SEND_REF_STATUS:
1022 if (datalen < sizeof(iref_status)) {
1023 err = got_error(GOT_ERR_PRIVSEP_MSG);
1024 break;
1026 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1027 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1028 err = got_error(GOT_ERR_PRIVSEP_MSG);
1029 break;
1031 *success = iref_status.success;
1032 *refname = strndup(imsg.data + sizeof(iref_status),
1033 iref_status.name_len);
1034 break;
1035 case GOT_IMSG_SEND_DONE:
1036 if (datalen != 0) {
1037 err = got_error(GOT_ERR_PRIVSEP_MSG);
1038 break;
1040 *done = 1;
1041 break;
1042 default:
1043 err = got_error(GOT_ERR_PRIVSEP_MSG);
1044 break;
1047 imsg_free(&imsg);
1048 return err;
1051 const struct got_error *
1052 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1053 int fd)
1055 const struct got_error *err = NULL;
1057 /* Keep in sync with struct got_imsg_index_pack_request */
1058 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1059 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1060 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1061 close(fd);
1062 return err;
1064 return flush_imsg(ibuf);
1067 const struct got_error *
1068 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1070 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1073 const struct got_error *
1074 got_privsep_recv_index_progress(int *done, int *nobj_total,
1075 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1076 struct imsgbuf *ibuf)
1078 const struct got_error *err = NULL;
1079 struct imsg imsg;
1080 struct got_imsg_index_pack_progress *iprogress;
1081 size_t datalen;
1083 *done = 0;
1084 *nobj_total = 0;
1085 *nobj_indexed = 0;
1086 *nobj_resolved = 0;
1088 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1089 if (err)
1090 return err;
1092 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1093 switch (imsg.hdr.type) {
1094 case GOT_IMSG_ERROR:
1095 err = recv_imsg_error(&imsg, datalen);
1096 break;
1097 case GOT_IMSG_IDXPACK_PROGRESS:
1098 if (datalen < sizeof(*iprogress)) {
1099 err = got_error(GOT_ERR_PRIVSEP_LEN);
1100 break;
1102 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1103 if (iprogress->nobj_total < 0 || iprogress->nobj_indexed < 0 ||
1104 iprogress->nobj_loose < 0 || iprogress->nobj_resolved < 0) {
1105 err = got_error(GOT_ERR_RANGE);
1106 break;
1108 *nobj_total = iprogress->nobj_total;
1109 *nobj_indexed = iprogress->nobj_indexed;
1110 *nobj_loose = iprogress->nobj_loose;
1111 *nobj_resolved = iprogress->nobj_resolved;
1112 break;
1113 case GOT_IMSG_IDXPACK_DONE:
1114 if (datalen != 0) {
1115 err = got_error(GOT_ERR_PRIVSEP_LEN);
1116 break;
1118 *done = 1;
1119 break;
1120 default:
1121 err = got_error(GOT_ERR_PRIVSEP_MSG);
1122 break;
1125 imsg_free(&imsg);
1126 return err;
1129 const struct got_error *
1130 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1131 struct imsgbuf *ibuf)
1133 struct got_imsg_object *iobj;
1134 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1136 if (datalen != sizeof(*iobj))
1137 return got_error(GOT_ERR_PRIVSEP_LEN);
1138 iobj = imsg->data;
1140 if (iobj->pack_offset < 0)
1141 return got_error(GOT_ERR_PACK_OFFSET);
1143 *obj = calloc(1, sizeof(**obj));
1144 if (*obj == NULL)
1145 return got_error_from_errno("calloc");
1147 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1148 (*obj)->type = iobj->type;
1149 (*obj)->flags = iobj->flags;
1150 (*obj)->hdrlen = iobj->hdrlen;
1151 (*obj)->size = iobj->size;
1152 /* path_packfile is handled by caller */
1153 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1154 (*obj)->pack_offset = iobj->pack_offset;
1155 (*obj)->pack_idx = iobj->pack_idx;
1157 STAILQ_INIT(&(*obj)->deltas.entries);
1158 return NULL;
1161 const struct got_error *
1162 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1164 const struct got_error *err = NULL;
1165 struct imsg imsg;
1166 const size_t min_datalen =
1167 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1169 *obj = NULL;
1171 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1172 if (err)
1173 return err;
1175 switch (imsg.hdr.type) {
1176 case GOT_IMSG_OBJECT:
1177 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1178 break;
1179 default:
1180 err = got_error(GOT_ERR_PRIVSEP_MSG);
1181 break;
1184 imsg_free(&imsg);
1186 return err;
1189 static const struct got_error *
1190 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1191 size_t logmsg_len)
1193 const struct got_error *err = NULL;
1194 size_t offset, remain;
1196 offset = 0;
1197 remain = logmsg_len;
1198 while (remain > 0) {
1199 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1201 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1202 commit->logmsg + offset, n) == -1) {
1203 err = got_error_from_errno("imsg_compose "
1204 "COMMIT_LOGMSG");
1205 break;
1208 err = flush_imsg(ibuf);
1209 if (err)
1210 break;
1212 offset += n;
1213 remain -= n;
1216 return err;
1219 const struct got_error *
1220 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1222 const struct got_error *err = NULL;
1223 struct got_imsg_commit_object *icommit;
1224 uint8_t *buf;
1225 size_t len, total;
1226 struct got_object_qid *qid;
1227 size_t author_len = strlen(commit->author);
1228 size_t committer_len = strlen(commit->committer);
1229 size_t logmsg_len = strlen(commit->logmsg);
1231 total = sizeof(*icommit) + author_len + committer_len +
1232 commit->nparents * SHA1_DIGEST_LENGTH;
1234 buf = malloc(total);
1235 if (buf == NULL)
1236 return got_error_from_errno("malloc");
1238 icommit = (struct got_imsg_commit_object *)buf;
1239 memcpy(icommit->tree_id, commit->tree_id->sha1,
1240 sizeof(icommit->tree_id));
1241 icommit->author_len = author_len;
1242 icommit->author_time = commit->author_time;
1243 icommit->author_gmtoff = commit->author_gmtoff;
1244 icommit->committer_len = committer_len;
1245 icommit->committer_time = commit->committer_time;
1246 icommit->committer_gmtoff = commit->committer_gmtoff;
1247 icommit->logmsg_len = logmsg_len;
1248 icommit->nparents = commit->nparents;
1250 len = sizeof(*icommit);
1251 memcpy(buf + len, commit->author, author_len);
1252 len += author_len;
1253 memcpy(buf + len, commit->committer, committer_len);
1254 len += committer_len;
1255 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1256 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1257 len += SHA1_DIGEST_LENGTH;
1260 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1261 err = got_error_from_errno("imsg_compose COMMIT");
1262 goto done;
1265 if (logmsg_len == 0 ||
1266 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1267 err = flush_imsg(ibuf);
1268 if (err)
1269 goto done;
1271 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1272 done:
1273 free(buf);
1274 return err;
1277 static const struct got_error *
1278 get_commit_from_imsg(struct got_commit_object **commit,
1279 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1281 const struct got_error *err = NULL;
1282 struct got_imsg_commit_object *icommit;
1283 size_t len = 0;
1284 int i;
1286 if (datalen < sizeof(*icommit))
1287 return got_error(GOT_ERR_PRIVSEP_LEN);
1289 icommit = imsg->data;
1290 if (datalen != sizeof(*icommit) + icommit->author_len +
1291 icommit->committer_len +
1292 icommit->nparents * SHA1_DIGEST_LENGTH)
1293 return got_error(GOT_ERR_PRIVSEP_LEN);
1295 if (icommit->nparents < 0)
1296 return got_error(GOT_ERR_PRIVSEP_LEN);
1298 len += sizeof(*icommit);
1300 *commit = got_object_commit_alloc_partial();
1301 if (*commit == NULL)
1302 return got_error_from_errno(
1303 "got_object_commit_alloc_partial");
1305 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1306 SHA1_DIGEST_LENGTH);
1307 (*commit)->author_time = icommit->author_time;
1308 (*commit)->author_gmtoff = icommit->author_gmtoff;
1309 (*commit)->committer_time = icommit->committer_time;
1310 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1312 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1313 if ((*commit)->author == NULL) {
1314 err = got_error_from_errno("strndup");
1315 goto done;
1317 len += icommit->author_len;
1319 (*commit)->committer = strndup(imsg->data + len,
1320 icommit->committer_len);
1321 if ((*commit)->committer == NULL) {
1322 err = got_error_from_errno("strndup");
1323 goto done;
1325 len += icommit->committer_len;
1327 if (icommit->logmsg_len == 0) {
1328 (*commit)->logmsg = strdup("");
1329 if ((*commit)->logmsg == NULL) {
1330 err = got_error_from_errno("strdup");
1331 goto done;
1333 } else {
1334 size_t offset = 0, remain = icommit->logmsg_len;
1336 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1337 if ((*commit)->logmsg == NULL) {
1338 err = got_error_from_errno("malloc");
1339 goto done;
1341 while (remain > 0) {
1342 struct imsg imsg_log;
1343 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1344 remain);
1346 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1347 if (err)
1348 goto done;
1350 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1351 err = got_error(GOT_ERR_PRIVSEP_MSG);
1352 goto done;
1355 memcpy((*commit)->logmsg + offset,
1356 imsg_log.data, n);
1357 imsg_free(&imsg_log);
1358 offset += n;
1359 remain -= n;
1361 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1364 for (i = 0; i < icommit->nparents; i++) {
1365 struct got_object_qid *qid;
1367 err = got_object_qid_alloc_partial(&qid);
1368 if (err)
1369 break;
1370 memcpy(&qid->id, imsg->data + len +
1371 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1372 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1373 (*commit)->nparents++;
1375 done:
1376 if (err) {
1377 got_object_commit_close(*commit);
1378 *commit = NULL;
1380 return err;
1383 const struct got_error *
1384 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1386 const struct got_error *err = NULL;
1387 struct imsg imsg;
1388 size_t datalen;
1389 const size_t min_datalen =
1390 MIN(sizeof(struct got_imsg_error),
1391 sizeof(struct got_imsg_commit_object));
1393 *commit = NULL;
1395 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1396 if (err)
1397 return err;
1399 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1401 switch (imsg.hdr.type) {
1402 case GOT_IMSG_COMMIT:
1403 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1404 break;
1405 default:
1406 err = got_error(GOT_ERR_PRIVSEP_MSG);
1407 break;
1410 imsg_free(&imsg);
1412 return err;
1415 static const struct got_error *
1416 send_tree_entries_batch(struct imsgbuf *ibuf,
1417 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1419 struct ibuf *wbuf;
1420 struct got_imsg_tree_entries ientries;
1421 int i;
1423 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1424 if (wbuf == NULL)
1425 return got_error_from_errno("imsg_create TREE_ENTRY");
1427 ientries.nentries = idxN - idx0 + 1;
1428 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1429 return got_error_from_errno("imsg_add TREE_ENTRY");
1431 for (i = idx0; i <= idxN; i++) {
1432 struct got_parsed_tree_entry *pte = &entries[i];
1434 /* Keep in sync with struct got_imsg_tree_object definition! */
1435 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1436 return got_error_from_errno("imsg_add TREE_ENTRY");
1437 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1438 return got_error_from_errno("imsg_add TREE_ENTRY");
1439 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1440 return got_error_from_errno("imsg_add TREE_ENTRY");
1442 /* Remaining bytes are the entry's name. */
1443 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1444 return got_error_from_errno("imsg_add TREE_ENTRY");
1447 wbuf->fd = -1;
1448 imsg_close(ibuf, wbuf);
1449 return NULL;
1452 static const struct got_error *
1453 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1454 int nentries)
1456 const struct got_error *err = NULL;
1457 int i, j;
1458 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1460 i = 0;
1461 for (j = 0; j < nentries; j++) {
1462 struct got_parsed_tree_entry *pte = &entries[j];
1463 size_t len = sizeof(*pte) + pte->namelen;
1465 if (j > 0 &&
1466 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1467 err = send_tree_entries_batch(ibuf, entries,
1468 i, j - 1, entries_len);
1469 if (err)
1470 return err;
1471 i = j;
1472 entries_len = sizeof(struct got_imsg_tree_entries);
1475 entries_len += len;
1478 if (j > 0) {
1479 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1480 entries_len);
1481 if (err)
1482 return err;
1485 return NULL;
1488 const struct got_error *
1489 got_privsep_send_tree(struct imsgbuf *ibuf,
1490 struct got_parsed_tree_entry *entries, int nentries)
1492 const struct got_error *err = NULL;
1493 struct got_imsg_tree_object itree;
1495 itree.nentries = nentries;
1496 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1497 == -1)
1498 return got_error_from_errno("imsg_compose TREE");
1500 err = send_tree_entries(ibuf, entries, nentries);
1501 if (err)
1502 return err;
1504 return flush_imsg(ibuf);
1508 static const struct got_error *
1509 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1510 int *nentries)
1512 const struct got_error *err = NULL;
1513 struct got_imsg_tree_entries *ientries;
1514 struct got_tree_entry *te;
1515 size_t te_offset;
1516 size_t i;
1518 if (datalen <= sizeof(*ientries) ||
1519 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1520 return got_error(GOT_ERR_PRIVSEP_LEN);
1522 ientries = (struct got_imsg_tree_entries *)data;
1523 if (ientries->nentries > INT_MAX) {
1524 return got_error_msg(GOT_ERR_NO_SPACE,
1525 "too many tree entries");
1528 te_offset = sizeof(*ientries);
1529 for (i = 0; i < ientries->nentries; i++) {
1530 struct got_imsg_tree_entry ite;
1531 const char *te_name;
1532 uint8_t *buf = (uint8_t *)data + te_offset;
1534 if (te_offset >= datalen) {
1535 err = got_error(GOT_ERR_PRIVSEP_LEN);
1536 break;
1539 /* Might not be aligned, size is ~32 bytes. */
1540 memcpy(&ite, buf, sizeof(ite));
1542 if (ite.namelen >= sizeof(te->name)) {
1543 err = got_error(GOT_ERR_PRIVSEP_LEN);
1544 break;
1546 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1547 err = got_error(GOT_ERR_PRIVSEP_LEN);
1548 break;
1551 if (*nentries >= tree->nentries) {
1552 err = got_error(GOT_ERR_PRIVSEP_LEN);
1553 break;
1555 te = &tree->entries[*nentries];
1556 te_name = buf + sizeof(ite);
1557 memcpy(te->name, te_name, ite.namelen);
1558 te->name[ite.namelen] = '\0';
1559 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1560 te->mode = ite.mode;
1561 te->idx = *nentries;
1562 (*nentries)++;
1564 te_offset += sizeof(ite) + ite.namelen;
1567 return err;
1570 const struct got_error *
1571 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1573 const struct got_error *err = NULL;
1574 const size_t min_datalen =
1575 MIN(sizeof(struct got_imsg_error),
1576 sizeof(struct got_imsg_tree_object));
1577 struct got_imsg_tree_object *itree;
1578 int nentries = 0;
1580 *tree = NULL;
1582 err = read_imsg(ibuf);
1583 if (err)
1584 goto done;
1586 for (;;) {
1587 struct imsg imsg;
1588 size_t n;
1589 size_t datalen;
1591 n = imsg_get(ibuf, &imsg);
1592 if (n == 0) {
1593 if ((*tree)) {
1594 if (nentries < (*tree)->nentries) {
1595 err = read_imsg(ibuf);
1596 if (err)
1597 break;
1598 continue;
1599 } else
1600 break;
1601 } else {
1602 err = got_error(GOT_ERR_PRIVSEP_MSG);
1603 break;
1607 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1608 imsg_free(&imsg);
1609 err = got_error(GOT_ERR_PRIVSEP_LEN);
1610 break;
1613 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1615 switch (imsg.hdr.type) {
1616 case GOT_IMSG_ERROR:
1617 err = recv_imsg_error(&imsg, datalen);
1618 break;
1619 case GOT_IMSG_TREE:
1620 /* This message should only appear once. */
1621 if (*tree != NULL) {
1622 err = got_error(GOT_ERR_PRIVSEP_MSG);
1623 break;
1625 if (datalen != sizeof(*itree)) {
1626 err = got_error(GOT_ERR_PRIVSEP_LEN);
1627 break;
1629 itree = imsg.data;
1630 if (itree->nentries < 0) {
1631 err = got_error(GOT_ERR_PRIVSEP_LEN);
1632 break;
1634 *tree = malloc(sizeof(**tree));
1635 if (*tree == NULL) {
1636 err = got_error_from_errno("malloc");
1637 break;
1639 (*tree)->entries = calloc(itree->nentries,
1640 sizeof(struct got_tree_entry));
1641 if ((*tree)->entries == NULL) {
1642 err = got_error_from_errno("malloc");
1643 free(*tree);
1644 *tree = NULL;
1645 break;
1647 (*tree)->nentries = itree->nentries;
1648 (*tree)->refcnt = 0;
1649 break;
1650 case GOT_IMSG_TREE_ENTRIES:
1651 /* This message should be preceeded by GOT_IMSG_TREE. */
1652 if (*tree == NULL) {
1653 err = got_error(GOT_ERR_PRIVSEP_MSG);
1654 break;
1656 err = recv_tree_entries(imsg.data, datalen,
1657 *tree, &nentries);
1658 break;
1659 default:
1660 err = got_error(GOT_ERR_PRIVSEP_MSG);
1661 break;
1664 imsg_free(&imsg);
1665 if (err)
1666 break;
1668 done:
1669 if (*tree && (*tree)->nentries != nentries) {
1670 if (err == NULL)
1671 err = got_error(GOT_ERR_PRIVSEP_LEN);
1672 got_object_tree_close(*tree);
1673 *tree = NULL;
1676 return err;
1679 const struct got_error *
1680 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1681 const uint8_t *data)
1683 struct got_imsg_blob iblob;
1685 iblob.size = size;
1686 iblob.hdrlen = hdrlen;
1688 if (data) {
1689 uint8_t *buf;
1691 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1692 return got_error(GOT_ERR_NO_SPACE);
1694 buf = malloc(sizeof(iblob) + size);
1695 if (buf == NULL)
1696 return got_error_from_errno("malloc");
1698 memcpy(buf, &iblob, sizeof(iblob));
1699 memcpy(buf + sizeof(iblob), data, size);
1700 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1701 sizeof(iblob) + size) == -1) {
1702 free(buf);
1703 return got_error_from_errno("imsg_compose BLOB");
1705 free(buf);
1706 } else {
1707 /* Data has already been written to file descriptor. */
1708 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1709 sizeof(iblob)) == -1)
1710 return got_error_from_errno("imsg_compose BLOB");
1714 return flush_imsg(ibuf);
1717 const struct got_error *
1718 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1719 struct imsgbuf *ibuf)
1721 const struct got_error *err = NULL;
1722 struct imsg imsg;
1723 struct got_imsg_blob *iblob;
1724 size_t datalen;
1726 *outbuf = NULL;
1728 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1729 if (err)
1730 return err;
1732 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1734 switch (imsg.hdr.type) {
1735 case GOT_IMSG_BLOB:
1736 if (datalen < sizeof(*iblob)) {
1737 err = got_error(GOT_ERR_PRIVSEP_LEN);
1738 break;
1740 iblob = imsg.data;
1741 *size = iblob->size;
1742 *hdrlen = iblob->hdrlen;
1744 if (datalen == sizeof(*iblob)) {
1745 /* Data has been written to file descriptor. */
1746 break;
1749 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX ||
1750 *size > datalen + sizeof(*iblob)) {
1751 err = got_error(GOT_ERR_PRIVSEP_LEN);
1752 break;
1755 *outbuf = malloc(*size);
1756 if (*outbuf == NULL) {
1757 err = got_error_from_errno("malloc");
1758 break;
1760 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1761 break;
1762 default:
1763 err = got_error(GOT_ERR_PRIVSEP_MSG);
1764 break;
1767 imsg_free(&imsg);
1769 return err;
1772 static const struct got_error *
1773 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1775 const struct got_error *err = NULL;
1776 size_t offset, remain;
1778 offset = 0;
1779 remain = tagmsg_len;
1780 while (remain > 0) {
1781 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1783 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1784 tag->tagmsg + offset, n) == -1) {
1785 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1786 break;
1789 err = flush_imsg(ibuf);
1790 if (err)
1791 break;
1793 offset += n;
1794 remain -= n;
1797 return err;
1800 const struct got_error *
1801 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1803 const struct got_error *err = NULL;
1804 struct got_imsg_tag_object *itag;
1805 uint8_t *buf;
1806 size_t len, total;
1807 size_t tag_len = strlen(tag->tag);
1808 size_t tagger_len = strlen(tag->tagger);
1809 size_t tagmsg_len = strlen(tag->tagmsg);
1811 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1813 buf = malloc(total);
1814 if (buf == NULL)
1815 return got_error_from_errno("malloc");
1817 itag = (struct got_imsg_tag_object *)buf;
1818 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1819 itag->obj_type = tag->obj_type;
1820 itag->tag_len = tag_len;
1821 itag->tagger_len = tagger_len;
1822 itag->tagger_time = tag->tagger_time;
1823 itag->tagger_gmtoff = tag->tagger_gmtoff;
1824 itag->tagmsg_len = tagmsg_len;
1826 len = sizeof(*itag);
1827 memcpy(buf + len, tag->tag, tag_len);
1828 len += tag_len;
1829 memcpy(buf + len, tag->tagger, tagger_len);
1830 len += tagger_len;
1832 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1833 err = got_error_from_errno("imsg_compose TAG");
1834 goto done;
1837 if (tagmsg_len == 0 ||
1838 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1839 err = flush_imsg(ibuf);
1840 if (err)
1841 goto done;
1843 err = send_tagmsg(ibuf, tag, tagmsg_len);
1844 done:
1845 free(buf);
1846 return err;
1849 const struct got_error *
1850 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1852 const struct got_error *err = NULL;
1853 struct imsg imsg;
1854 struct got_imsg_tag_object *itag;
1855 size_t len, datalen;
1856 const size_t min_datalen =
1857 MIN(sizeof(struct got_imsg_error),
1858 sizeof(struct got_imsg_tag_object));
1860 *tag = NULL;
1862 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1863 if (err)
1864 return err;
1866 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1867 len = 0;
1869 switch (imsg.hdr.type) {
1870 case GOT_IMSG_TAG:
1871 if (datalen < sizeof(*itag)) {
1872 err = got_error(GOT_ERR_PRIVSEP_LEN);
1873 break;
1875 itag = imsg.data;
1876 if (datalen != sizeof(*itag) + itag->tag_len +
1877 itag->tagger_len) {
1878 err = got_error(GOT_ERR_PRIVSEP_LEN);
1879 break;
1881 len += sizeof(*itag);
1883 *tag = calloc(1, sizeof(**tag));
1884 if (*tag == NULL) {
1885 err = got_error_from_errno("calloc");
1886 break;
1889 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1891 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1892 if ((*tag)->tag == NULL) {
1893 err = got_error_from_errno("strndup");
1894 break;
1896 len += itag->tag_len;
1898 (*tag)->obj_type = itag->obj_type;
1899 (*tag)->tagger_time = itag->tagger_time;
1900 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1902 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1903 if ((*tag)->tagger == NULL) {
1904 err = got_error_from_errno("strndup");
1905 break;
1907 len += itag->tagger_len;
1909 if (itag->tagmsg_len == 0) {
1910 (*tag)->tagmsg = strdup("");
1911 if ((*tag)->tagmsg == NULL) {
1912 err = got_error_from_errno("strdup");
1913 break;
1915 } else {
1916 size_t offset = 0, remain = itag->tagmsg_len;
1918 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1919 if ((*tag)->tagmsg == NULL) {
1920 err = got_error_from_errno("malloc");
1921 break;
1923 while (remain > 0) {
1924 struct imsg imsg_log;
1925 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1926 remain);
1928 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1929 if (err)
1930 return err;
1932 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1933 return got_error(GOT_ERR_PRIVSEP_MSG);
1935 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1936 n);
1937 imsg_free(&imsg_log);
1938 offset += n;
1939 remain -= n;
1941 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1944 break;
1945 default:
1946 err = got_error(GOT_ERR_PRIVSEP_MSG);
1947 break;
1950 imsg_free(&imsg);
1952 return err;
1955 const struct got_error *
1956 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1957 struct got_packidx *packidx)
1959 const struct got_error *err = NULL;
1960 struct got_imsg_packidx ipackidx;
1961 struct got_imsg_pack ipack;
1962 int fd;
1964 ipackidx.len = packidx->len;
1965 ipackidx.packfile_size = pack->filesize;
1966 fd = dup(packidx->fd);
1967 if (fd == -1)
1968 return got_error_from_errno("dup");
1970 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1971 sizeof(ipackidx)) == -1) {
1972 err = got_error_from_errno("imsg_compose PACKIDX");
1973 close(fd);
1974 return err;
1977 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1978 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1979 return got_error(GOT_ERR_NO_SPACE);
1980 ipack.filesize = pack->filesize;
1982 fd = dup(pack->fd);
1983 if (fd == -1)
1984 return got_error_from_errno("dup");
1986 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1987 == -1) {
1988 err = got_error_from_errno("imsg_compose PACK");
1989 close(fd);
1990 return err;
1993 return flush_imsg(ibuf);
1996 const struct got_error *
1997 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1998 struct got_object_id *id)
2000 struct got_imsg_packed_object iobj;
2002 iobj.idx = idx;
2003 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2005 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2006 &iobj, sizeof(iobj)) == -1)
2007 return got_error_from_errno("imsg_compose "
2008 "PACKED_OBJECT_REQUEST");
2010 return flush_imsg(ibuf);
2013 const struct got_error *
2014 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2015 struct got_object_id *id)
2017 struct got_imsg_packed_object iobj;
2019 iobj.idx = idx;
2020 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2022 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2023 &iobj, sizeof(iobj)) == -1)
2024 return got_error_from_errno("imsg_compose "
2025 "PACKED_OBJECT_REQUEST");
2027 return flush_imsg(ibuf);
2030 const struct got_error *
2031 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2033 const struct got_error *err = NULL;
2035 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2036 NULL, 0) == -1) {
2037 err = got_error_from_errno("imsg_compose "
2038 "GITCONFIG_PARSE_REQUEST");
2039 close(fd);
2040 return err;
2043 return flush_imsg(ibuf);
2046 const struct got_error *
2047 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2049 if (imsg_compose(ibuf,
2050 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2051 NULL, 0) == -1)
2052 return got_error_from_errno("imsg_compose "
2053 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2055 return flush_imsg(ibuf);
2058 const struct got_error *
2059 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2061 if (imsg_compose(ibuf,
2062 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2063 NULL, 0) == -1)
2064 return got_error_from_errno("imsg_compose "
2065 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2067 return flush_imsg(ibuf);
2071 const struct got_error *
2072 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2074 if (imsg_compose(ibuf,
2075 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2076 return got_error_from_errno("imsg_compose "
2077 "GITCONFIG_AUTHOR_NAME_REQUEST");
2079 return flush_imsg(ibuf);
2082 const struct got_error *
2083 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2085 if (imsg_compose(ibuf,
2086 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2087 return got_error_from_errno("imsg_compose "
2088 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2090 return flush_imsg(ibuf);
2093 const struct got_error *
2094 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2096 if (imsg_compose(ibuf,
2097 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2098 return got_error_from_errno("imsg_compose "
2099 "GITCONFIG_REMOTE_REQUEST");
2101 return flush_imsg(ibuf);
2104 const struct got_error *
2105 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2107 if (imsg_compose(ibuf,
2108 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2109 return got_error_from_errno("imsg_compose "
2110 "GITCONFIG_OWNER_REQUEST");
2112 return flush_imsg(ibuf);
2115 const struct got_error *
2116 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2118 const struct got_error *err = NULL;
2119 struct imsg imsg;
2120 size_t datalen;
2122 *str = NULL;
2124 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2125 if (err)
2126 return err;
2127 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2129 switch (imsg.hdr.type) {
2130 case GOT_IMSG_GITCONFIG_STR_VAL:
2131 if (datalen == 0)
2132 break;
2133 /* datalen does not include terminating \0 */
2134 *str = malloc(datalen + 1);
2135 if (*str == NULL) {
2136 err = got_error_from_errno("malloc");
2137 break;
2139 memcpy(*str, imsg.data, datalen);
2140 (*str)[datalen] = '\0';
2141 break;
2142 default:
2143 err = got_error(GOT_ERR_PRIVSEP_MSG);
2144 break;
2147 imsg_free(&imsg);
2148 return err;
2151 const struct got_error *
2152 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2154 const struct got_error *err = NULL;
2155 struct imsg imsg;
2156 size_t datalen;
2157 const size_t min_datalen =
2158 MIN(sizeof(struct got_imsg_error), sizeof(int));
2160 *val = 0;
2162 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2163 if (err)
2164 return err;
2165 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2167 switch (imsg.hdr.type) {
2168 case GOT_IMSG_GITCONFIG_INT_VAL:
2169 if (datalen != sizeof(*val)) {
2170 err = got_error(GOT_ERR_PRIVSEP_LEN);
2171 break;
2173 memcpy(val, imsg.data, sizeof(*val));
2174 break;
2175 default:
2176 err = got_error(GOT_ERR_PRIVSEP_MSG);
2177 break;
2180 imsg_free(&imsg);
2181 return err;
2184 static void
2185 free_remote_data(struct got_remote_repo *remote)
2187 int i;
2189 free(remote->name);
2190 free(remote->fetch_url);
2191 free(remote->send_url);
2192 for (i = 0; i < remote->nfetch_branches; i++)
2193 free(remote->fetch_branches[i]);
2194 free(remote->fetch_branches);
2195 for (i = 0; i < remote->nsend_branches; i++)
2196 free(remote->send_branches[i]);
2197 free(remote->send_branches);
2198 for (i = 0; i < remote->nfetch_refs; i++)
2199 free(remote->fetch_refs[i]);
2200 free(remote->fetch_refs);
2203 const struct got_error *
2204 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2205 int *nremotes, struct imsgbuf *ibuf)
2207 const struct got_error *err = NULL;
2208 struct imsg imsg;
2209 size_t datalen;
2210 struct got_imsg_remotes iremotes;
2211 struct got_imsg_remote iremote;
2213 *remotes = NULL;
2214 *nremotes = 0;
2215 iremotes.nremotes = 0;
2217 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2218 if (err)
2219 return err;
2220 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2222 switch (imsg.hdr.type) {
2223 case GOT_IMSG_GITCONFIG_REMOTES:
2224 if (datalen != sizeof(iremotes)) {
2225 err = got_error(GOT_ERR_PRIVSEP_LEN);
2226 break;
2228 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2229 if (iremotes.nremotes == 0) {
2230 imsg_free(&imsg);
2231 return NULL;
2233 break;
2234 default:
2235 imsg_free(&imsg);
2236 return got_error(GOT_ERR_PRIVSEP_MSG);
2239 imsg_free(&imsg);
2241 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2242 if (*remotes == NULL)
2243 return got_error_from_errno("recallocarray");
2245 while (*nremotes < iremotes.nremotes) {
2246 struct got_remote_repo *remote;
2248 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2249 if (err)
2250 break;
2251 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2253 switch (imsg.hdr.type) {
2254 case GOT_IMSG_GITCONFIG_REMOTE:
2255 remote = &(*remotes)[*nremotes];
2256 memset(remote, 0, sizeof(*remote));
2257 if (datalen < sizeof(iremote)) {
2258 err = got_error(GOT_ERR_PRIVSEP_LEN);
2259 break;
2261 memcpy(&iremote, imsg.data, sizeof(iremote));
2262 if (iremote.name_len == 0 ||
2263 iremote.fetch_url_len == 0 ||
2264 iremote.send_url_len == 0 ||
2265 (sizeof(iremote) + iremote.name_len +
2266 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2267 err = got_error(GOT_ERR_PRIVSEP_LEN);
2268 break;
2270 remote->name = strndup(imsg.data + sizeof(iremote),
2271 iremote.name_len);
2272 if (remote->name == NULL) {
2273 err = got_error_from_errno("strndup");
2274 break;
2276 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2277 iremote.name_len, iremote.fetch_url_len);
2278 if (remote->fetch_url == NULL) {
2279 err = got_error_from_errno("strndup");
2280 free_remote_data(remote);
2281 break;
2283 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2284 iremote.name_len + iremote.fetch_url_len,
2285 iremote.send_url_len);
2286 if (remote->send_url == NULL) {
2287 err = got_error_from_errno("strndup");
2288 free_remote_data(remote);
2289 break;
2291 remote->mirror_references = iremote.mirror_references;
2292 remote->fetch_all_branches = iremote.fetch_all_branches;
2293 remote->nfetch_branches = 0;
2294 remote->fetch_branches = NULL;
2295 remote->nsend_branches = 0;
2296 remote->send_branches = NULL;
2297 remote->nfetch_refs = 0;
2298 remote->fetch_refs = NULL;
2299 (*nremotes)++;
2300 break;
2301 default:
2302 err = got_error(GOT_ERR_PRIVSEP_MSG);
2303 break;
2306 imsg_free(&imsg);
2307 if (err)
2308 break;
2311 if (err) {
2312 int i;
2313 for (i = 0; i < *nremotes; i++)
2314 free_remote_data(&(*remotes)[i]);
2315 free(*remotes);
2316 *remotes = NULL;
2317 *nremotes = 0;
2319 return err;
2322 const struct got_error *
2323 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2325 const struct got_error *err = NULL;
2327 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2328 NULL, 0) == -1) {
2329 err = got_error_from_errno("imsg_compose "
2330 "GOTCONFIG_PARSE_REQUEST");
2331 close(fd);
2332 return err;
2335 return flush_imsg(ibuf);
2338 const struct got_error *
2339 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2341 if (imsg_compose(ibuf,
2342 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2343 return got_error_from_errno("imsg_compose "
2344 "GOTCONFIG_AUTHOR_REQUEST");
2346 return flush_imsg(ibuf);
2349 const struct got_error *
2350 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2352 if (imsg_compose(ibuf,
2353 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2354 return got_error_from_errno("imsg_compose "
2355 "GOTCONFIG_REMOTE_REQUEST");
2357 return flush_imsg(ibuf);
2360 const struct got_error *
2361 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2363 const struct got_error *err = NULL;
2364 struct imsg imsg;
2365 size_t datalen;
2367 *str = NULL;
2369 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2370 if (err)
2371 return err;
2372 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2374 switch (imsg.hdr.type) {
2375 case GOT_IMSG_ERROR:
2376 err = recv_imsg_error(&imsg, datalen);
2377 break;
2378 case GOT_IMSG_GOTCONFIG_STR_VAL:
2379 if (datalen == 0)
2380 break;
2381 /* datalen does not include terminating \0 */
2382 *str = malloc(datalen + 1);
2383 if (*str == NULL) {
2384 err = got_error_from_errno("malloc");
2385 break;
2387 memcpy(*str, imsg.data, datalen);
2388 (*str)[datalen] = '\0';
2389 break;
2390 default:
2391 err = got_error(GOT_ERR_PRIVSEP_MSG);
2392 break;
2395 imsg_free(&imsg);
2396 return err;
2399 const struct got_error *
2400 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2401 int *nremotes, struct imsgbuf *ibuf)
2403 const struct got_error *err = NULL;
2404 struct imsg imsg;
2405 size_t datalen;
2406 struct got_imsg_remotes iremotes;
2407 struct got_imsg_remote iremote;
2408 const size_t min_datalen =
2409 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2411 *remotes = NULL;
2412 *nremotes = 0;
2413 iremotes.nremotes = 0;
2415 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2416 if (err)
2417 return err;
2418 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2420 switch (imsg.hdr.type) {
2421 case GOT_IMSG_ERROR:
2422 err = recv_imsg_error(&imsg, datalen);
2423 break;
2424 case GOT_IMSG_GOTCONFIG_REMOTES:
2425 if (datalen != sizeof(iremotes)) {
2426 err = got_error(GOT_ERR_PRIVSEP_LEN);
2427 break;
2429 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2430 if (iremotes.nremotes < 0) {
2431 err = got_error(GOT_ERR_PRIVSEP_LEN);
2432 break;
2434 if (iremotes.nremotes == 0) {
2435 imsg_free(&imsg);
2436 return NULL;
2438 break;
2439 default:
2440 imsg_free(&imsg);
2441 return got_error(GOT_ERR_PRIVSEP_MSG);
2444 imsg_free(&imsg);
2446 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2447 if (*remotes == NULL)
2448 return got_error_from_errno("recallocarray");
2450 while (*nremotes < iremotes.nremotes) {
2451 struct got_remote_repo *remote;
2452 const size_t min_datalen =
2453 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2454 int i;
2456 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2457 if (err)
2458 break;
2459 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2461 switch (imsg.hdr.type) {
2462 case GOT_IMSG_ERROR:
2463 err = recv_imsg_error(&imsg, datalen);
2464 break;
2465 case GOT_IMSG_GOTCONFIG_REMOTE:
2466 remote = &(*remotes)[*nremotes];
2467 memset(remote, 0, sizeof(*remote));
2468 if (datalen < sizeof(iremote)) {
2469 err = got_error(GOT_ERR_PRIVSEP_LEN);
2470 break;
2472 memcpy(&iremote, imsg.data, sizeof(iremote));
2473 if (iremote.name_len == 0 ||
2474 (iremote.fetch_url_len == 0 &&
2475 iremote.send_url_len == 0) ||
2476 (sizeof(iremote) + iremote.name_len +
2477 iremote.fetch_url_len + iremote.send_url_len) >
2478 datalen) {
2479 err = got_error(GOT_ERR_PRIVSEP_LEN);
2480 break;
2482 remote->name = strndup(imsg.data + sizeof(iremote),
2483 iremote.name_len);
2484 if (remote->name == NULL) {
2485 err = got_error_from_errno("strndup");
2486 break;
2488 remote->fetch_url = strndup(imsg.data +
2489 sizeof(iremote) + iremote.name_len,
2490 iremote.fetch_url_len);
2491 if (remote->fetch_url == NULL) {
2492 err = got_error_from_errno("strndup");
2493 free_remote_data(remote);
2494 break;
2496 remote->send_url = strndup(imsg.data +
2497 sizeof(iremote) + iremote.name_len +
2498 iremote.fetch_url_len, iremote.send_url_len);
2499 if (remote->send_url == NULL) {
2500 err = got_error_from_errno("strndup");
2501 free_remote_data(remote);
2502 break;
2504 remote->mirror_references = iremote.mirror_references;
2505 remote->fetch_all_branches = iremote.fetch_all_branches;
2506 if (iremote.nfetch_branches > 0) {
2507 remote->fetch_branches = recallocarray(NULL, 0,
2508 iremote.nfetch_branches, sizeof(char *));
2509 if (remote->fetch_branches == NULL) {
2510 err = got_error_from_errno("calloc");
2511 free_remote_data(remote);
2512 break;
2515 remote->nfetch_branches = 0;
2516 for (i = 0; i < iremote.nfetch_branches; i++) {
2517 char *branch;
2518 err = got_privsep_recv_gotconfig_str(&branch,
2519 ibuf);
2520 if (err) {
2521 free_remote_data(remote);
2522 goto done;
2524 remote->fetch_branches[i] = branch;
2525 remote->nfetch_branches++;
2527 if (iremote.nsend_branches > 0) {
2528 remote->send_branches = recallocarray(NULL, 0,
2529 iremote.nsend_branches, sizeof(char *));
2530 if (remote->send_branches == NULL) {
2531 err = got_error_from_errno("calloc");
2532 free_remote_data(remote);
2533 break;
2536 remote->nsend_branches = 0;
2537 for (i = 0; i < iremote.nsend_branches; i++) {
2538 char *branch;
2539 err = got_privsep_recv_gotconfig_str(&branch,
2540 ibuf);
2541 if (err) {
2542 free_remote_data(remote);
2543 goto done;
2545 remote->send_branches[i] = branch;
2546 remote->nsend_branches++;
2548 if (iremote.nfetch_refs > 0) {
2549 remote->fetch_refs = recallocarray(NULL, 0,
2550 iremote.nfetch_refs, sizeof(char *));
2551 if (remote->fetch_refs == NULL) {
2552 err = got_error_from_errno("calloc");
2553 free_remote_data(remote);
2554 break;
2557 remote->nfetch_refs = 0;
2558 for (i = 0; i < iremote.nfetch_refs; i++) {
2559 char *ref;
2560 err = got_privsep_recv_gotconfig_str(&ref,
2561 ibuf);
2562 if (err) {
2563 free_remote_data(remote);
2564 goto done;
2566 remote->fetch_refs[i] = ref;
2567 remote->nfetch_refs++;
2569 (*nremotes)++;
2570 break;
2571 default:
2572 err = got_error(GOT_ERR_PRIVSEP_MSG);
2573 break;
2576 imsg_free(&imsg);
2577 if (err)
2578 break;
2580 done:
2581 if (err) {
2582 int i;
2583 for (i = 0; i < *nremotes; i++)
2584 free_remote_data(&(*remotes)[i]);
2585 free(*remotes);
2586 *remotes = NULL;
2587 *nremotes = 0;
2589 return err;
2592 const struct got_error *
2593 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2594 struct got_object_id *id, int idx, const char *path)
2596 struct ibuf *wbuf;
2597 size_t path_len = strlen(path) + 1;
2599 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2600 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2601 if (wbuf == NULL)
2602 return got_error_from_errno(
2603 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2604 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2605 return got_error_from_errno("imsg_add "
2606 "COMMIT_TRAVERSAL_REQUEST");
2607 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2608 return got_error_from_errno("imsg_add "
2609 "COMMIT_TRAVERSAL_REQUEST");
2610 if (imsg_add(wbuf, path, path_len) == -1)
2611 return got_error_from_errno("imsg_add "
2612 "COMMIT_TRAVERSAL_REQUEST");
2614 wbuf->fd = -1;
2615 imsg_close(ibuf, wbuf);
2617 return flush_imsg(ibuf);
2620 const struct got_error *
2621 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2622 struct got_object_id **changed_commit_id,
2623 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2625 const struct got_error *err = NULL;
2626 struct imsg imsg;
2627 struct got_imsg_traversed_commits *icommits;
2628 size_t datalen;
2629 int i, done = 0;
2631 *changed_commit = NULL;
2632 *changed_commit_id = NULL;
2634 while (!done) {
2635 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2636 if (err)
2637 return err;
2639 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2640 switch (imsg.hdr.type) {
2641 case GOT_IMSG_TRAVERSED_COMMITS:
2642 icommits = imsg.data;
2643 if (datalen != sizeof(*icommits) +
2644 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2645 err = got_error(GOT_ERR_PRIVSEP_LEN);
2646 break;
2648 for (i = 0; i < icommits->ncommits; i++) {
2649 struct got_object_qid *qid;
2650 uint8_t *sha1 = (uint8_t *)imsg.data +
2651 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2652 err = got_object_qid_alloc_partial(&qid);
2653 if (err)
2654 break;
2655 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2656 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2658 /* The last commit may contain a change. */
2659 if (i == icommits->ncommits - 1) {
2660 *changed_commit_id =
2661 got_object_id_dup(&qid->id);
2662 if (*changed_commit_id == NULL) {
2663 err = got_error_from_errno(
2664 "got_object_id_dup");
2665 break;
2669 break;
2670 case GOT_IMSG_COMMIT:
2671 if (*changed_commit_id == NULL) {
2672 err = got_error(GOT_ERR_PRIVSEP_MSG);
2673 break;
2675 err = get_commit_from_imsg(changed_commit, &imsg,
2676 datalen, ibuf);
2677 break;
2678 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2679 done = 1;
2680 break;
2681 default:
2682 err = got_error(GOT_ERR_PRIVSEP_MSG);
2683 break;
2686 imsg_free(&imsg);
2687 if (err)
2688 break;
2691 if (err)
2692 got_object_id_queue_free(commit_ids);
2693 return err;
2696 const struct got_error *
2697 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2698 struct got_object_id *tree_id, const char *path,
2699 struct got_parsed_tree_entry *entries, int nentries)
2701 const struct got_error *err = NULL;
2702 struct ibuf *wbuf;
2703 size_t path_len = strlen(path);
2704 size_t msglen;
2706 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2707 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2708 if (wbuf == NULL)
2709 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2711 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2712 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2713 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2714 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2715 if (imsg_add(wbuf, path, path_len) == -1)
2716 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2718 wbuf->fd = -1;
2719 imsg_close(ibuf, wbuf);
2721 if (entries) {
2722 err = send_tree_entries(ibuf, entries, nentries);
2723 if (err)
2724 return err;
2727 return flush_imsg(ibuf);
2730 const struct got_error *
2731 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2733 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2734 0, 0, -1, NULL, 0) == -1)
2735 return got_error_from_errno("imsg_compose "
2736 "OBJECT_ENUMERATION_REQUEST");
2738 return flush_imsg(ibuf);
2741 const struct got_error *
2742 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2744 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2745 0, 0, -1, NULL, 0) == -1)
2746 return got_error_from_errno("imsg_compose "
2747 "OBJECT_ENUMERATION_DONE");
2749 return flush_imsg(ibuf);
2752 const struct got_error *
2753 got_privsep_send_object_enumeration_incomplete(struct imsgbuf *ibuf)
2755 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE,
2756 0, 0, -1, NULL, 0) == -1)
2757 return got_error_from_errno("imsg_compose "
2758 "OBJECT_ENUMERATION_INCOMPLETE");
2760 return flush_imsg(ibuf);
2763 const struct got_error *
2764 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2765 struct got_object_id *id, time_t mtime)
2767 struct ibuf *wbuf;
2769 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2770 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2771 if (wbuf == NULL)
2772 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2774 /* Keep in sync with struct got_imsg_enumerated_commit! */
2775 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2776 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2777 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2778 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2780 wbuf->fd = -1;
2781 imsg_close(ibuf, wbuf);
2782 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2783 return NULL;
2786 const struct got_error *
2787 got_privsep_recv_enumerated_objects(int *found_all_objects,
2788 struct imsgbuf *ibuf,
2789 got_object_enumerate_commit_cb cb_commit,
2790 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2791 struct got_repository *repo)
2793 const struct got_error *err = NULL;
2794 struct imsg imsg;
2795 struct got_imsg_enumerated_commit *icommit = NULL;
2796 struct got_object_id commit_id;
2797 int have_commit = 0;
2798 time_t mtime = 0;
2799 struct got_tree_object tree;
2800 struct got_imsg_enumerated_tree *itree;
2801 struct got_object_id tree_id;
2802 char *path = NULL, *canon_path = NULL;
2803 size_t datalen, path_len;
2804 int nentries = -1;
2805 int done = 0;
2807 *found_all_objects = 0;
2808 memset(&tree, 0, sizeof(tree));
2810 while (!done) {
2811 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2812 if (err)
2813 break;
2815 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2816 switch (imsg.hdr.type) {
2817 case GOT_IMSG_ENUMERATED_COMMIT:
2818 if (have_commit && nentries != -1) {
2819 err = got_error(GOT_ERR_PRIVSEP_MSG);
2820 break;
2822 if (datalen != sizeof(*icommit)) {
2823 err = got_error(GOT_ERR_PRIVSEP_LEN);
2824 break;
2826 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2827 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2828 mtime = icommit->mtime;
2829 have_commit = 1;
2830 break;
2831 case GOT_IMSG_ENUMERATED_TREE:
2832 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2833 if (!have_commit) {
2834 err = got_error(GOT_ERR_PRIVSEP_MSG);
2835 break;
2837 if (datalen < sizeof(*itree)) {
2838 err = got_error(GOT_ERR_PRIVSEP_LEN);
2839 break;
2841 itree = imsg.data;
2842 path_len = datalen - sizeof(*itree);
2843 if (path_len == 0) {
2844 err = got_error(GOT_ERR_PRIVSEP_LEN);
2845 break;
2847 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2848 free(path);
2849 path = malloc(path_len + 1);
2850 if (path == NULL) {
2851 err = got_error_from_errno("malloc");
2852 break;
2854 free(canon_path);
2855 canon_path = malloc(path_len + 1);
2856 if (canon_path == NULL) {
2857 err = got_error_from_errno("malloc");
2858 break;
2860 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2861 path_len);
2862 path[path_len] = '\0';
2863 if (!got_path_is_absolute(path)) {
2864 err = got_error(GOT_ERR_BAD_PATH);
2865 break;
2867 if (got_path_is_root_dir(path)) {
2868 /* XXX check what got_canonpath() does wrong */
2869 canon_path[0] = '/';
2870 canon_path[1] = '\0';
2871 } else {
2872 err = got_canonpath(path, canon_path,
2873 path_len + 1);
2874 if (err)
2875 break;
2877 if (strcmp(path, canon_path) != 0) {
2878 err = got_error(GOT_ERR_BAD_PATH);
2879 break;
2881 if (nentries != -1) {
2882 err = got_error(GOT_ERR_PRIVSEP_MSG);
2883 break;
2885 if (itree->nentries < -1) {
2886 err = got_error(GOT_ERR_PRIVSEP_MSG);
2887 break;
2889 if (itree->nentries == -1) {
2890 /* Tree was not found in pack file. */
2891 done = 1;
2892 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2893 path, repo);
2894 break;
2896 if (itree->nentries > INT_MAX) {
2897 err = got_error(GOT_ERR_PRIVSEP_LEN);
2898 break;
2900 tree.entries = calloc(itree->nentries,
2901 sizeof(struct got_tree_entry));
2902 if (tree.entries == NULL) {
2903 err = got_error_from_errno("calloc");
2904 break;
2906 if (itree->nentries == 0) {
2907 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2908 path, repo);
2909 if (err)
2910 break;
2912 /* Prepare for next tree. */
2913 free(tree.entries);
2914 memset(&tree, 0, sizeof(tree));
2915 nentries = -1;
2916 } else {
2917 tree.nentries = itree->nentries;
2918 nentries = 0;
2920 break;
2921 case GOT_IMSG_TREE_ENTRIES:
2922 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2923 if (nentries <= -1) {
2924 err = got_error(GOT_ERR_PRIVSEP_MSG);
2925 break;
2927 err = recv_tree_entries(imsg.data, datalen,
2928 &tree, &nentries);
2929 if (err)
2930 break;
2931 if (tree.nentries == nentries) {
2932 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2933 path, repo);
2934 if (err)
2935 break;
2937 /* Prepare for next tree. */
2938 free(tree.entries);
2939 memset(&tree, 0, sizeof(tree));
2940 nentries = -1;
2942 break;
2943 case GOT_IMSG_TREE_ENUMERATION_DONE:
2944 /* All trees have been found and traversed. */
2945 if (!have_commit || path == NULL || nentries != -1) {
2946 err = got_error(GOT_ERR_PRIVSEP_MSG);
2947 break;
2949 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2950 if (err)
2951 break;
2952 have_commit = 0;
2953 break;
2954 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2955 *found_all_objects = 1;
2956 done = 1;
2957 break;
2958 case GOT_IMSG_OBJECT_ENUMERATION_INCOMPLETE:
2959 done = 1;
2960 break;
2961 default:
2962 err = got_error(GOT_ERR_PRIVSEP_MSG);
2963 break;
2966 imsg_free(&imsg);
2967 if (err)
2968 break;
2971 free(path);
2972 free(canon_path);
2973 free(tree.entries);
2974 return err;
2977 const struct got_error *
2978 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2979 struct got_object_id *id)
2981 struct got_imsg_raw_delta_request dreq;
2983 dreq.idx = idx;
2984 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2986 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2987 &dreq, sizeof(dreq)) == -1)
2988 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2990 return flush_imsg(ibuf);
2993 const struct got_error *
2994 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2996 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2999 const struct got_error *
3000 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
3001 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
3002 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
3004 struct got_imsg_raw_delta idelta;
3005 int ret;
3007 idelta.base_size = base_size;
3008 idelta.result_size = result_size;
3009 idelta.delta_size = delta_size;
3010 idelta.delta_compressed_size = delta_compressed_size;
3011 idelta.delta_offset = delta_offset;
3012 idelta.delta_out_offset = delta_out_offset;
3013 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
3015 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
3016 &idelta, sizeof(idelta));
3017 if (ret == -1)
3018 return got_error_from_errno("imsg_compose RAW_DELTA");
3020 return flush_imsg(ibuf);
3023 const struct got_error *
3024 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
3025 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
3026 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
3028 const struct got_error *err = NULL;
3029 struct imsg imsg;
3030 struct got_imsg_raw_delta *delta;
3031 size_t datalen;
3033 *base_size = 0;
3034 *result_size = 0;
3035 *delta_size = 0;
3036 *delta_compressed_size = 0;
3037 *delta_offset = 0;
3038 *delta_out_offset = 0;
3039 *base_id = NULL;
3041 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3042 if (err)
3043 return err;
3045 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3047 switch (imsg.hdr.type) {
3048 case GOT_IMSG_RAW_DELTA:
3049 if (datalen != sizeof(*delta)) {
3050 err = got_error(GOT_ERR_PRIVSEP_LEN);
3051 break;
3053 delta = imsg.data;
3054 *base_size = delta->base_size;
3055 *result_size = delta->result_size;
3056 *delta_size = delta->delta_size;
3057 *delta_compressed_size = delta->delta_compressed_size;
3058 *delta_offset = delta->delta_offset;
3059 *delta_out_offset = delta->delta_out_offset;
3060 *base_id = calloc(1, sizeof(**base_id));
3061 if (*base_id == NULL) {
3062 err = got_error_from_errno("malloc");
3063 break;
3065 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3066 break;
3067 default:
3068 err = got_error(GOT_ERR_PRIVSEP_MSG);
3069 break;
3072 imsg_free(&imsg);
3074 if (err) {
3075 free(*base_id);
3076 *base_id = NULL;
3078 return err;
3081 static const struct got_error *
3082 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3084 const struct got_error *err = NULL;
3085 struct got_imsg_object_idlist idlist;
3086 struct ibuf *wbuf;
3087 size_t i;
3089 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3090 return got_error(GOT_ERR_NO_SPACE);
3092 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3093 sizeof(idlist) + nids * sizeof(**ids));
3094 if (wbuf == NULL) {
3095 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3096 return err;
3099 idlist.nids = nids;
3100 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3101 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3103 for (i = 0; i < nids; i++) {
3104 struct got_object_id *id = ids[i];
3105 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3106 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3109 wbuf->fd = -1;
3110 imsg_close(ibuf, wbuf);
3112 return flush_imsg(ibuf);
3115 const struct got_error *
3116 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3117 struct got_object_id **ids, size_t nids)
3119 const struct got_error *err = NULL;
3120 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3121 int i, queued = 0;
3123 for (i = 0; i < nids; i++) {
3124 idlist[i % nitems(idlist)] = ids[i];
3125 queued++;
3126 if (queued >= nitems(idlist)) {
3127 err = send_idlist(ibuf, idlist, queued);
3128 if (err)
3129 return err;
3130 queued = 0;
3134 if (queued > 0) {
3135 err = send_idlist(ibuf, idlist, queued);
3136 if (err)
3137 return err;
3140 return NULL;
3143 const struct got_error *
3144 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3146 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3147 == -1)
3148 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3150 return flush_imsg(ibuf);
3153 const struct got_error *
3154 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3155 size_t *nids, struct imsgbuf *ibuf)
3157 const struct got_error *err = NULL;
3158 struct imsg imsg;
3159 struct got_imsg_object_idlist *idlist;
3160 size_t datalen;
3162 *ids = NULL;
3163 *done = 0;
3164 *nids = 0;
3166 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3167 if (err)
3168 return err;
3170 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3171 switch (imsg.hdr.type) {
3172 case GOT_IMSG_OBJ_ID_LIST:
3173 if (datalen < sizeof(*idlist)) {
3174 err = got_error(GOT_ERR_PRIVSEP_LEN);
3175 break;
3177 idlist = imsg.data;
3178 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3179 idlist->nids * sizeof(**ids) > datalen - sizeof(*idlist)) {
3180 err = got_error(GOT_ERR_PRIVSEP_LEN);
3181 break;
3183 *nids = idlist->nids;
3184 *ids = calloc(*nids, sizeof(**ids));
3185 if (*ids == NULL) {
3186 err = got_error_from_errno("calloc");
3187 break;
3189 memcpy(*ids, (uint8_t *)imsg.data + sizeof(*idlist),
3190 *nids * sizeof(**ids));
3191 break;
3192 case GOT_IMSG_OBJ_ID_LIST_DONE:
3193 *done = 1;
3194 break;
3195 default:
3196 err = got_error(GOT_ERR_PRIVSEP_MSG);
3197 break;
3200 imsg_free(&imsg);
3202 return err;
3205 const struct got_error *
3206 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3208 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3209 == -1)
3210 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3212 return flush_imsg(ibuf);
3215 const struct got_error *
3216 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3217 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3219 const struct got_error *err = NULL;
3220 struct ibuf *wbuf;
3221 struct got_imsg_reused_deltas ideltas;
3222 size_t i;
3224 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3225 return got_error(GOT_ERR_NO_SPACE);
3227 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3228 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3229 if (wbuf == NULL) {
3230 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3231 return err;
3234 ideltas.ndeltas = ndeltas;
3235 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3236 return got_error_from_errno("imsg_add REUSED_DELTAS");
3238 for (i = 0; i < ndeltas; i++) {
3239 struct got_imsg_reused_delta *delta = &deltas[i];
3240 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3241 return got_error_from_errno("imsg_add REUSED_DELTAS");
3244 wbuf->fd = -1;
3245 imsg_close(ibuf, wbuf);
3247 return flush_imsg(ibuf);
3250 const struct got_error *
3251 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3253 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3254 == -1)
3255 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3257 return flush_imsg(ibuf);
3260 const struct got_error *
3261 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3262 size_t *ndeltas, struct imsgbuf *ibuf)
3264 const struct got_error *err = NULL;
3265 struct imsg imsg;
3266 struct got_imsg_reused_deltas *ideltas;
3267 size_t datalen;
3269 *done = 0;
3270 *ndeltas = 0;
3272 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3273 if (err)
3274 return err;
3276 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3277 switch (imsg.hdr.type) {
3278 case GOT_IMSG_REUSED_DELTAS:
3279 if (datalen < sizeof(*ideltas)) {
3280 err = got_error(GOT_ERR_PRIVSEP_LEN);
3281 break;
3283 ideltas = imsg.data;
3284 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS ||
3285 ideltas->ndeltas * sizeof(*deltas) >
3286 datalen - sizeof(*ideltas)) {
3287 err = got_error(GOT_ERR_PRIVSEP_LEN);
3288 break;
3290 *ndeltas = ideltas->ndeltas;
3291 memcpy(deltas, (uint8_t *)imsg.data + sizeof(*ideltas),
3292 *ndeltas * sizeof(*deltas));
3293 break;
3294 case GOT_IMSG_DELTA_REUSE_DONE:
3295 *done = 1;
3296 break;
3297 default:
3298 err = got_error(GOT_ERR_PRIVSEP_MSG);
3299 break;
3302 imsg_free(&imsg);
3304 return err;
3307 const struct got_error *
3308 got_privsep_unveil_exec_helpers(void)
3310 const char *helpers[] = {
3311 GOT_PATH_PROG_READ_PACK,
3312 GOT_PATH_PROG_READ_OBJECT,
3313 GOT_PATH_PROG_READ_COMMIT,
3314 GOT_PATH_PROG_READ_TREE,
3315 GOT_PATH_PROG_READ_BLOB,
3316 GOT_PATH_PROG_READ_TAG,
3317 GOT_PATH_PROG_READ_GITCONFIG,
3318 GOT_PATH_PROG_READ_GOTCONFIG,
3319 GOT_PATH_PROG_READ_PATCH,
3320 GOT_PATH_PROG_FETCH_PACK,
3321 GOT_PATH_PROG_INDEX_PACK,
3322 GOT_PATH_PROG_SEND_PACK,
3324 size_t i;
3326 for (i = 0; i < nitems(helpers); i++) {
3327 if (unveil(helpers[i], "x") == 0)
3328 continue;
3329 return got_error_from_errno2("unveil", helpers[i]);
3332 return NULL;
3335 void
3336 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3338 if (close(imsg_fds[0]) == -1) {
3339 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3340 _exit(1);
3343 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3344 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3345 _exit(1);
3348 closefrom(GOT_IMSG_FD_CHILD + 1);
3350 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3351 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3352 strerror(errno));
3353 _exit(1);