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 + *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 *nobj_total = iprogress->nobj_total;
1104 *nobj_indexed = iprogress->nobj_indexed;
1105 *nobj_loose = iprogress->nobj_loose;
1106 *nobj_resolved = iprogress->nobj_resolved;
1107 break;
1108 case GOT_IMSG_IDXPACK_DONE:
1109 if (datalen != 0) {
1110 err = got_error(GOT_ERR_PRIVSEP_LEN);
1111 break;
1113 *done = 1;
1114 break;
1115 default:
1116 err = got_error(GOT_ERR_PRIVSEP_MSG);
1117 break;
1120 imsg_free(&imsg);
1121 return err;
1124 const struct got_error *
1125 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1126 struct imsgbuf *ibuf)
1128 struct got_imsg_object *iobj;
1129 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1131 if (datalen != sizeof(*iobj))
1132 return got_error(GOT_ERR_PRIVSEP_LEN);
1133 iobj = imsg->data;
1135 *obj = calloc(1, sizeof(**obj));
1136 if (*obj == NULL)
1137 return got_error_from_errno("calloc");
1139 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1140 (*obj)->type = iobj->type;
1141 (*obj)->flags = iobj->flags;
1142 (*obj)->hdrlen = iobj->hdrlen;
1143 (*obj)->size = iobj->size;
1144 /* path_packfile is handled by caller */
1145 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1146 (*obj)->pack_offset = iobj->pack_offset;
1147 (*obj)->pack_idx = iobj->pack_idx;
1149 STAILQ_INIT(&(*obj)->deltas.entries);
1150 return NULL;
1153 const struct got_error *
1154 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1156 const struct got_error *err = NULL;
1157 struct imsg imsg;
1158 const size_t min_datalen =
1159 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1161 *obj = NULL;
1163 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1164 if (err)
1165 return err;
1167 switch (imsg.hdr.type) {
1168 case GOT_IMSG_OBJECT:
1169 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1170 break;
1171 default:
1172 err = got_error(GOT_ERR_PRIVSEP_MSG);
1173 break;
1176 imsg_free(&imsg);
1178 return err;
1181 static const struct got_error *
1182 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1183 size_t logmsg_len)
1185 const struct got_error *err = NULL;
1186 size_t offset, remain;
1188 offset = 0;
1189 remain = logmsg_len;
1190 while (remain > 0) {
1191 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1193 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1194 commit->logmsg + offset, n) == -1) {
1195 err = got_error_from_errno("imsg_compose "
1196 "COMMIT_LOGMSG");
1197 break;
1200 err = flush_imsg(ibuf);
1201 if (err)
1202 break;
1204 offset += n;
1205 remain -= n;
1208 return err;
1211 const struct got_error *
1212 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1214 const struct got_error *err = NULL;
1215 struct got_imsg_commit_object *icommit;
1216 uint8_t *buf;
1217 size_t len, total;
1218 struct got_object_qid *qid;
1219 size_t author_len = strlen(commit->author);
1220 size_t committer_len = strlen(commit->committer);
1221 size_t logmsg_len = strlen(commit->logmsg);
1223 total = sizeof(*icommit) + author_len + committer_len +
1224 commit->nparents * SHA1_DIGEST_LENGTH;
1226 buf = malloc(total);
1227 if (buf == NULL)
1228 return got_error_from_errno("malloc");
1230 icommit = (struct got_imsg_commit_object *)buf;
1231 memcpy(icommit->tree_id, commit->tree_id->sha1,
1232 sizeof(icommit->tree_id));
1233 icommit->author_len = author_len;
1234 icommit->author_time = commit->author_time;
1235 icommit->author_gmtoff = commit->author_gmtoff;
1236 icommit->committer_len = committer_len;
1237 icommit->committer_time = commit->committer_time;
1238 icommit->committer_gmtoff = commit->committer_gmtoff;
1239 icommit->logmsg_len = logmsg_len;
1240 icommit->nparents = commit->nparents;
1242 len = sizeof(*icommit);
1243 memcpy(buf + len, commit->author, author_len);
1244 len += author_len;
1245 memcpy(buf + len, commit->committer, committer_len);
1246 len += committer_len;
1247 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1248 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1249 len += SHA1_DIGEST_LENGTH;
1252 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1253 err = got_error_from_errno("imsg_compose COMMIT");
1254 goto done;
1257 if (logmsg_len == 0 ||
1258 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1259 err = flush_imsg(ibuf);
1260 if (err)
1261 goto done;
1263 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1264 done:
1265 free(buf);
1266 return err;
1269 static const struct got_error *
1270 get_commit_from_imsg(struct got_commit_object **commit,
1271 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1273 const struct got_error *err = NULL;
1274 struct got_imsg_commit_object *icommit;
1275 size_t len = 0;
1276 int i;
1278 if (datalen < sizeof(*icommit))
1279 return got_error(GOT_ERR_PRIVSEP_LEN);
1281 icommit = imsg->data;
1282 if (datalen != sizeof(*icommit) + icommit->author_len +
1283 icommit->committer_len +
1284 icommit->nparents * SHA1_DIGEST_LENGTH)
1285 return got_error(GOT_ERR_PRIVSEP_LEN);
1287 if (icommit->nparents < 0)
1288 return got_error(GOT_ERR_PRIVSEP_LEN);
1290 len += sizeof(*icommit);
1292 *commit = got_object_commit_alloc_partial();
1293 if (*commit == NULL)
1294 return got_error_from_errno(
1295 "got_object_commit_alloc_partial");
1297 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1298 SHA1_DIGEST_LENGTH);
1299 (*commit)->author_time = icommit->author_time;
1300 (*commit)->author_gmtoff = icommit->author_gmtoff;
1301 (*commit)->committer_time = icommit->committer_time;
1302 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1304 (*commit)->author = strndup(imsg->data + len, icommit->author_len);
1305 if ((*commit)->author == NULL) {
1306 err = got_error_from_errno("strndup");
1307 goto done;
1309 len += icommit->author_len;
1311 (*commit)->committer = strndup(imsg->data + len,
1312 icommit->committer_len);
1313 if ((*commit)->committer == NULL) {
1314 err = got_error_from_errno("strndup");
1315 goto done;
1317 len += icommit->committer_len;
1319 if (icommit->logmsg_len == 0) {
1320 (*commit)->logmsg = strdup("");
1321 if ((*commit)->logmsg == NULL) {
1322 err = got_error_from_errno("strdup");
1323 goto done;
1325 } else {
1326 size_t offset = 0, remain = icommit->logmsg_len;
1328 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1329 if ((*commit)->logmsg == NULL) {
1330 err = got_error_from_errno("malloc");
1331 goto done;
1333 while (remain > 0) {
1334 struct imsg imsg_log;
1335 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1336 remain);
1338 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1339 if (err)
1340 goto done;
1342 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1343 err = got_error(GOT_ERR_PRIVSEP_MSG);
1344 goto done;
1347 memcpy((*commit)->logmsg + offset,
1348 imsg_log.data, n);
1349 imsg_free(&imsg_log);
1350 offset += n;
1351 remain -= n;
1353 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1356 for (i = 0; i < icommit->nparents; i++) {
1357 struct got_object_qid *qid;
1359 err = got_object_qid_alloc_partial(&qid);
1360 if (err)
1361 break;
1362 memcpy(&qid->id, imsg->data + len +
1363 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1364 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1365 (*commit)->nparents++;
1367 done:
1368 if (err) {
1369 got_object_commit_close(*commit);
1370 *commit = NULL;
1372 return err;
1375 const struct got_error *
1376 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1378 const struct got_error *err = NULL;
1379 struct imsg imsg;
1380 size_t datalen;
1381 const size_t min_datalen =
1382 MIN(sizeof(struct got_imsg_error),
1383 sizeof(struct got_imsg_commit_object));
1385 *commit = NULL;
1387 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1388 if (err)
1389 return err;
1391 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1393 switch (imsg.hdr.type) {
1394 case GOT_IMSG_COMMIT:
1395 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1396 break;
1397 default:
1398 err = got_error(GOT_ERR_PRIVSEP_MSG);
1399 break;
1402 imsg_free(&imsg);
1404 return err;
1407 static const struct got_error *
1408 send_tree_entries_batch(struct imsgbuf *ibuf,
1409 struct got_parsed_tree_entry *entries, int idx0, int idxN, size_t len)
1411 struct ibuf *wbuf;
1412 struct got_imsg_tree_entries ientries;
1413 int i;
1415 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1416 if (wbuf == NULL)
1417 return got_error_from_errno("imsg_create TREE_ENTRY");
1419 ientries.nentries = idxN - idx0 + 1;
1420 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1421 return got_error_from_errno("imsg_add TREE_ENTRY");
1423 for (i = idx0; i <= idxN; i++) {
1424 struct got_parsed_tree_entry *pte = &entries[i];
1426 /* Keep in sync with struct got_imsg_tree_object definition! */
1427 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1428 return got_error_from_errno("imsg_add TREE_ENTRY");
1429 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1430 return got_error_from_errno("imsg_add TREE_ENTRY");
1431 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1432 return got_error_from_errno("imsg_add TREE_ENTRY");
1434 /* Remaining bytes are the entry's name. */
1435 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1436 return got_error_from_errno("imsg_add TREE_ENTRY");
1439 wbuf->fd = -1;
1440 imsg_close(ibuf, wbuf);
1441 return NULL;
1444 static const struct got_error *
1445 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1446 int nentries)
1448 const struct got_error *err = NULL;
1449 int i, j;
1450 size_t entries_len = sizeof(struct got_imsg_tree_entries);
1452 i = 0;
1453 for (j = 0; j < nentries; j++) {
1454 struct got_parsed_tree_entry *pte = &entries[j];
1455 size_t len = sizeof(*pte) + pte->namelen;
1457 if (j > 0 &&
1458 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1459 err = send_tree_entries_batch(ibuf, entries,
1460 i, j - 1, entries_len);
1461 if (err)
1462 return err;
1463 i = j;
1464 entries_len = sizeof(struct got_imsg_tree_entries);
1467 entries_len += len;
1470 if (j > 0) {
1471 err = send_tree_entries_batch(ibuf, entries, i, j - 1,
1472 entries_len);
1473 if (err)
1474 return err;
1477 return NULL;
1480 const struct got_error *
1481 got_privsep_send_tree(struct imsgbuf *ibuf,
1482 struct got_parsed_tree_entry *entries, int nentries)
1484 const struct got_error *err = NULL;
1485 struct got_imsg_tree_object itree;
1487 itree.nentries = nentries;
1488 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1489 == -1)
1490 return got_error_from_errno("imsg_compose TREE");
1492 err = send_tree_entries(ibuf, entries, nentries);
1493 if (err)
1494 return err;
1496 return flush_imsg(ibuf);
1500 static const struct got_error *
1501 recv_tree_entries(void *data, size_t datalen, struct got_tree_object *tree,
1502 int *nentries)
1504 const struct got_error *err = NULL;
1505 struct got_imsg_tree_entries *ientries;
1506 struct got_tree_entry *te;
1507 size_t te_offset;
1508 size_t i;
1510 if (datalen <= sizeof(*ientries) ||
1511 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1512 return got_error(GOT_ERR_PRIVSEP_LEN);
1514 ientries = (struct got_imsg_tree_entries *)data;
1515 if (ientries->nentries > INT_MAX) {
1516 return got_error_msg(GOT_ERR_NO_SPACE,
1517 "too many tree entries");
1520 te_offset = sizeof(*ientries);
1521 for (i = 0; i < ientries->nentries; i++) {
1522 struct got_imsg_tree_entry ite;
1523 const char *te_name;
1524 uint8_t *buf = (uint8_t *)data + te_offset;
1526 if (te_offset >= datalen) {
1527 err = got_error(GOT_ERR_PRIVSEP_LEN);
1528 break;
1531 /* Might not be aligned, size is ~32 bytes. */
1532 memcpy(&ite, buf, sizeof(ite));
1534 if (ite.namelen >= sizeof(te->name)) {
1535 err = got_error(GOT_ERR_PRIVSEP_LEN);
1536 break;
1538 if (te_offset + sizeof(ite) + ite.namelen > datalen) {
1539 err = got_error(GOT_ERR_PRIVSEP_LEN);
1540 break;
1543 if (*nentries >= tree->nentries) {
1544 err = got_error(GOT_ERR_PRIVSEP_LEN);
1545 break;
1547 te = &tree->entries[*nentries];
1548 te_name = buf + sizeof(ite);
1549 memcpy(te->name, te_name, ite.namelen);
1550 te->name[ite.namelen] = '\0';
1551 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1552 te->mode = ite.mode;
1553 te->idx = *nentries;
1554 (*nentries)++;
1556 te_offset += sizeof(ite) + ite.namelen;
1559 return err;
1562 const struct got_error *
1563 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1565 const struct got_error *err = NULL;
1566 const size_t min_datalen =
1567 MIN(sizeof(struct got_imsg_error),
1568 sizeof(struct got_imsg_tree_object));
1569 struct got_imsg_tree_object *itree;
1570 int nentries = 0;
1572 *tree = NULL;
1574 err = read_imsg(ibuf);
1575 if (err)
1576 goto done;
1578 for (;;) {
1579 struct imsg imsg;
1580 size_t n;
1581 size_t datalen;
1583 n = imsg_get(ibuf, &imsg);
1584 if (n == 0) {
1585 if ((*tree)) {
1586 if (nentries < (*tree)->nentries) {
1587 err = read_imsg(ibuf);
1588 if (err)
1589 break;
1590 continue;
1591 } else
1592 break;
1593 } else {
1594 err = got_error(GOT_ERR_PRIVSEP_MSG);
1595 break;
1599 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1600 imsg_free(&imsg);
1601 err = got_error(GOT_ERR_PRIVSEP_LEN);
1602 break;
1605 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1607 switch (imsg.hdr.type) {
1608 case GOT_IMSG_ERROR:
1609 err = recv_imsg_error(&imsg, datalen);
1610 break;
1611 case GOT_IMSG_TREE:
1612 /* This message should only appear once. */
1613 if (*tree != NULL) {
1614 err = got_error(GOT_ERR_PRIVSEP_MSG);
1615 break;
1617 if (datalen != sizeof(*itree)) {
1618 err = got_error(GOT_ERR_PRIVSEP_LEN);
1619 break;
1621 itree = imsg.data;
1622 if (itree->nentries < 0) {
1623 err = got_error(GOT_ERR_PRIVSEP_LEN);
1624 break;
1626 *tree = malloc(sizeof(**tree));
1627 if (*tree == NULL) {
1628 err = got_error_from_errno("malloc");
1629 break;
1631 (*tree)->entries = calloc(itree->nentries,
1632 sizeof(struct got_tree_entry));
1633 if ((*tree)->entries == NULL) {
1634 err = got_error_from_errno("malloc");
1635 free(*tree);
1636 *tree = NULL;
1637 break;
1639 (*tree)->nentries = itree->nentries;
1640 (*tree)->refcnt = 0;
1641 break;
1642 case GOT_IMSG_TREE_ENTRIES:
1643 /* This message should be preceeded by GOT_IMSG_TREE. */
1644 if (*tree == NULL) {
1645 err = got_error(GOT_ERR_PRIVSEP_MSG);
1646 break;
1648 err = recv_tree_entries(imsg.data, datalen,
1649 *tree, &nentries);
1650 break;
1651 default:
1652 err = got_error(GOT_ERR_PRIVSEP_MSG);
1653 break;
1656 imsg_free(&imsg);
1657 if (err)
1658 break;
1660 done:
1661 if (*tree && (*tree)->nentries != nentries) {
1662 if (err == NULL)
1663 err = got_error(GOT_ERR_PRIVSEP_LEN);
1664 got_object_tree_close(*tree);
1665 *tree = NULL;
1668 return err;
1671 const struct got_error *
1672 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1673 const uint8_t *data)
1675 struct got_imsg_blob iblob;
1677 iblob.size = size;
1678 iblob.hdrlen = hdrlen;
1680 if (data) {
1681 uint8_t *buf;
1683 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1684 return got_error(GOT_ERR_NO_SPACE);
1686 buf = malloc(sizeof(iblob) + size);
1687 if (buf == NULL)
1688 return got_error_from_errno("malloc");
1690 memcpy(buf, &iblob, sizeof(iblob));
1691 memcpy(buf + sizeof(iblob), data, size);
1692 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1693 sizeof(iblob) + size) == -1) {
1694 free(buf);
1695 return got_error_from_errno("imsg_compose BLOB");
1697 free(buf);
1698 } else {
1699 /* Data has already been written to file descriptor. */
1700 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1701 sizeof(iblob)) == -1)
1702 return got_error_from_errno("imsg_compose BLOB");
1706 return flush_imsg(ibuf);
1709 const struct got_error *
1710 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1711 struct imsgbuf *ibuf)
1713 const struct got_error *err = NULL;
1714 struct imsg imsg;
1715 struct got_imsg_blob *iblob;
1716 size_t datalen;
1718 *outbuf = NULL;
1720 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1721 if (err)
1722 return err;
1724 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1726 switch (imsg.hdr.type) {
1727 case GOT_IMSG_BLOB:
1728 if (datalen < sizeof(*iblob)) {
1729 err = got_error(GOT_ERR_PRIVSEP_LEN);
1730 break;
1732 iblob = imsg.data;
1733 *size = iblob->size;
1734 *hdrlen = iblob->hdrlen;
1736 if (datalen == sizeof(*iblob)) {
1737 /* Data has been written to file descriptor. */
1738 break;
1741 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1742 err = got_error(GOT_ERR_PRIVSEP_LEN);
1743 break;
1746 *outbuf = malloc(*size);
1747 if (*outbuf == NULL) {
1748 err = got_error_from_errno("malloc");
1749 break;
1751 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1752 break;
1753 default:
1754 err = got_error(GOT_ERR_PRIVSEP_MSG);
1755 break;
1758 imsg_free(&imsg);
1760 return err;
1763 static const struct got_error *
1764 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1766 const struct got_error *err = NULL;
1767 size_t offset, remain;
1769 offset = 0;
1770 remain = tagmsg_len;
1771 while (remain > 0) {
1772 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1774 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1775 tag->tagmsg + offset, n) == -1) {
1776 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1777 break;
1780 err = flush_imsg(ibuf);
1781 if (err)
1782 break;
1784 offset += n;
1785 remain -= n;
1788 return err;
1791 const struct got_error *
1792 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1794 const struct got_error *err = NULL;
1795 struct got_imsg_tag_object *itag;
1796 uint8_t *buf;
1797 size_t len, total;
1798 size_t tag_len = strlen(tag->tag);
1799 size_t tagger_len = strlen(tag->tagger);
1800 size_t tagmsg_len = strlen(tag->tagmsg);
1802 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1804 buf = malloc(total);
1805 if (buf == NULL)
1806 return got_error_from_errno("malloc");
1808 itag = (struct got_imsg_tag_object *)buf;
1809 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1810 itag->obj_type = tag->obj_type;
1811 itag->tag_len = tag_len;
1812 itag->tagger_len = tagger_len;
1813 itag->tagger_time = tag->tagger_time;
1814 itag->tagger_gmtoff = tag->tagger_gmtoff;
1815 itag->tagmsg_len = tagmsg_len;
1817 len = sizeof(*itag);
1818 memcpy(buf + len, tag->tag, tag_len);
1819 len += tag_len;
1820 memcpy(buf + len, tag->tagger, tagger_len);
1821 len += tagger_len;
1823 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1824 err = got_error_from_errno("imsg_compose TAG");
1825 goto done;
1828 if (tagmsg_len == 0 ||
1829 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1830 err = flush_imsg(ibuf);
1831 if (err)
1832 goto done;
1834 err = send_tagmsg(ibuf, tag, tagmsg_len);
1835 done:
1836 free(buf);
1837 return err;
1840 const struct got_error *
1841 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1843 const struct got_error *err = NULL;
1844 struct imsg imsg;
1845 struct got_imsg_tag_object *itag;
1846 size_t len, datalen;
1847 const size_t min_datalen =
1848 MIN(sizeof(struct got_imsg_error),
1849 sizeof(struct got_imsg_tag_object));
1851 *tag = NULL;
1853 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1854 if (err)
1855 return err;
1857 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1858 len = 0;
1860 switch (imsg.hdr.type) {
1861 case GOT_IMSG_TAG:
1862 if (datalen < sizeof(*itag)) {
1863 err = got_error(GOT_ERR_PRIVSEP_LEN);
1864 break;
1866 itag = imsg.data;
1867 if (datalen != sizeof(*itag) + itag->tag_len +
1868 itag->tagger_len) {
1869 err = got_error(GOT_ERR_PRIVSEP_LEN);
1870 break;
1872 len += sizeof(*itag);
1874 *tag = calloc(1, sizeof(**tag));
1875 if (*tag == NULL) {
1876 err = got_error_from_errno("calloc");
1877 break;
1880 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1882 (*tag)->tag = strndup(imsg.data + len, itag->tag_len);
1883 if ((*tag)->tag == NULL) {
1884 err = got_error_from_errno("strndup");
1885 break;
1887 len += itag->tag_len;
1889 (*tag)->obj_type = itag->obj_type;
1890 (*tag)->tagger_time = itag->tagger_time;
1891 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1893 (*tag)->tagger = strndup(imsg.data + len, itag->tagger_len);
1894 if ((*tag)->tagger == NULL) {
1895 err = got_error_from_errno("strndup");
1896 break;
1898 len += itag->tagger_len;
1900 if (itag->tagmsg_len == 0) {
1901 (*tag)->tagmsg = strdup("");
1902 if ((*tag)->tagmsg == NULL) {
1903 err = got_error_from_errno("strdup");
1904 break;
1906 } else {
1907 size_t offset = 0, remain = itag->tagmsg_len;
1909 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1910 if ((*tag)->tagmsg == NULL) {
1911 err = got_error_from_errno("malloc");
1912 break;
1914 while (remain > 0) {
1915 struct imsg imsg_log;
1916 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1917 remain);
1919 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1920 if (err)
1921 return err;
1923 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1924 return got_error(GOT_ERR_PRIVSEP_MSG);
1926 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1927 n);
1928 imsg_free(&imsg_log);
1929 offset += n;
1930 remain -= n;
1932 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1935 break;
1936 default:
1937 err = got_error(GOT_ERR_PRIVSEP_MSG);
1938 break;
1941 imsg_free(&imsg);
1943 return err;
1946 const struct got_error *
1947 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1948 struct got_packidx *packidx)
1950 const struct got_error *err = NULL;
1951 struct got_imsg_packidx ipackidx;
1952 struct got_imsg_pack ipack;
1953 int fd;
1955 ipackidx.len = packidx->len;
1956 ipackidx.packfile_size = pack->filesize;
1957 fd = dup(packidx->fd);
1958 if (fd == -1)
1959 return got_error_from_errno("dup");
1961 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1962 sizeof(ipackidx)) == -1) {
1963 err = got_error_from_errno("imsg_compose PACKIDX");
1964 close(fd);
1965 return err;
1968 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1969 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1970 return got_error(GOT_ERR_NO_SPACE);
1971 ipack.filesize = pack->filesize;
1973 fd = dup(pack->fd);
1974 if (fd == -1)
1975 return got_error_from_errno("dup");
1977 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1978 == -1) {
1979 err = got_error_from_errno("imsg_compose PACK");
1980 close(fd);
1981 return err;
1984 return flush_imsg(ibuf);
1987 const struct got_error *
1988 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1989 struct got_object_id *id)
1991 struct got_imsg_packed_object iobj;
1993 iobj.idx = idx;
1994 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1996 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1997 &iobj, sizeof(iobj)) == -1)
1998 return got_error_from_errno("imsg_compose "
1999 "PACKED_OBJECT_REQUEST");
2001 return flush_imsg(ibuf);
2004 const struct got_error *
2005 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2006 struct got_object_id *id)
2008 struct got_imsg_packed_object iobj;
2010 iobj.idx = idx;
2011 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2013 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2014 &iobj, sizeof(iobj)) == -1)
2015 return got_error_from_errno("imsg_compose "
2016 "PACKED_OBJECT_REQUEST");
2018 return flush_imsg(ibuf);
2021 const struct got_error *
2022 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2024 const struct got_error *err = NULL;
2026 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2027 NULL, 0) == -1) {
2028 err = got_error_from_errno("imsg_compose "
2029 "GITCONFIG_PARSE_REQUEST");
2030 close(fd);
2031 return err;
2034 return flush_imsg(ibuf);
2037 const struct got_error *
2038 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2040 if (imsg_compose(ibuf,
2041 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2042 NULL, 0) == -1)
2043 return got_error_from_errno("imsg_compose "
2044 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2046 return flush_imsg(ibuf);
2049 const struct got_error *
2050 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2052 if (imsg_compose(ibuf,
2053 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2054 NULL, 0) == -1)
2055 return got_error_from_errno("imsg_compose "
2056 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2058 return flush_imsg(ibuf);
2062 const struct got_error *
2063 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2065 if (imsg_compose(ibuf,
2066 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2067 return got_error_from_errno("imsg_compose "
2068 "GITCONFIG_AUTHOR_NAME_REQUEST");
2070 return flush_imsg(ibuf);
2073 const struct got_error *
2074 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2076 if (imsg_compose(ibuf,
2077 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2078 return got_error_from_errno("imsg_compose "
2079 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2081 return flush_imsg(ibuf);
2084 const struct got_error *
2085 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2087 if (imsg_compose(ibuf,
2088 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2089 return got_error_from_errno("imsg_compose "
2090 "GITCONFIG_REMOTE_REQUEST");
2092 return flush_imsg(ibuf);
2095 const struct got_error *
2096 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2098 if (imsg_compose(ibuf,
2099 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2100 return got_error_from_errno("imsg_compose "
2101 "GITCONFIG_OWNER_REQUEST");
2103 return flush_imsg(ibuf);
2106 const struct got_error *
2107 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2109 const struct got_error *err = NULL;
2110 struct imsg imsg;
2111 size_t datalen;
2113 *str = NULL;
2115 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2116 if (err)
2117 return err;
2118 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2120 switch (imsg.hdr.type) {
2121 case GOT_IMSG_GITCONFIG_STR_VAL:
2122 if (datalen == 0)
2123 break;
2124 /* datalen does not include terminating \0 */
2125 *str = malloc(datalen + 1);
2126 if (*str == NULL) {
2127 err = got_error_from_errno("malloc");
2128 break;
2130 memcpy(*str, imsg.data, datalen);
2131 (*str)[datalen] = '\0';
2132 break;
2133 default:
2134 err = got_error(GOT_ERR_PRIVSEP_MSG);
2135 break;
2138 imsg_free(&imsg);
2139 return err;
2142 const struct got_error *
2143 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2145 const struct got_error *err = NULL;
2146 struct imsg imsg;
2147 size_t datalen;
2148 const size_t min_datalen =
2149 MIN(sizeof(struct got_imsg_error), sizeof(int));
2151 *val = 0;
2153 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2154 if (err)
2155 return err;
2156 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2158 switch (imsg.hdr.type) {
2159 case GOT_IMSG_GITCONFIG_INT_VAL:
2160 if (datalen != sizeof(*val)) {
2161 err = got_error(GOT_ERR_PRIVSEP_LEN);
2162 break;
2164 memcpy(val, imsg.data, sizeof(*val));
2165 break;
2166 default:
2167 err = got_error(GOT_ERR_PRIVSEP_MSG);
2168 break;
2171 imsg_free(&imsg);
2172 return err;
2175 static void
2176 free_remote_data(struct got_remote_repo *remote)
2178 int i;
2180 free(remote->name);
2181 free(remote->fetch_url);
2182 free(remote->send_url);
2183 for (i = 0; i < remote->nfetch_branches; i++)
2184 free(remote->fetch_branches[i]);
2185 free(remote->fetch_branches);
2186 for (i = 0; i < remote->nsend_branches; i++)
2187 free(remote->send_branches[i]);
2188 free(remote->send_branches);
2189 for (i = 0; i < remote->nfetch_refs; i++)
2190 free(remote->fetch_refs[i]);
2191 free(remote->fetch_refs);
2194 const struct got_error *
2195 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2196 int *nremotes, struct imsgbuf *ibuf)
2198 const struct got_error *err = NULL;
2199 struct imsg imsg;
2200 size_t datalen;
2201 struct got_imsg_remotes iremotes;
2202 struct got_imsg_remote iremote;
2204 *remotes = NULL;
2205 *nremotes = 0;
2206 iremotes.nremotes = 0;
2208 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2209 if (err)
2210 return err;
2211 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2213 switch (imsg.hdr.type) {
2214 case GOT_IMSG_GITCONFIG_REMOTES:
2215 if (datalen != sizeof(iremotes)) {
2216 err = got_error(GOT_ERR_PRIVSEP_LEN);
2217 break;
2219 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2220 if (iremotes.nremotes == 0) {
2221 imsg_free(&imsg);
2222 return NULL;
2224 break;
2225 default:
2226 imsg_free(&imsg);
2227 return got_error(GOT_ERR_PRIVSEP_MSG);
2230 imsg_free(&imsg);
2232 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2233 if (*remotes == NULL)
2234 return got_error_from_errno("recallocarray");
2236 while (*nremotes < iremotes.nremotes) {
2237 struct got_remote_repo *remote;
2239 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2240 if (err)
2241 break;
2242 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2244 switch (imsg.hdr.type) {
2245 case GOT_IMSG_GITCONFIG_REMOTE:
2246 remote = &(*remotes)[*nremotes];
2247 memset(remote, 0, sizeof(*remote));
2248 if (datalen < sizeof(iremote)) {
2249 err = got_error(GOT_ERR_PRIVSEP_LEN);
2250 break;
2252 memcpy(&iremote, imsg.data, sizeof(iremote));
2253 if (iremote.name_len == 0 ||
2254 iremote.fetch_url_len == 0 ||
2255 iremote.send_url_len == 0 ||
2256 (sizeof(iremote) + iremote.name_len +
2257 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2258 err = got_error(GOT_ERR_PRIVSEP_LEN);
2259 break;
2261 remote->name = strndup(imsg.data + sizeof(iremote),
2262 iremote.name_len);
2263 if (remote->name == NULL) {
2264 err = got_error_from_errno("strndup");
2265 break;
2267 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2268 iremote.name_len, iremote.fetch_url_len);
2269 if (remote->fetch_url == NULL) {
2270 err = got_error_from_errno("strndup");
2271 free_remote_data(remote);
2272 break;
2274 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2275 iremote.name_len + iremote.fetch_url_len,
2276 iremote.send_url_len);
2277 if (remote->send_url == NULL) {
2278 err = got_error_from_errno("strndup");
2279 free_remote_data(remote);
2280 break;
2282 remote->mirror_references = iremote.mirror_references;
2283 remote->fetch_all_branches = iremote.fetch_all_branches;
2284 remote->nfetch_branches = 0;
2285 remote->fetch_branches = NULL;
2286 remote->nsend_branches = 0;
2287 remote->send_branches = NULL;
2288 remote->nfetch_refs = 0;
2289 remote->fetch_refs = NULL;
2290 (*nremotes)++;
2291 break;
2292 default:
2293 err = got_error(GOT_ERR_PRIVSEP_MSG);
2294 break;
2297 imsg_free(&imsg);
2298 if (err)
2299 break;
2302 if (err) {
2303 int i;
2304 for (i = 0; i < *nremotes; i++)
2305 free_remote_data(&(*remotes)[i]);
2306 free(*remotes);
2307 *remotes = NULL;
2308 *nremotes = 0;
2310 return err;
2313 const struct got_error *
2314 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2316 const struct got_error *err = NULL;
2318 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2319 NULL, 0) == -1) {
2320 err = got_error_from_errno("imsg_compose "
2321 "GOTCONFIG_PARSE_REQUEST");
2322 close(fd);
2323 return err;
2326 return flush_imsg(ibuf);
2329 const struct got_error *
2330 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2332 if (imsg_compose(ibuf,
2333 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2334 return got_error_from_errno("imsg_compose "
2335 "GOTCONFIG_AUTHOR_REQUEST");
2337 return flush_imsg(ibuf);
2340 const struct got_error *
2341 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2343 if (imsg_compose(ibuf,
2344 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2345 return got_error_from_errno("imsg_compose "
2346 "GOTCONFIG_REMOTE_REQUEST");
2348 return flush_imsg(ibuf);
2351 const struct got_error *
2352 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2354 const struct got_error *err = NULL;
2355 struct imsg imsg;
2356 size_t datalen;
2358 *str = NULL;
2360 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2361 if (err)
2362 return err;
2363 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2365 switch (imsg.hdr.type) {
2366 case GOT_IMSG_ERROR:
2367 err = recv_imsg_error(&imsg, datalen);
2368 break;
2369 case GOT_IMSG_GOTCONFIG_STR_VAL:
2370 if (datalen == 0)
2371 break;
2372 /* datalen does not include terminating \0 */
2373 *str = malloc(datalen + 1);
2374 if (*str == NULL) {
2375 err = got_error_from_errno("malloc");
2376 break;
2378 memcpy(*str, imsg.data, datalen);
2379 (*str)[datalen] = '\0';
2380 break;
2381 default:
2382 err = got_error(GOT_ERR_PRIVSEP_MSG);
2383 break;
2386 imsg_free(&imsg);
2387 return err;
2390 const struct got_error *
2391 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2392 int *nremotes, struct imsgbuf *ibuf)
2394 const struct got_error *err = NULL;
2395 struct imsg imsg;
2396 size_t datalen;
2397 struct got_imsg_remotes iremotes;
2398 struct got_imsg_remote iremote;
2399 const size_t min_datalen =
2400 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2402 *remotes = NULL;
2403 *nremotes = 0;
2404 iremotes.nremotes = 0;
2406 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2407 if (err)
2408 return err;
2409 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2411 switch (imsg.hdr.type) {
2412 case GOT_IMSG_ERROR:
2413 err = recv_imsg_error(&imsg, datalen);
2414 break;
2415 case GOT_IMSG_GOTCONFIG_REMOTES:
2416 if (datalen != sizeof(iremotes)) {
2417 err = got_error(GOT_ERR_PRIVSEP_LEN);
2418 break;
2420 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2421 if (iremotes.nremotes == 0) {
2422 imsg_free(&imsg);
2423 return NULL;
2425 break;
2426 default:
2427 imsg_free(&imsg);
2428 return got_error(GOT_ERR_PRIVSEP_MSG);
2431 imsg_free(&imsg);
2433 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2434 if (*remotes == NULL)
2435 return got_error_from_errno("recallocarray");
2437 while (*nremotes < iremotes.nremotes) {
2438 struct got_remote_repo *remote;
2439 const size_t min_datalen =
2440 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2441 int i;
2443 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2444 if (err)
2445 break;
2446 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2448 switch (imsg.hdr.type) {
2449 case GOT_IMSG_ERROR:
2450 err = recv_imsg_error(&imsg, datalen);
2451 break;
2452 case GOT_IMSG_GOTCONFIG_REMOTE:
2453 remote = &(*remotes)[*nremotes];
2454 memset(remote, 0, sizeof(*remote));
2455 if (datalen < sizeof(iremote)) {
2456 err = got_error(GOT_ERR_PRIVSEP_LEN);
2457 break;
2459 memcpy(&iremote, imsg.data, sizeof(iremote));
2460 if (iremote.name_len == 0 ||
2461 (iremote.fetch_url_len == 0 &&
2462 iremote.send_url_len == 0) ||
2463 (sizeof(iremote) + iremote.name_len +
2464 iremote.fetch_url_len + iremote.send_url_len) >
2465 datalen) {
2466 err = got_error(GOT_ERR_PRIVSEP_LEN);
2467 break;
2469 remote->name = strndup(imsg.data + sizeof(iremote),
2470 iremote.name_len);
2471 if (remote->name == NULL) {
2472 err = got_error_from_errno("strndup");
2473 break;
2475 remote->fetch_url = strndup(imsg.data +
2476 sizeof(iremote) + iremote.name_len,
2477 iremote.fetch_url_len);
2478 if (remote->fetch_url == NULL) {
2479 err = got_error_from_errno("strndup");
2480 free_remote_data(remote);
2481 break;
2483 remote->send_url = strndup(imsg.data +
2484 sizeof(iremote) + iremote.name_len +
2485 iremote.fetch_url_len, iremote.send_url_len);
2486 if (remote->send_url == NULL) {
2487 err = got_error_from_errno("strndup");
2488 free_remote_data(remote);
2489 break;
2491 remote->mirror_references = iremote.mirror_references;
2492 remote->fetch_all_branches = iremote.fetch_all_branches;
2493 if (iremote.nfetch_branches > 0) {
2494 remote->fetch_branches = recallocarray(NULL, 0,
2495 iremote.nfetch_branches, sizeof(char *));
2496 if (remote->fetch_branches == NULL) {
2497 err = got_error_from_errno("calloc");
2498 free_remote_data(remote);
2499 break;
2502 remote->nfetch_branches = 0;
2503 for (i = 0; i < iremote.nfetch_branches; i++) {
2504 char *branch;
2505 err = got_privsep_recv_gotconfig_str(&branch,
2506 ibuf);
2507 if (err) {
2508 free_remote_data(remote);
2509 goto done;
2511 remote->fetch_branches[i] = branch;
2512 remote->nfetch_branches++;
2514 if (iremote.nsend_branches > 0) {
2515 remote->send_branches = recallocarray(NULL, 0,
2516 iremote.nsend_branches, sizeof(char *));
2517 if (remote->send_branches == NULL) {
2518 err = got_error_from_errno("calloc");
2519 free_remote_data(remote);
2520 break;
2523 remote->nsend_branches = 0;
2524 for (i = 0; i < iremote.nsend_branches; i++) {
2525 char *branch;
2526 err = got_privsep_recv_gotconfig_str(&branch,
2527 ibuf);
2528 if (err) {
2529 free_remote_data(remote);
2530 goto done;
2532 remote->send_branches[i] = branch;
2533 remote->nsend_branches++;
2535 if (iremote.nfetch_refs > 0) {
2536 remote->fetch_refs = recallocarray(NULL, 0,
2537 iremote.nfetch_refs, sizeof(char *));
2538 if (remote->fetch_refs == NULL) {
2539 err = got_error_from_errno("calloc");
2540 free_remote_data(remote);
2541 break;
2544 remote->nfetch_refs = 0;
2545 for (i = 0; i < iremote.nfetch_refs; i++) {
2546 char *ref;
2547 err = got_privsep_recv_gotconfig_str(&ref,
2548 ibuf);
2549 if (err) {
2550 free_remote_data(remote);
2551 goto done;
2553 remote->fetch_refs[i] = ref;
2554 remote->nfetch_refs++;
2556 (*nremotes)++;
2557 break;
2558 default:
2559 err = got_error(GOT_ERR_PRIVSEP_MSG);
2560 break;
2563 imsg_free(&imsg);
2564 if (err)
2565 break;
2567 done:
2568 if (err) {
2569 int i;
2570 for (i = 0; i < *nremotes; i++)
2571 free_remote_data(&(*remotes)[i]);
2572 free(*remotes);
2573 *remotes = NULL;
2574 *nremotes = 0;
2576 return err;
2579 const struct got_error *
2580 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2581 struct got_object_id *id, int idx, const char *path)
2583 struct ibuf *wbuf;
2584 size_t path_len = strlen(path) + 1;
2586 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2587 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2588 if (wbuf == NULL)
2589 return got_error_from_errno(
2590 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2591 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2592 return got_error_from_errno("imsg_add "
2593 "COMMIT_TRAVERSAL_REQUEST");
2594 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2595 return got_error_from_errno("imsg_add "
2596 "COMMIT_TRAVERSAL_REQUEST");
2597 if (imsg_add(wbuf, path, path_len) == -1)
2598 return got_error_from_errno("imsg_add "
2599 "COMMIT_TRAVERSAL_REQUEST");
2601 wbuf->fd = -1;
2602 imsg_close(ibuf, wbuf);
2604 return flush_imsg(ibuf);
2607 const struct got_error *
2608 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2609 struct got_object_id **changed_commit_id,
2610 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2612 const struct got_error *err = NULL;
2613 struct imsg imsg;
2614 struct got_imsg_traversed_commits *icommits;
2615 size_t datalen;
2616 int i, done = 0;
2618 *changed_commit = NULL;
2619 *changed_commit_id = NULL;
2621 while (!done) {
2622 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2623 if (err)
2624 return err;
2626 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2627 switch (imsg.hdr.type) {
2628 case GOT_IMSG_TRAVERSED_COMMITS:
2629 icommits = imsg.data;
2630 if (datalen != sizeof(*icommits) +
2631 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2632 err = got_error(GOT_ERR_PRIVSEP_LEN);
2633 break;
2635 for (i = 0; i < icommits->ncommits; i++) {
2636 struct got_object_qid *qid;
2637 uint8_t *sha1 = (uint8_t *)imsg.data +
2638 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2639 err = got_object_qid_alloc_partial(&qid);
2640 if (err)
2641 break;
2642 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2643 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2645 /* The last commit may contain a change. */
2646 if (i == icommits->ncommits - 1) {
2647 *changed_commit_id =
2648 got_object_id_dup(&qid->id);
2649 if (*changed_commit_id == NULL) {
2650 err = got_error_from_errno(
2651 "got_object_id_dup");
2652 break;
2656 break;
2657 case GOT_IMSG_COMMIT:
2658 if (*changed_commit_id == NULL) {
2659 err = got_error(GOT_ERR_PRIVSEP_MSG);
2660 break;
2662 err = get_commit_from_imsg(changed_commit, &imsg,
2663 datalen, ibuf);
2664 break;
2665 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2666 done = 1;
2667 break;
2668 default:
2669 err = got_error(GOT_ERR_PRIVSEP_MSG);
2670 break;
2673 imsg_free(&imsg);
2674 if (err)
2675 break;
2678 if (err)
2679 got_object_id_queue_free(commit_ids);
2680 return err;
2683 const struct got_error *
2684 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2685 struct got_object_id *tree_id, const char *path,
2686 struct got_parsed_tree_entry *entries, int nentries)
2688 const struct got_error *err = NULL;
2689 struct ibuf *wbuf;
2690 size_t path_len = strlen(path);
2691 size_t msglen;
2693 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2694 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2695 if (wbuf == NULL)
2696 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2698 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2699 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2700 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2701 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2702 if (imsg_add(wbuf, path, path_len) == -1)
2703 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2705 wbuf->fd = -1;
2706 imsg_close(ibuf, wbuf);
2708 if (entries) {
2709 err = send_tree_entries(ibuf, entries, nentries);
2710 if (err)
2711 return err;
2714 return flush_imsg(ibuf);
2717 const struct got_error *
2718 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2720 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2721 0, 0, -1, NULL, 0) == -1)
2722 return got_error_from_errno("imsg_compose "
2723 "OBJECT_ENUMERATION_REQUEST");
2725 return flush_imsg(ibuf);
2728 const struct got_error *
2729 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2731 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2732 0, 0, -1, NULL, 0) == -1)
2733 return got_error_from_errno("imsg_compose "
2734 "OBJECT_ENUMERATION_DONE");
2736 return flush_imsg(ibuf);
2739 const struct got_error *
2740 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2741 struct got_object_id *id, time_t mtime)
2743 struct ibuf *wbuf;
2745 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2746 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2747 if (wbuf == NULL)
2748 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2750 /* Keep in sync with struct got_imsg_enumerated_commit! */
2751 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2752 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2753 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2754 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2756 wbuf->fd = -1;
2757 imsg_close(ibuf, wbuf);
2758 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2759 return NULL;
2762 const struct got_error *
2763 got_privsep_recv_enumerated_objects(struct imsgbuf *ibuf,
2764 got_object_enumerate_commit_cb cb_commit,
2765 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2766 struct got_repository *repo)
2768 const struct got_error *err = NULL;
2769 struct imsg imsg;
2770 struct got_imsg_enumerated_commit *icommit = NULL;
2771 struct got_object_id commit_id;
2772 int have_commit = 0;
2773 time_t mtime = 0;
2774 struct got_tree_object tree;
2775 struct got_imsg_enumerated_tree *itree;
2776 struct got_object_id tree_id;
2777 char *path = NULL, *canon_path = NULL;
2778 size_t datalen, path_len;
2779 int nentries = -1;
2780 int done = 0;
2782 memset(&tree, 0, sizeof(tree));
2784 while (!done) {
2785 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2786 if (err)
2787 break;
2789 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2790 switch (imsg.hdr.type) {
2791 case GOT_IMSG_ENUMERATED_COMMIT:
2792 if (have_commit && nentries != -1) {
2793 err = got_error(GOT_ERR_PRIVSEP_MSG);
2794 break;
2796 if (datalen != sizeof(*icommit)) {
2797 err = got_error(GOT_ERR_PRIVSEP_LEN);
2798 break;
2800 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2801 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2802 mtime = icommit->mtime;
2803 have_commit = 1;
2804 break;
2805 case GOT_IMSG_ENUMERATED_TREE:
2806 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2807 if (!have_commit) {
2808 err = got_error(GOT_ERR_PRIVSEP_MSG);
2809 break;
2811 if (datalen < sizeof(*itree)) {
2812 err = got_error(GOT_ERR_PRIVSEP_LEN);
2813 break;
2815 itree = imsg.data;
2816 path_len = datalen - sizeof(*itree);
2817 if (path_len == 0) {
2818 err = got_error(GOT_ERR_PRIVSEP_LEN);
2819 break;
2821 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2822 free(path);
2823 path = malloc(path_len + 1);
2824 if (path == NULL) {
2825 err = got_error_from_errno("malloc");
2826 break;
2828 free(canon_path);
2829 canon_path = malloc(path_len + 1);
2830 if (canon_path == NULL) {
2831 err = got_error_from_errno("malloc");
2832 break;
2834 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2835 path_len);
2836 path[path_len] = '\0';
2837 if (!got_path_is_absolute(path)) {
2838 err = got_error(GOT_ERR_BAD_PATH);
2839 break;
2841 if (got_path_is_root_dir(path)) {
2842 /* XXX check what got_canonpath() does wrong */
2843 canon_path[0] = '/';
2844 canon_path[1] = '\0';
2845 } else {
2846 err = got_canonpath(path, canon_path,
2847 path_len + 1);
2848 if (err)
2849 break;
2851 if (strcmp(path, canon_path) != 0) {
2852 err = got_error(GOT_ERR_BAD_PATH);
2853 break;
2855 if (nentries != -1) {
2856 err = got_error(GOT_ERR_PRIVSEP_MSG);
2857 break;
2859 if (itree->nentries < -1) {
2860 err = got_error(GOT_ERR_PRIVSEP_MSG);
2861 break;
2863 if (itree->nentries == -1) {
2864 /* Tree was not found in pack file. */
2865 done = 1;
2866 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2867 path, repo);
2868 break;
2870 if (itree->nentries > INT_MAX) {
2871 err = got_error(GOT_ERR_PRIVSEP_LEN);
2872 break;
2874 tree.entries = calloc(itree->nentries,
2875 sizeof(struct got_tree_entry));
2876 if (tree.entries == NULL) {
2877 err = got_error_from_errno("calloc");
2878 break;
2880 if (itree->nentries == 0) {
2881 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2882 path, repo);
2883 if (err)
2884 break;
2886 /* Prepare for next tree. */
2887 free(tree.entries);
2888 memset(&tree, 0, sizeof(tree));
2889 nentries = -1;
2890 } else {
2891 tree.nentries = itree->nentries;
2892 nentries = 0;
2894 break;
2895 case GOT_IMSG_TREE_ENTRIES:
2896 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2897 if (nentries <= -1) {
2898 err = got_error(GOT_ERR_PRIVSEP_MSG);
2899 break;
2901 err = recv_tree_entries(imsg.data, datalen,
2902 &tree, &nentries);
2903 if (err)
2904 break;
2905 if (tree.nentries == nentries) {
2906 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2907 path, repo);
2908 if (err)
2909 break;
2911 /* Prepare for next tree. */
2912 free(tree.entries);
2913 memset(&tree, 0, sizeof(tree));
2914 nentries = -1;
2916 break;
2917 case GOT_IMSG_TREE_ENUMERATION_DONE:
2918 /* All trees have been found and traversed. */
2919 if (!have_commit || path == NULL || nentries != -1) {
2920 err = got_error(GOT_ERR_PRIVSEP_MSG);
2921 break;
2923 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2924 if (err)
2925 break;
2926 have_commit = 0;
2927 break;
2928 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2929 done = 1;
2930 break;
2931 default:
2932 err = got_error(GOT_ERR_PRIVSEP_MSG);
2933 break;
2936 imsg_free(&imsg);
2937 if (err)
2938 break;
2941 free(path);
2942 free(canon_path);
2943 free(tree.entries);
2944 return err;
2947 const struct got_error *
2948 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2949 struct got_object_id *id)
2951 struct got_imsg_raw_delta_request dreq;
2953 dreq.idx = idx;
2954 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2956 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2957 &dreq, sizeof(dreq)) == -1)
2958 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2960 return flush_imsg(ibuf);
2963 const struct got_error *
2964 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2966 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2969 const struct got_error *
2970 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2971 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2972 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2974 struct got_imsg_raw_delta idelta;
2975 int ret;
2977 idelta.base_size = base_size;
2978 idelta.result_size = result_size;
2979 idelta.delta_size = delta_size;
2980 idelta.delta_compressed_size = delta_compressed_size;
2981 idelta.delta_offset = delta_offset;
2982 idelta.delta_out_offset = delta_out_offset;
2983 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2985 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2986 &idelta, sizeof(idelta));
2987 if (ret == -1)
2988 return got_error_from_errno("imsg_compose RAW_DELTA");
2990 return flush_imsg(ibuf);
2993 const struct got_error *
2994 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2995 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2996 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
2998 const struct got_error *err = NULL;
2999 struct imsg imsg;
3000 struct got_imsg_raw_delta *delta;
3001 size_t datalen;
3003 *base_size = 0;
3004 *result_size = 0;
3005 *delta_size = 0;
3006 *delta_compressed_size = 0;
3007 *delta_offset = 0;
3008 *delta_out_offset = 0;
3009 *base_id = NULL;
3011 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3012 if (err)
3013 return err;
3015 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3017 switch (imsg.hdr.type) {
3018 case GOT_IMSG_RAW_DELTA:
3019 if (datalen != sizeof(*delta)) {
3020 err = got_error(GOT_ERR_PRIVSEP_LEN);
3021 break;
3023 delta = imsg.data;
3024 *base_size = delta->base_size;
3025 *result_size = delta->result_size;
3026 *delta_size = delta->delta_size;
3027 *delta_compressed_size = delta->delta_compressed_size;
3028 *delta_offset = delta->delta_offset;
3029 *delta_out_offset = delta->delta_out_offset;
3030 *base_id = calloc(1, sizeof(**base_id));
3031 if (*base_id == NULL) {
3032 err = got_error_from_errno("malloc");
3033 break;
3035 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3036 break;
3037 default:
3038 err = got_error(GOT_ERR_PRIVSEP_MSG);
3039 break;
3042 imsg_free(&imsg);
3044 if (err) {
3045 free(*base_id);
3046 *base_id = NULL;
3048 return err;
3051 static const struct got_error *
3052 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3054 const struct got_error *err = NULL;
3055 struct got_imsg_object_idlist idlist;
3056 struct ibuf *wbuf;
3057 size_t i;
3059 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3060 return got_error(GOT_ERR_NO_SPACE);
3062 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3063 sizeof(idlist) + nids * sizeof(**ids));
3064 if (wbuf == NULL) {
3065 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3066 return err;
3069 idlist.nids = nids;
3070 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3071 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3073 for (i = 0; i < nids; i++) {
3074 struct got_object_id *id = ids[i];
3075 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3076 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3079 wbuf->fd = -1;
3080 imsg_close(ibuf, wbuf);
3082 return flush_imsg(ibuf);
3085 const struct got_error *
3086 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3087 struct got_object_id **ids, size_t nids)
3089 const struct got_error *err = NULL;
3090 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3091 int i, j = 0;
3093 for (i = 0; i < nids; i++) {
3094 j = i % nitems(idlist);
3095 idlist[j] = ids[i];
3096 if (j >= nitems(idlist) - 1) {
3097 err = send_idlist(ibuf, idlist, j + 1);
3098 if (err)
3099 return err;
3100 j = 0;
3104 if (j > 0) {
3105 err = send_idlist(ibuf, idlist, j + 1);
3106 if (err)
3107 return err;
3110 return NULL;
3113 const struct got_error *
3114 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3116 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3117 == -1)
3118 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3120 return flush_imsg(ibuf);
3123 const struct got_error *
3124 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3125 size_t *nids, struct imsgbuf *ibuf)
3127 const struct got_error *err = NULL;
3128 struct imsg imsg;
3129 struct got_imsg_object_idlist *idlist;
3130 size_t datalen;
3132 *ids = NULL;
3133 *done = 0;
3134 *nids = 0;
3136 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3137 if (err)
3138 return err;
3140 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3141 switch (imsg.hdr.type) {
3142 case GOT_IMSG_OBJ_ID_LIST:
3143 if (datalen < sizeof(*idlist)) {
3144 err = got_error(GOT_ERR_PRIVSEP_LEN);
3145 break;
3147 idlist = imsg.data;
3148 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3149 err = got_error(GOT_ERR_PRIVSEP_LEN);
3150 break;
3152 *nids = idlist->nids;
3153 *ids = calloc(*nids, sizeof(**ids));
3154 if (*ids == NULL) {
3155 err = got_error_from_errno("calloc");
3156 break;
3158 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
3159 *nids * sizeof(**ids));
3160 break;
3161 case GOT_IMSG_OBJ_ID_LIST_DONE:
3162 *done = 1;
3163 break;
3164 default:
3165 err = got_error(GOT_ERR_PRIVSEP_MSG);
3166 break;
3169 imsg_free(&imsg);
3171 return err;
3174 const struct got_error *
3175 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3177 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3178 == -1)
3179 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3181 return flush_imsg(ibuf);
3184 const struct got_error *
3185 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3186 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3188 const struct got_error *err = NULL;
3189 struct ibuf *wbuf;
3190 struct got_imsg_reused_deltas ideltas;
3191 size_t i;
3193 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3194 return got_error(GOT_ERR_NO_SPACE);
3196 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3197 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3198 if (wbuf == NULL) {
3199 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3200 return err;
3203 ideltas.ndeltas = ndeltas;
3204 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3205 return got_error_from_errno("imsg_add REUSED_DELTAS");
3207 for (i = 0; i < ndeltas; i++) {
3208 struct got_imsg_reused_delta *delta = &deltas[i];
3209 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3210 return got_error_from_errno("imsg_add REUSED_DELTAS");
3213 wbuf->fd = -1;
3214 imsg_close(ibuf, wbuf);
3216 return flush_imsg(ibuf);
3219 const struct got_error *
3220 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3222 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3223 == -1)
3224 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3226 return flush_imsg(ibuf);
3229 const struct got_error *
3230 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3231 size_t *ndeltas, struct imsgbuf *ibuf)
3233 const struct got_error *err = NULL;
3234 struct imsg imsg;
3235 struct got_imsg_reused_deltas *ideltas;
3236 size_t datalen;
3238 *done = 0;
3239 *ndeltas = 0;
3241 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3242 if (err)
3243 return err;
3245 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3246 switch (imsg.hdr.type) {
3247 case GOT_IMSG_REUSED_DELTAS:
3248 if (datalen < sizeof(*ideltas)) {
3249 err = got_error(GOT_ERR_PRIVSEP_LEN);
3250 break;
3252 ideltas = imsg.data;
3253 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3254 err = got_error(GOT_ERR_PRIVSEP_LEN);
3255 break;
3257 *ndeltas = ideltas->ndeltas;
3258 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3259 *ndeltas * sizeof(*deltas));
3260 break;
3261 case GOT_IMSG_DELTA_REUSE_DONE:
3262 *done = 1;
3263 break;
3264 default:
3265 err = got_error(GOT_ERR_PRIVSEP_MSG);
3266 break;
3269 imsg_free(&imsg);
3271 return err;
3274 const struct got_error *
3275 got_privsep_unveil_exec_helpers(void)
3277 const char *helpers[] = {
3278 GOT_PATH_PROG_READ_PACK,
3279 GOT_PATH_PROG_READ_OBJECT,
3280 GOT_PATH_PROG_READ_COMMIT,
3281 GOT_PATH_PROG_READ_TREE,
3282 GOT_PATH_PROG_READ_BLOB,
3283 GOT_PATH_PROG_READ_TAG,
3284 GOT_PATH_PROG_READ_GITCONFIG,
3285 GOT_PATH_PROG_READ_GOTCONFIG,
3286 GOT_PATH_PROG_READ_PATCH,
3287 GOT_PATH_PROG_FETCH_PACK,
3288 GOT_PATH_PROG_INDEX_PACK,
3289 GOT_PATH_PROG_SEND_PACK,
3291 size_t i;
3293 for (i = 0; i < nitems(helpers); i++) {
3294 if (unveil(helpers[i], "x") == 0)
3295 continue;
3296 return got_error_from_errno2("unveil", helpers[i]);
3299 return NULL;
3302 void
3303 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3305 if (close(imsg_fds[0]) == -1) {
3306 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3307 _exit(1);
3310 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3311 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3312 _exit(1);
3315 closefrom(GOT_IMSG_FD_CHILD + 1);
3317 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3318 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3319 strerror(errno));
3320 _exit(1);