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;
2112 const size_t min_datalen = 0;
2114 *str = NULL;
2116 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2117 if (err)
2118 return err;
2119 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2121 switch (imsg.hdr.type) {
2122 case GOT_IMSG_GITCONFIG_STR_VAL:
2123 if (datalen == 0)
2124 break;
2125 /* datalen does not include terminating \0 */
2126 *str = malloc(datalen + 1);
2127 if (*str == NULL) {
2128 err = got_error_from_errno("malloc");
2129 break;
2131 memcpy(*str, imsg.data, datalen);
2132 (*str)[datalen] = '\0';
2133 break;
2134 default:
2135 err = got_error(GOT_ERR_PRIVSEP_MSG);
2136 break;
2139 imsg_free(&imsg);
2140 return err;
2143 const struct got_error *
2144 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2146 const struct got_error *err = NULL;
2147 struct imsg imsg;
2148 size_t datalen;
2149 const size_t min_datalen =
2150 MIN(sizeof(struct got_imsg_error), sizeof(int));
2152 *val = 0;
2154 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2155 if (err)
2156 return err;
2157 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2159 switch (imsg.hdr.type) {
2160 case GOT_IMSG_GITCONFIG_INT_VAL:
2161 if (datalen != sizeof(*val)) {
2162 err = got_error(GOT_ERR_PRIVSEP_LEN);
2163 break;
2165 memcpy(val, imsg.data, sizeof(*val));
2166 break;
2167 default:
2168 err = got_error(GOT_ERR_PRIVSEP_MSG);
2169 break;
2172 imsg_free(&imsg);
2173 return err;
2176 static void
2177 free_remote_data(struct got_remote_repo *remote)
2179 int i;
2181 free(remote->name);
2182 free(remote->fetch_url);
2183 free(remote->send_url);
2184 for (i = 0; i < remote->nfetch_branches; i++)
2185 free(remote->fetch_branches[i]);
2186 free(remote->fetch_branches);
2187 for (i = 0; i < remote->nsend_branches; i++)
2188 free(remote->send_branches[i]);
2189 free(remote->send_branches);
2190 for (i = 0; i < remote->nfetch_refs; i++)
2191 free(remote->fetch_refs[i]);
2192 free(remote->fetch_refs);
2195 const struct got_error *
2196 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2197 int *nremotes, struct imsgbuf *ibuf)
2199 const struct got_error *err = NULL;
2200 struct imsg imsg;
2201 size_t datalen;
2202 struct got_imsg_remotes iremotes;
2203 struct got_imsg_remote iremote;
2205 *remotes = NULL;
2206 *nremotes = 0;
2207 iremotes.nremotes = 0;
2209 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2210 if (err)
2211 return err;
2212 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2214 switch (imsg.hdr.type) {
2215 case GOT_IMSG_GITCONFIG_REMOTES:
2216 if (datalen != sizeof(iremotes)) {
2217 err = got_error(GOT_ERR_PRIVSEP_LEN);
2218 break;
2220 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2221 if (iremotes.nremotes == 0) {
2222 imsg_free(&imsg);
2223 return NULL;
2225 break;
2226 default:
2227 imsg_free(&imsg);
2228 return got_error(GOT_ERR_PRIVSEP_MSG);
2231 imsg_free(&imsg);
2233 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2234 if (*remotes == NULL)
2235 return got_error_from_errno("recallocarray");
2237 while (*nremotes < iremotes.nremotes) {
2238 struct got_remote_repo *remote;
2240 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2241 if (err)
2242 break;
2243 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2245 switch (imsg.hdr.type) {
2246 case GOT_IMSG_GITCONFIG_REMOTE:
2247 remote = &(*remotes)[*nremotes];
2248 memset(remote, 0, sizeof(*remote));
2249 if (datalen < sizeof(iremote)) {
2250 err = got_error(GOT_ERR_PRIVSEP_LEN);
2251 break;
2253 memcpy(&iremote, imsg.data, sizeof(iremote));
2254 if (iremote.name_len == 0 ||
2255 iremote.fetch_url_len == 0 ||
2256 iremote.send_url_len == 0 ||
2257 (sizeof(iremote) + iremote.name_len +
2258 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2259 err = got_error(GOT_ERR_PRIVSEP_LEN);
2260 break;
2262 remote->name = strndup(imsg.data + sizeof(iremote),
2263 iremote.name_len);
2264 if (remote->name == NULL) {
2265 err = got_error_from_errno("strndup");
2266 break;
2268 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2269 iremote.name_len, iremote.fetch_url_len);
2270 if (remote->fetch_url == NULL) {
2271 err = got_error_from_errno("strndup");
2272 free_remote_data(remote);
2273 break;
2275 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2276 iremote.name_len + iremote.fetch_url_len,
2277 iremote.send_url_len);
2278 if (remote->send_url == NULL) {
2279 err = got_error_from_errno("strndup");
2280 free_remote_data(remote);
2281 break;
2283 remote->mirror_references = iremote.mirror_references;
2284 remote->fetch_all_branches = iremote.fetch_all_branches;
2285 remote->nfetch_branches = 0;
2286 remote->fetch_branches = NULL;
2287 remote->nsend_branches = 0;
2288 remote->send_branches = NULL;
2289 remote->nfetch_refs = 0;
2290 remote->fetch_refs = NULL;
2291 (*nremotes)++;
2292 break;
2293 default:
2294 err = got_error(GOT_ERR_PRIVSEP_MSG);
2295 break;
2298 imsg_free(&imsg);
2299 if (err)
2300 break;
2303 if (err) {
2304 int i;
2305 for (i = 0; i < *nremotes; i++)
2306 free_remote_data(&(*remotes)[i]);
2307 free(*remotes);
2308 *remotes = NULL;
2309 *nremotes = 0;
2311 return err;
2314 const struct got_error *
2315 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2317 const struct got_error *err = NULL;
2319 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2320 NULL, 0) == -1) {
2321 err = got_error_from_errno("imsg_compose "
2322 "GOTCONFIG_PARSE_REQUEST");
2323 close(fd);
2324 return err;
2327 return flush_imsg(ibuf);
2330 const struct got_error *
2331 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2333 if (imsg_compose(ibuf,
2334 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2335 return got_error_from_errno("imsg_compose "
2336 "GOTCONFIG_AUTHOR_REQUEST");
2338 return flush_imsg(ibuf);
2341 const struct got_error *
2342 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2344 if (imsg_compose(ibuf,
2345 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2346 return got_error_from_errno("imsg_compose "
2347 "GOTCONFIG_REMOTE_REQUEST");
2349 return flush_imsg(ibuf);
2352 const struct got_error *
2353 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2355 const struct got_error *err = NULL;
2356 struct imsg imsg;
2357 size_t datalen;
2358 const size_t min_datalen = 0;
2360 *str = NULL;
2362 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2363 if (err)
2364 return err;
2365 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2367 switch (imsg.hdr.type) {
2368 case GOT_IMSG_ERROR:
2369 err = recv_imsg_error(&imsg, datalen);
2370 break;
2371 case GOT_IMSG_GOTCONFIG_STR_VAL:
2372 if (datalen == 0)
2373 break;
2374 /* datalen does not include terminating \0 */
2375 *str = malloc(datalen + 1);
2376 if (*str == NULL) {
2377 err = got_error_from_errno("malloc");
2378 break;
2380 memcpy(*str, imsg.data, datalen);
2381 (*str)[datalen] = '\0';
2382 break;
2383 default:
2384 err = got_error(GOT_ERR_PRIVSEP_MSG);
2385 break;
2388 imsg_free(&imsg);
2389 return err;
2392 const struct got_error *
2393 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2394 int *nremotes, struct imsgbuf *ibuf)
2396 const struct got_error *err = NULL;
2397 struct imsg imsg;
2398 size_t datalen;
2399 struct got_imsg_remotes iremotes;
2400 struct got_imsg_remote iremote;
2401 const size_t min_datalen =
2402 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2404 *remotes = NULL;
2405 *nremotes = 0;
2406 iremotes.nremotes = 0;
2408 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2409 if (err)
2410 return err;
2411 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2413 switch (imsg.hdr.type) {
2414 case GOT_IMSG_ERROR:
2415 err = recv_imsg_error(&imsg, datalen);
2416 break;
2417 case GOT_IMSG_GOTCONFIG_REMOTES:
2418 if (datalen != sizeof(iremotes)) {
2419 err = got_error(GOT_ERR_PRIVSEP_LEN);
2420 break;
2422 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2423 if (iremotes.nremotes == 0) {
2424 imsg_free(&imsg);
2425 return NULL;
2427 break;
2428 default:
2429 imsg_free(&imsg);
2430 return got_error(GOT_ERR_PRIVSEP_MSG);
2433 imsg_free(&imsg);
2435 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2436 if (*remotes == NULL)
2437 return got_error_from_errno("recallocarray");
2439 while (*nremotes < iremotes.nremotes) {
2440 struct got_remote_repo *remote;
2441 const size_t min_datalen =
2442 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2443 int i;
2445 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2446 if (err)
2447 break;
2448 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2450 switch (imsg.hdr.type) {
2451 case GOT_IMSG_ERROR:
2452 err = recv_imsg_error(&imsg, datalen);
2453 break;
2454 case GOT_IMSG_GOTCONFIG_REMOTE:
2455 remote = &(*remotes)[*nremotes];
2456 memset(remote, 0, sizeof(*remote));
2457 if (datalen < sizeof(iremote)) {
2458 err = got_error(GOT_ERR_PRIVSEP_LEN);
2459 break;
2461 memcpy(&iremote, imsg.data, sizeof(iremote));
2462 if (iremote.name_len == 0 ||
2463 (iremote.fetch_url_len == 0 &&
2464 iremote.send_url_len == 0) ||
2465 (sizeof(iremote) + iremote.name_len +
2466 iremote.fetch_url_len + iremote.send_url_len) >
2467 datalen) {
2468 err = got_error(GOT_ERR_PRIVSEP_LEN);
2469 break;
2471 remote->name = strndup(imsg.data + sizeof(iremote),
2472 iremote.name_len);
2473 if (remote->name == NULL) {
2474 err = got_error_from_errno("strndup");
2475 break;
2477 remote->fetch_url = strndup(imsg.data +
2478 sizeof(iremote) + iremote.name_len,
2479 iremote.fetch_url_len);
2480 if (remote->fetch_url == NULL) {
2481 err = got_error_from_errno("strndup");
2482 free_remote_data(remote);
2483 break;
2485 remote->send_url = strndup(imsg.data +
2486 sizeof(iremote) + iremote.name_len +
2487 iremote.fetch_url_len, iremote.send_url_len);
2488 if (remote->send_url == NULL) {
2489 err = got_error_from_errno("strndup");
2490 free_remote_data(remote);
2491 break;
2493 remote->mirror_references = iremote.mirror_references;
2494 remote->fetch_all_branches = iremote.fetch_all_branches;
2495 if (iremote.nfetch_branches > 0) {
2496 remote->fetch_branches = recallocarray(NULL, 0,
2497 iremote.nfetch_branches, sizeof(char *));
2498 if (remote->fetch_branches == NULL) {
2499 err = got_error_from_errno("calloc");
2500 free_remote_data(remote);
2501 break;
2504 remote->nfetch_branches = 0;
2505 for (i = 0; i < iremote.nfetch_branches; i++) {
2506 char *branch;
2507 err = got_privsep_recv_gotconfig_str(&branch,
2508 ibuf);
2509 if (err) {
2510 free_remote_data(remote);
2511 goto done;
2513 remote->fetch_branches[i] = branch;
2514 remote->nfetch_branches++;
2516 if (iremote.nsend_branches > 0) {
2517 remote->send_branches = recallocarray(NULL, 0,
2518 iremote.nsend_branches, sizeof(char *));
2519 if (remote->send_branches == NULL) {
2520 err = got_error_from_errno("calloc");
2521 free_remote_data(remote);
2522 break;
2525 remote->nsend_branches = 0;
2526 for (i = 0; i < iremote.nsend_branches; i++) {
2527 char *branch;
2528 err = got_privsep_recv_gotconfig_str(&branch,
2529 ibuf);
2530 if (err) {
2531 free_remote_data(remote);
2532 goto done;
2534 remote->send_branches[i] = branch;
2535 remote->nsend_branches++;
2537 if (iremote.nfetch_refs > 0) {
2538 remote->fetch_refs = recallocarray(NULL, 0,
2539 iremote.nfetch_refs, sizeof(char *));
2540 if (remote->fetch_refs == NULL) {
2541 err = got_error_from_errno("calloc");
2542 free_remote_data(remote);
2543 break;
2546 remote->nfetch_refs = 0;
2547 for (i = 0; i < iremote.nfetch_refs; i++) {
2548 char *ref;
2549 err = got_privsep_recv_gotconfig_str(&ref,
2550 ibuf);
2551 if (err) {
2552 free_remote_data(remote);
2553 goto done;
2555 remote->fetch_refs[i] = ref;
2556 remote->nfetch_refs++;
2558 (*nremotes)++;
2559 break;
2560 default:
2561 err = got_error(GOT_ERR_PRIVSEP_MSG);
2562 break;
2565 imsg_free(&imsg);
2566 if (err)
2567 break;
2569 done:
2570 if (err) {
2571 int i;
2572 for (i = 0; i < *nremotes; i++)
2573 free_remote_data(&(*remotes)[i]);
2574 free(*remotes);
2575 *remotes = NULL;
2576 *nremotes = 0;
2578 return err;
2581 const struct got_error *
2582 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2583 struct got_object_id *id, int idx, const char *path)
2585 struct ibuf *wbuf;
2586 size_t path_len = strlen(path) + 1;
2588 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2589 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2590 if (wbuf == NULL)
2591 return got_error_from_errno(
2592 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2593 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2594 return got_error_from_errno("imsg_add "
2595 "COMMIT_TRAVERSAL_REQUEST");
2596 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2597 return got_error_from_errno("imsg_add "
2598 "COMMIT_TRAVERSAL_REQUEST");
2599 if (imsg_add(wbuf, path, path_len) == -1)
2600 return got_error_from_errno("imsg_add "
2601 "COMMIT_TRAVERSAL_REQUEST");
2603 wbuf->fd = -1;
2604 imsg_close(ibuf, wbuf);
2606 return flush_imsg(ibuf);
2609 const struct got_error *
2610 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2611 struct got_object_id **changed_commit_id,
2612 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2614 const struct got_error *err = NULL;
2615 struct imsg imsg;
2616 struct got_imsg_traversed_commits *icommits;
2617 size_t datalen;
2618 int i, done = 0;
2620 *changed_commit = NULL;
2621 *changed_commit_id = NULL;
2623 while (!done) {
2624 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2625 if (err)
2626 return err;
2628 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2629 switch (imsg.hdr.type) {
2630 case GOT_IMSG_TRAVERSED_COMMITS:
2631 icommits = imsg.data;
2632 if (datalen != sizeof(*icommits) +
2633 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2634 err = got_error(GOT_ERR_PRIVSEP_LEN);
2635 break;
2637 for (i = 0; i < icommits->ncommits; i++) {
2638 struct got_object_qid *qid;
2639 uint8_t *sha1 = (uint8_t *)imsg.data +
2640 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2641 err = got_object_qid_alloc_partial(&qid);
2642 if (err)
2643 break;
2644 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2645 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2647 /* The last commit may contain a change. */
2648 if (i == icommits->ncommits - 1) {
2649 *changed_commit_id =
2650 got_object_id_dup(&qid->id);
2651 if (*changed_commit_id == NULL) {
2652 err = got_error_from_errno(
2653 "got_object_id_dup");
2654 break;
2658 break;
2659 case GOT_IMSG_COMMIT:
2660 if (*changed_commit_id == NULL) {
2661 err = got_error(GOT_ERR_PRIVSEP_MSG);
2662 break;
2664 err = get_commit_from_imsg(changed_commit, &imsg,
2665 datalen, ibuf);
2666 break;
2667 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2668 done = 1;
2669 break;
2670 default:
2671 err = got_error(GOT_ERR_PRIVSEP_MSG);
2672 break;
2675 imsg_free(&imsg);
2676 if (err)
2677 break;
2680 if (err)
2681 got_object_id_queue_free(commit_ids);
2682 return err;
2685 const struct got_error *
2686 got_privsep_send_enumerated_tree(size_t *totlen, struct imsgbuf *ibuf,
2687 struct got_object_id *tree_id, const char *path,
2688 struct got_parsed_tree_entry *entries, int nentries)
2690 const struct got_error *err = NULL;
2691 struct ibuf *wbuf;
2692 size_t path_len = strlen(path);
2693 size_t msglen;
2695 msglen = sizeof(struct got_imsg_enumerated_tree) + path_len;
2696 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_TREE, 0, 0, msglen);
2697 if (wbuf == NULL)
2698 return got_error_from_errno("imsg_create ENUMERATED_TREE");
2700 if (imsg_add(wbuf, tree_id->sha1, SHA1_DIGEST_LENGTH) == -1)
2701 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2702 if (imsg_add(wbuf, &nentries, sizeof(nentries)) == -1)
2703 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2704 if (imsg_add(wbuf, path, path_len) == -1)
2705 return got_error_from_errno("imsg_add ENUMERATED_TREE");
2707 wbuf->fd = -1;
2708 imsg_close(ibuf, wbuf);
2710 if (entries) {
2711 err = send_tree_entries(ibuf, entries, nentries);
2712 if (err)
2713 return err;
2716 return flush_imsg(ibuf);
2719 const struct got_error *
2720 got_privsep_send_object_enumeration_request(struct imsgbuf *ibuf)
2722 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_REQUEST,
2723 0, 0, -1, NULL, 0) == -1)
2724 return got_error_from_errno("imsg_compose "
2725 "OBJECT_ENUMERATION_REQUEST");
2727 return flush_imsg(ibuf);
2730 const struct got_error *
2731 got_privsep_send_object_enumeration_done(struct imsgbuf *ibuf)
2733 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_ENUMERATION_DONE,
2734 0, 0, -1, NULL, 0) == -1)
2735 return got_error_from_errno("imsg_compose "
2736 "OBJECT_ENUMERATION_DONE");
2738 return flush_imsg(ibuf);
2741 const struct got_error *
2742 got_privsep_send_enumerated_commit(struct imsgbuf *ibuf,
2743 struct got_object_id *id, time_t mtime)
2745 struct ibuf *wbuf;
2747 wbuf = imsg_create(ibuf, GOT_IMSG_ENUMERATED_COMMIT, 0, 0,
2748 sizeof(struct got_imsg_enumerated_commit) + SHA1_DIGEST_LENGTH);
2749 if (wbuf == NULL)
2750 return got_error_from_errno("imsg_create ENUMERATED_COMMIT");
2752 /* Keep in sync with struct got_imsg_enumerated_commit! */
2753 if (imsg_add(wbuf, id, SHA1_DIGEST_LENGTH) == -1)
2754 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2755 if (imsg_add(wbuf, &mtime, sizeof(mtime)) == -1)
2756 return got_error_from_errno("imsg_add ENUMERATED_COMMIT");
2758 wbuf->fd = -1;
2759 imsg_close(ibuf, wbuf);
2760 /* Don't flush yet, tree entries or ENUMERATION_DONE will follow. */
2761 return NULL;
2764 const struct got_error *
2765 got_privsep_recv_enumerated_objects(struct imsgbuf *ibuf,
2766 got_object_enumerate_commit_cb cb_commit,
2767 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2768 struct got_repository *repo)
2770 const struct got_error *err = NULL;
2771 struct imsg imsg;
2772 struct got_imsg_enumerated_commit *icommit = NULL;
2773 struct got_object_id commit_id;
2774 int have_commit = 0;
2775 time_t mtime = 0;
2776 struct got_tree_object tree;
2777 struct got_imsg_enumerated_tree *itree;
2778 struct got_object_id tree_id;
2779 char *path = NULL, *canon_path = NULL;
2780 size_t datalen, path_len;
2781 int nentries = -1;
2782 int done = 0;
2784 memset(&tree, 0, sizeof(tree));
2786 while (!done) {
2787 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2788 if (err)
2789 break;
2791 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2792 switch (imsg.hdr.type) {
2793 case GOT_IMSG_ENUMERATED_COMMIT:
2794 if (have_commit && nentries != -1) {
2795 err = got_error(GOT_ERR_PRIVSEP_MSG);
2796 break;
2798 if (datalen != sizeof(*icommit)) {
2799 err = got_error(GOT_ERR_PRIVSEP_LEN);
2800 break;
2802 icommit = (struct got_imsg_enumerated_commit *)imsg.data;
2803 memcpy(commit_id.sha1, icommit->id, SHA1_DIGEST_LENGTH);
2804 mtime = icommit->mtime;
2805 have_commit = 1;
2806 break;
2807 case GOT_IMSG_ENUMERATED_TREE:
2808 /* Should be preceeded by GOT_IMSG_ENUMERATED_COMMIT. */
2809 if (!have_commit) {
2810 err = got_error(GOT_ERR_PRIVSEP_MSG);
2811 break;
2813 if (datalen < sizeof(*itree)) {
2814 err = got_error(GOT_ERR_PRIVSEP_LEN);
2815 break;
2817 itree = imsg.data;
2818 path_len = datalen - sizeof(*itree);
2819 if (path_len == 0) {
2820 err = got_error(GOT_ERR_PRIVSEP_LEN);
2821 break;
2823 memcpy(tree_id.sha1, itree->id, sizeof(tree_id.sha1));
2824 free(path);
2825 path = malloc(path_len + 1);
2826 if (path == NULL) {
2827 err = got_error_from_errno("malloc");
2828 break;
2830 free(canon_path);
2831 canon_path = malloc(path_len + 1);
2832 if (canon_path == NULL) {
2833 err = got_error_from_errno("malloc");
2834 break;
2836 memcpy(path, (uint8_t *)imsg.data + sizeof(*itree),
2837 path_len);
2838 path[path_len] = '\0';
2839 if (!got_path_is_absolute(path)) {
2840 err = got_error(GOT_ERR_BAD_PATH);
2841 break;
2843 if (got_path_is_root_dir(path)) {
2844 /* XXX check what got_canonpath() does wrong */
2845 canon_path[0] = '/';
2846 canon_path[1] = '\0';
2847 } else {
2848 err = got_canonpath(path, canon_path,
2849 path_len + 1);
2850 if (err)
2851 break;
2853 if (strcmp(path, canon_path) != 0) {
2854 err = got_error(GOT_ERR_BAD_PATH);
2855 break;
2857 if (nentries != -1) {
2858 err = got_error(GOT_ERR_PRIVSEP_MSG);
2859 break;
2861 if (itree->nentries < -1) {
2862 err = got_error(GOT_ERR_PRIVSEP_MSG);
2863 break;
2865 if (itree->nentries == -1) {
2866 /* Tree was not found in pack file. */
2867 done = 1;
2868 err = cb_tree(cb_arg, NULL, mtime, &tree_id,
2869 path, repo);
2870 break;
2872 if (itree->nentries > INT_MAX) {
2873 err = got_error(GOT_ERR_PRIVSEP_LEN);
2874 break;
2876 tree.entries = calloc(itree->nentries,
2877 sizeof(struct got_tree_entry));
2878 if (tree.entries == NULL) {
2879 err = got_error_from_errno("calloc");
2880 break;
2882 if (itree->nentries == 0) {
2883 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2884 path, repo);
2885 if (err)
2886 break;
2888 /* Prepare for next tree. */
2889 free(tree.entries);
2890 memset(&tree, 0, sizeof(tree));
2891 nentries = -1;
2892 } else {
2893 tree.nentries = itree->nentries;
2894 nentries = 0;
2896 break;
2897 case GOT_IMSG_TREE_ENTRIES:
2898 /* Should be preceeded by GOT_IMSG_ENUMERATED_TREE. */
2899 if (nentries <= -1) {
2900 err = got_error(GOT_ERR_PRIVSEP_MSG);
2901 break;
2903 err = recv_tree_entries(imsg.data, datalen,
2904 &tree, &nentries);
2905 if (err)
2906 break;
2907 if (tree.nentries == nentries) {
2908 err = cb_tree(cb_arg, &tree, mtime, &tree_id,
2909 path, repo);
2910 if (err)
2911 break;
2913 /* Prepare for next tree. */
2914 free(tree.entries);
2915 memset(&tree, 0, sizeof(tree));
2916 nentries = -1;
2918 break;
2919 case GOT_IMSG_TREE_ENUMERATION_DONE:
2920 /* All trees have been found and traversed. */
2921 if (!have_commit || path == NULL || nentries != -1) {
2922 err = got_error(GOT_ERR_PRIVSEP_MSG);
2923 break;
2925 err = cb_commit(cb_arg, mtime, &commit_id, repo);
2926 if (err)
2927 break;
2928 have_commit = 0;
2929 break;
2930 case GOT_IMSG_OBJECT_ENUMERATION_DONE:
2931 done = 1;
2932 break;
2933 default:
2934 err = got_error(GOT_ERR_PRIVSEP_MSG);
2935 break;
2938 imsg_free(&imsg);
2939 if (err)
2940 break;
2943 free(path);
2944 free(canon_path);
2945 free(tree.entries);
2946 return err;
2949 const struct got_error *
2950 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2951 struct got_object_id *id)
2953 struct got_imsg_raw_delta_request dreq;
2955 dreq.idx = idx;
2956 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2958 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2959 &dreq, sizeof(dreq)) == -1)
2960 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2962 return flush_imsg(ibuf);
2965 const struct got_error *
2966 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2968 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2971 const struct got_error *
2972 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2973 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2974 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2976 struct got_imsg_raw_delta idelta;
2977 int ret;
2979 idelta.base_size = base_size;
2980 idelta.result_size = result_size;
2981 idelta.delta_size = delta_size;
2982 idelta.delta_compressed_size = delta_compressed_size;
2983 idelta.delta_offset = delta_offset;
2984 idelta.delta_out_offset = delta_out_offset;
2985 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2987 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2988 &idelta, sizeof(idelta));
2989 if (ret == -1)
2990 return got_error_from_errno("imsg_compose RAW_DELTA");
2992 return flush_imsg(ibuf);
2995 const struct got_error *
2996 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2997 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2998 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
3000 const struct got_error *err = NULL;
3001 struct imsg imsg;
3002 struct got_imsg_raw_delta *delta;
3003 size_t datalen;
3005 *base_size = 0;
3006 *result_size = 0;
3007 *delta_size = 0;
3008 *delta_compressed_size = 0;
3009 *delta_offset = 0;
3010 *delta_out_offset = 0;
3011 *base_id = NULL;
3013 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3014 if (err)
3015 return err;
3017 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3019 switch (imsg.hdr.type) {
3020 case GOT_IMSG_RAW_DELTA:
3021 if (datalen != sizeof(*delta)) {
3022 err = got_error(GOT_ERR_PRIVSEP_LEN);
3023 break;
3025 delta = imsg.data;
3026 *base_size = delta->base_size;
3027 *result_size = delta->result_size;
3028 *delta_size = delta->delta_size;
3029 *delta_compressed_size = delta->delta_compressed_size;
3030 *delta_offset = delta->delta_offset;
3031 *delta_out_offset = delta->delta_out_offset;
3032 *base_id = calloc(1, sizeof(**base_id));
3033 if (*base_id == NULL) {
3034 err = got_error_from_errno("malloc");
3035 break;
3037 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
3038 break;
3039 default:
3040 err = got_error(GOT_ERR_PRIVSEP_MSG);
3041 break;
3044 imsg_free(&imsg);
3046 if (err) {
3047 free(*base_id);
3048 *base_id = NULL;
3050 return err;
3053 static const struct got_error *
3054 send_idlist(struct imsgbuf *ibuf, struct got_object_id **ids, size_t nids)
3056 const struct got_error *err = NULL;
3057 struct got_imsg_object_idlist idlist;
3058 struct ibuf *wbuf;
3059 size_t i;
3061 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
3062 return got_error(GOT_ERR_NO_SPACE);
3064 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
3065 sizeof(idlist) + nids * sizeof(**ids));
3066 if (wbuf == NULL) {
3067 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
3068 return err;
3071 idlist.nids = nids;
3072 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
3073 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3075 for (i = 0; i < nids; i++) {
3076 struct got_object_id *id = ids[i];
3077 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
3078 return got_error_from_errno("imsg_add OBJ_ID_LIST");
3081 wbuf->fd = -1;
3082 imsg_close(ibuf, wbuf);
3084 return flush_imsg(ibuf);
3087 const struct got_error *
3088 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
3089 struct got_object_id **ids, size_t nids)
3091 const struct got_error *err = NULL;
3092 struct got_object_id *idlist[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
3093 int i, j = 0;
3095 for (i = 0; i < nids; i++) {
3096 j = i % nitems(idlist);
3097 idlist[j] = ids[i];
3098 if (j >= nitems(idlist) - 1) {
3099 err = send_idlist(ibuf, idlist, j + 1);
3100 if (err)
3101 return err;
3102 j = 0;
3106 if (j > 0) {
3107 err = send_idlist(ibuf, idlist, j + 1);
3108 if (err)
3109 return err;
3112 return NULL;
3115 const struct got_error *
3116 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
3118 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
3119 == -1)
3120 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
3122 return flush_imsg(ibuf);
3125 const struct got_error *
3126 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
3127 size_t *nids, struct imsgbuf *ibuf)
3129 const struct got_error *err = NULL;
3130 struct imsg imsg;
3131 struct got_imsg_object_idlist *idlist;
3132 size_t datalen;
3134 *ids = NULL;
3135 *done = 0;
3136 *nids = 0;
3138 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3139 if (err)
3140 return err;
3142 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3143 switch (imsg.hdr.type) {
3144 case GOT_IMSG_OBJ_ID_LIST:
3145 if (datalen < sizeof(*idlist)) {
3146 err = got_error(GOT_ERR_PRIVSEP_LEN);
3147 break;
3149 idlist = imsg.data;
3150 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3151 err = got_error(GOT_ERR_PRIVSEP_LEN);
3152 break;
3154 *nids = idlist->nids;
3155 *ids = calloc(*nids, sizeof(**ids));
3156 if (*ids == NULL) {
3157 err = got_error_from_errno("calloc");
3158 break;
3160 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
3161 *nids * sizeof(**ids));
3162 break;
3163 case GOT_IMSG_OBJ_ID_LIST_DONE:
3164 *done = 1;
3165 break;
3166 default:
3167 err = got_error(GOT_ERR_PRIVSEP_MSG);
3168 break;
3171 imsg_free(&imsg);
3173 return err;
3176 const struct got_error *
3177 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
3179 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
3180 == -1)
3181 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
3183 return flush_imsg(ibuf);
3186 const struct got_error *
3187 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
3188 struct got_imsg_reused_delta *deltas, size_t ndeltas)
3190 const struct got_error *err = NULL;
3191 struct ibuf *wbuf;
3192 struct got_imsg_reused_deltas ideltas;
3193 size_t i;
3195 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
3196 return got_error(GOT_ERR_NO_SPACE);
3198 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
3199 sizeof(ideltas) + ndeltas * sizeof(*deltas));
3200 if (wbuf == NULL) {
3201 err = got_error_from_errno("imsg_create REUSED_DELTAS");
3202 return err;
3205 ideltas.ndeltas = ndeltas;
3206 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
3207 return got_error_from_errno("imsg_add REUSED_DELTAS");
3209 for (i = 0; i < ndeltas; i++) {
3210 struct got_imsg_reused_delta *delta = &deltas[i];
3211 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
3212 return got_error_from_errno("imsg_add REUSED_DELTAS");
3215 wbuf->fd = -1;
3216 imsg_close(ibuf, wbuf);
3218 return flush_imsg(ibuf);
3221 const struct got_error *
3222 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
3224 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
3225 == -1)
3226 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
3228 return flush_imsg(ibuf);
3231 const struct got_error *
3232 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
3233 size_t *ndeltas, struct imsgbuf *ibuf)
3235 const struct got_error *err = NULL;
3236 struct imsg imsg;
3237 struct got_imsg_reused_deltas *ideltas;
3238 size_t datalen;
3240 *done = 0;
3241 *ndeltas = 0;
3243 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3244 if (err)
3245 return err;
3247 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3248 switch (imsg.hdr.type) {
3249 case GOT_IMSG_REUSED_DELTAS:
3250 if (datalen < sizeof(*ideltas)) {
3251 err = got_error(GOT_ERR_PRIVSEP_LEN);
3252 break;
3254 ideltas = imsg.data;
3255 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3256 err = got_error(GOT_ERR_PRIVSEP_LEN);
3257 break;
3259 *ndeltas = ideltas->ndeltas;
3260 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3261 *ndeltas * sizeof(*deltas));
3262 break;
3263 case GOT_IMSG_DELTA_REUSE_DONE:
3264 *done = 1;
3265 break;
3266 default:
3267 err = got_error(GOT_ERR_PRIVSEP_MSG);
3268 break;
3271 imsg_free(&imsg);
3273 return err;
3276 const struct got_error *
3277 got_privsep_unveil_exec_helpers(void)
3279 const char *helpers[] = {
3280 GOT_PATH_PROG_READ_PACK,
3281 GOT_PATH_PROG_READ_OBJECT,
3282 GOT_PATH_PROG_READ_COMMIT,
3283 GOT_PATH_PROG_READ_TREE,
3284 GOT_PATH_PROG_READ_BLOB,
3285 GOT_PATH_PROG_READ_TAG,
3286 GOT_PATH_PROG_READ_GITCONFIG,
3287 GOT_PATH_PROG_READ_GOTCONFIG,
3288 GOT_PATH_PROG_READ_PATCH,
3289 GOT_PATH_PROG_FETCH_PACK,
3290 GOT_PATH_PROG_INDEX_PACK,
3291 GOT_PATH_PROG_SEND_PACK,
3293 size_t i;
3295 for (i = 0; i < nitems(helpers); i++) {
3296 if (unveil(helpers[i], "x") == 0)
3297 continue;
3298 return got_error_from_errno2("unveil", helpers[i]);
3301 return NULL;
3304 void
3305 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3307 if (close(imsg_fds[0]) == -1) {
3308 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3309 _exit(1);
3312 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3313 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3314 _exit(1);
3317 closefrom(GOT_IMSG_FD_CHILD + 1);
3319 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3320 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3321 strerror(errno));
3322 _exit(1);