Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/uio.h>
20 #include <sys/wait.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <stdint.h>
30 #include <poll.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <time.h>
35 #include "got_compat.h"
37 #include "got_object.h"
38 #include "got_error.h"
39 #include "got_path.h"
40 #include "got_repository.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
50 #ifndef MIN
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 #endif
54 #ifndef nitems
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 #endif
58 static const struct got_error *
59 poll_fd(int fd, int events, int timeout)
60 {
61 struct pollfd pfd[1];
62 struct timespec ts;
63 sigset_t sigset;
64 int n;
66 pfd[0].fd = fd;
67 pfd[0].events = events;
69 ts.tv_sec = timeout;
70 ts.tv_nsec = 0;
72 if (sigemptyset(&sigset) == -1)
73 return got_error_from_errno("sigemptyset");
74 if (sigaddset(&sigset, SIGWINCH) == -1)
75 return got_error_from_errno("sigaddset");
77 n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
78 if (n == -1)
79 return got_error_from_errno("ppoll");
80 if (n == 0)
81 return got_error(GOT_ERR_TIMEOUT);
82 if (pfd[0].revents & (POLLERR | POLLNVAL))
83 return got_error_from_errno("poll error");
84 if (pfd[0].revents & (events | POLLHUP))
85 return NULL;
87 return got_error(GOT_ERR_INTERRUPT);
88 }
90 static const struct got_error *
91 read_imsg(struct imsgbuf *ibuf)
92 {
93 const struct got_error *err;
94 size_t n;
96 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
97 if (err)
98 return err;
100 n = imsg_read(ibuf);
101 if (n == -1) {
102 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
103 return got_error(GOT_ERR_PRIVSEP_NO_FD);
104 return got_error(GOT_ERR_PRIVSEP_READ);
106 if (n == 0)
107 return got_error(GOT_ERR_PRIVSEP_PIPE);
109 return NULL;
112 const struct got_error *
113 got_privsep_wait_for_child(pid_t pid)
115 int child_status;
117 if (waitpid(pid, &child_status, 0) == -1)
118 return got_error_from_errno("waitpid");
120 if (!WIFEXITED(child_status))
121 return got_error(GOT_ERR_PRIVSEP_DIED);
123 if (WEXITSTATUS(child_status) != 0)
124 return got_error(GOT_ERR_PRIVSEP_EXIT);
126 return NULL;
129 static const struct got_error *
130 recv_imsg_error(struct imsg *imsg, size_t datalen)
132 struct got_imsg_error *ierr;
134 if (datalen != sizeof(*ierr))
135 return got_error(GOT_ERR_PRIVSEP_LEN);
137 ierr = imsg->data;
138 if (ierr->code == GOT_ERR_ERRNO) {
139 static struct got_error serr;
140 serr.code = GOT_ERR_ERRNO;
141 serr.msg = strerror(ierr->errno_code);
142 return &serr;
145 return got_error(ierr->code);
148 const struct got_error *
149 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
150 size_t min_datalen)
152 const struct got_error *err;
153 ssize_t n;
155 n = imsg_get(ibuf, imsg);
156 if (n == -1)
157 return got_error_from_errno("imsg_get");
159 while (n == 0) {
160 err = read_imsg(ibuf);
161 if (err)
162 return err;
163 n = imsg_get(ibuf, imsg);
164 if (n == -1)
165 return got_error_from_errno("imsg_get");
168 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
169 return got_error(GOT_ERR_PRIVSEP_LEN);
171 if (imsg->hdr.type == GOT_IMSG_ERROR) {
172 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
173 return recv_imsg_error(imsg, datalen);
176 return NULL;
179 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
180 void
181 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
183 const struct got_error *poll_err;
184 struct got_imsg_error ierr;
185 int ret;
187 ierr.code = err->code;
188 if (err->code == GOT_ERR_ERRNO)
189 ierr.errno_code = errno;
190 else
191 ierr.errno_code = 0;
192 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
193 if (ret == -1) {
194 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
195 getprogname(), err->code, err->msg, strerror(errno));
196 return;
199 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
200 if (poll_err) {
201 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
202 getprogname(), err->code, err->msg, poll_err->msg);
203 return;
206 ret = imsg_flush(ibuf);
207 if (ret == -1) {
208 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
209 getprogname(), err->code, err->msg, strerror(errno));
210 imsg_clear(ibuf);
211 return;
215 static const struct got_error *
216 flush_imsg(struct imsgbuf *ibuf)
218 const struct got_error *err;
220 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
221 if (err)
222 return err;
224 if (imsg_flush(ibuf) == -1) {
225 imsg_clear(ibuf);
226 return got_error_from_errno("imsg_flush");
229 return NULL;
232 const struct got_error *
233 got_privsep_flush_imsg(struct imsgbuf *ibuf)
235 return flush_imsg(ibuf);
238 const struct got_error *
239 got_privsep_send_stop(int fd)
241 const struct got_error *err = NULL;
242 struct imsgbuf ibuf;
244 imsg_init(&ibuf, fd);
246 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
247 return got_error_from_errno("imsg_compose STOP");
249 err = flush_imsg(&ibuf);
250 return err;
253 const struct got_error *
254 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
255 struct got_object_id *id)
257 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
258 id, sizeof(*id)) == -1)
259 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
261 return flush_imsg(ibuf);
264 const struct got_error *
265 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
266 struct got_object_id *id)
268 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
269 id, sizeof(*id)) == -1)
270 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
272 return flush_imsg(ibuf);
275 const struct got_error *
276 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
278 const struct got_error *err = NULL;
280 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
281 == -1) {
282 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
283 close(outfd);
284 return err;
287 return flush_imsg(ibuf);
290 const struct got_error *
291 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
292 uint8_t *data)
294 const struct got_error *err = NULL;
295 struct got_imsg_raw_obj iobj;
296 size_t len = sizeof(iobj);
297 struct ibuf *wbuf;
299 iobj.hdrlen = hdrlen;
300 iobj.size = size;
302 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
303 len += (size_t)size + hdrlen;
305 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
306 if (wbuf == NULL) {
307 err = got_error_from_errno("imsg_create RAW_OBJECT");
308 return err;
311 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
312 return got_error_from_errno("imsg_add RAW_OBJECT");
314 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
315 if (imsg_add(wbuf, data, size + hdrlen) == -1)
316 return got_error_from_errno("imsg_add RAW_OBJECT");
319 wbuf->fd = -1;
320 imsg_close(ibuf, wbuf);
322 return flush_imsg(ibuf);
325 const struct got_error *
326 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
327 struct imsgbuf *ibuf)
329 const struct got_error *err = NULL;
330 struct imsg imsg;
331 struct got_imsg_raw_obj *iobj;
332 size_t datalen;
334 *outbuf = NULL;
336 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
337 if (err)
338 return err;
340 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
342 switch (imsg.hdr.type) {
343 case GOT_IMSG_RAW_OBJECT:
344 if (datalen < sizeof(*iobj)) {
345 err = got_error(GOT_ERR_PRIVSEP_LEN);
346 break;
348 iobj = imsg.data;
349 *size = iobj->size;
350 *hdrlen = iobj->hdrlen;
352 if (datalen == sizeof(*iobj)) {
353 /* Data has been written to file descriptor. */
354 break;
357 if (*size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
358 err = got_error(GOT_ERR_PRIVSEP_LEN);
359 break;
362 *outbuf = malloc(*size + *hdrlen);
363 if (*outbuf == NULL) {
364 err = got_error_from_errno("malloc");
365 break;
367 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
368 break;
369 default:
370 err = got_error(GOT_ERR_PRIVSEP_MSG);
371 break;
374 imsg_free(&imsg);
376 return err;
379 const struct got_error *
380 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
381 struct got_object_id *id, int pack_idx)
383 const struct got_error *err = NULL;
384 struct got_imsg_packed_object iobj;
385 void *data;
386 size_t len;
388 if (pack_idx != -1) { /* commit is packed */
389 iobj.idx = pack_idx;
390 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
391 data = &iobj;
392 len = sizeof(iobj);
393 } else {
394 data = id;
395 len = sizeof(*id);
398 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
399 == -1) {
400 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
401 close(fd);
402 return err;
405 return flush_imsg(ibuf);
408 const struct got_error *
409 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
410 struct got_object_id *id, int pack_idx)
412 struct ibuf *wbuf;
413 size_t len;
415 if (pack_idx != -1)
416 len = sizeof(struct got_imsg_packed_object);
417 else
418 len = sizeof(*id);
420 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
421 if (wbuf == NULL)
422 return got_error_from_errno("imsg_create TREE_REQUEST");
424 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
425 return got_error_from_errno("imsg_add TREE_REQUEST");
427 if (pack_idx != -1) { /* tree is packed */
428 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
429 return got_error_from_errno("imsg_add TREE_REQUEST");
432 wbuf->fd = fd;
433 imsg_close(ibuf, wbuf);
435 return flush_imsg(ibuf);
438 const struct got_error *
439 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
440 struct got_object_id *id, int pack_idx)
442 struct got_imsg_packed_object iobj;
443 void *data;
444 size_t len;
446 if (pack_idx != -1) { /* tag is packed */
447 iobj.idx = pack_idx;
448 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
449 data = &iobj;
450 len = sizeof(iobj);
451 } else {
452 data = id;
453 len = sizeof(*id);
456 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
457 == -1)
458 return got_error_from_errno("imsg_compose TAG_REQUEST");
460 return flush_imsg(ibuf);
463 const struct got_error *
464 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
465 struct got_object_id *id, int pack_idx)
467 const struct got_error *err = NULL;
468 struct got_imsg_packed_object iobj;
469 void *data;
470 size_t len;
472 if (pack_idx != -1) { /* blob is packed */
473 iobj.idx = pack_idx;
474 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
475 data = &iobj;
476 len = sizeof(iobj);
477 } else {
478 data = id;
479 len = sizeof(*id);
482 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
483 == -1) {
484 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
485 close(infd);
486 return err;
489 return flush_imsg(ibuf);
492 const struct got_error *
493 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
495 const struct got_error *err = NULL;
497 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
498 == -1) {
499 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
500 close(outfd);
501 return err;
504 return flush_imsg(ibuf);
507 static const struct got_error *
508 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
510 const struct got_error *err = NULL;
512 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
513 err = got_error_from_errno("imsg_compose TMPFD");
514 close(fd);
515 return err;
518 return flush_imsg(ibuf);
521 const struct got_error *
522 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
524 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
527 const struct got_error *
528 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
530 struct got_imsg_object iobj;
532 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
533 iobj.type = obj->type;
534 iobj.flags = obj->flags;
535 iobj.hdrlen = obj->hdrlen;
536 iobj.size = obj->size;
537 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
538 iobj.pack_offset = obj->pack_offset;
539 iobj.pack_idx = obj->pack_idx;
542 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
543 == -1)
544 return got_error_from_errno("imsg_compose OBJECT");
546 return flush_imsg(ibuf);
549 const struct got_error *
550 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
551 struct got_pathlist_head *have_refs, int fetch_all_branches,
552 struct got_pathlist_head *wanted_branches,
553 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
555 const struct got_error *err = NULL;
556 struct ibuf *wbuf;
557 size_t len;
558 struct got_pathlist_entry *pe;
559 struct got_imsg_fetch_request fetchreq;
561 memset(&fetchreq, 0, sizeof(fetchreq));
562 fetchreq.fetch_all_branches = fetch_all_branches;
563 fetchreq.list_refs_only = list_refs_only;
564 fetchreq.verbosity = verbosity;
565 TAILQ_FOREACH(pe, have_refs, entry)
566 fetchreq.n_have_refs++;
567 TAILQ_FOREACH(pe, wanted_branches, entry)
568 fetchreq.n_wanted_branches++;
569 TAILQ_FOREACH(pe, wanted_refs, entry)
570 fetchreq.n_wanted_refs++;
571 len = sizeof(struct got_imsg_fetch_request);
572 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
573 close(fd);
574 return got_error(GOT_ERR_NO_SPACE);
577 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
578 &fetchreq, sizeof(fetchreq)) == -1)
579 return got_error_from_errno(
580 "imsg_compose FETCH_SERVER_PROGRESS");
582 err = flush_imsg(ibuf);
583 if (err) {
584 close(fd);
585 return err;
587 fd = -1;
589 TAILQ_FOREACH(pe, have_refs, entry) {
590 const char *name = pe->path;
591 size_t name_len = pe->path_len;
592 struct got_object_id *id = pe->data;
594 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
595 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
596 if (wbuf == NULL)
597 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
599 /* Keep in sync with struct got_imsg_fetch_have_ref! */
600 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
601 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
602 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
603 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
604 if (imsg_add(wbuf, name, name_len) == -1)
605 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
607 wbuf->fd = -1;
608 imsg_close(ibuf, wbuf);
609 err = flush_imsg(ibuf);
610 if (err)
611 return err;
614 TAILQ_FOREACH(pe, wanted_branches, entry) {
615 const char *name = pe->path;
616 size_t name_len = pe->path_len;
618 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
619 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
620 len);
621 if (wbuf == NULL)
622 return got_error_from_errno(
623 "imsg_create FETCH_WANTED_BRANCH");
625 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
626 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
627 return got_error_from_errno(
628 "imsg_add FETCH_WANTED_BRANCH");
629 if (imsg_add(wbuf, name, name_len) == -1)
630 return got_error_from_errno(
631 "imsg_add FETCH_WANTED_BRANCH");
633 wbuf->fd = -1;
634 imsg_close(ibuf, wbuf);
635 err = flush_imsg(ibuf);
636 if (err)
637 return err;
640 TAILQ_FOREACH(pe, wanted_refs, entry) {
641 const char *name = pe->path;
642 size_t name_len = pe->path_len;
644 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
645 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
646 len);
647 if (wbuf == NULL)
648 return got_error_from_errno(
649 "imsg_create FETCH_WANTED_REF");
651 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
652 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
653 return got_error_from_errno(
654 "imsg_add FETCH_WANTED_REF");
655 if (imsg_add(wbuf, name, name_len) == -1)
656 return got_error_from_errno(
657 "imsg_add FETCH_WANTED_REF");
659 wbuf->fd = -1;
660 imsg_close(ibuf, wbuf);
661 err = flush_imsg(ibuf);
662 if (err)
663 return err;
667 return NULL;
671 const struct got_error *
672 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
674 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
677 const struct got_error *
678 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
679 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
680 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
682 const struct got_error *err = NULL;
683 struct imsg imsg;
684 size_t datalen;
685 struct got_imsg_fetch_symrefs *isymrefs = NULL;
686 size_t n, remain;
687 off_t off;
688 int i;
690 *done = 0;
691 *id = NULL;
692 *refname = NULL;
693 *server_progress = NULL;
694 *packfile_size = 0;
695 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
697 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
698 if (err)
699 return err;
701 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
702 switch (imsg.hdr.type) {
703 case GOT_IMSG_ERROR:
704 if (datalen < sizeof(struct got_imsg_error)) {
705 err = got_error(GOT_ERR_PRIVSEP_LEN);
706 break;
708 err = recv_imsg_error(&imsg, datalen);
709 break;
710 case GOT_IMSG_FETCH_SYMREFS:
711 if (datalen < sizeof(*isymrefs)) {
712 err = got_error(GOT_ERR_PRIVSEP_LEN);
713 break;
715 if (isymrefs != NULL) {
716 err = got_error(GOT_ERR_PRIVSEP_MSG);
717 break;
719 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
720 off = sizeof(*isymrefs);
721 remain = datalen - off;
722 for (n = 0; n < isymrefs->nsymrefs; n++) {
723 struct got_imsg_fetch_symref *s;
724 char *name, *target;
725 if (remain < sizeof(struct got_imsg_fetch_symref)) {
726 err = got_error(GOT_ERR_PRIVSEP_LEN);
727 goto done;
729 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
730 off += sizeof(*s);
731 remain -= sizeof(*s);
732 if (remain < s->name_len) {
733 err = got_error(GOT_ERR_PRIVSEP_LEN);
734 goto done;
736 name = strndup(imsg.data + off, s->name_len);
737 if (name == NULL) {
738 err = got_error_from_errno("strndup");
739 goto done;
741 off += s->name_len;
742 remain -= s->name_len;
743 if (remain < s->target_len) {
744 err = got_error(GOT_ERR_PRIVSEP_LEN);
745 free(name);
746 goto done;
748 target = strndup(imsg.data + off, s->target_len);
749 if (target == NULL) {
750 err = got_error_from_errno("strndup");
751 free(name);
752 goto done;
754 off += s->target_len;
755 remain -= s->target_len;
756 err = got_pathlist_append(symrefs, name, target);
757 if (err) {
758 free(name);
759 free(target);
760 goto done;
763 break;
764 case GOT_IMSG_FETCH_REF:
765 if (datalen <= SHA1_DIGEST_LENGTH) {
766 err = got_error(GOT_ERR_PRIVSEP_MSG);
767 break;
769 *id = malloc(sizeof(**id));
770 if (*id == NULL) {
771 err = got_error_from_errno("malloc");
772 break;
774 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
775 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
776 datalen - SHA1_DIGEST_LENGTH);
777 if (*refname == NULL) {
778 err = got_error_from_errno("strndup");
779 break;
781 break;
782 case GOT_IMSG_FETCH_SERVER_PROGRESS:
783 if (datalen == 0) {
784 err = got_error(GOT_ERR_PRIVSEP_LEN);
785 break;
787 *server_progress = strndup(imsg.data, datalen);
788 if (*server_progress == NULL) {
789 err = got_error_from_errno("strndup");
790 break;
792 for (i = 0; i < datalen; i++) {
793 if (!isprint((unsigned char)(*server_progress)[i]) &&
794 !isspace((unsigned char)(*server_progress)[i])) {
795 err = got_error(GOT_ERR_PRIVSEP_MSG);
796 free(*server_progress);
797 *server_progress = NULL;
798 goto done;
801 break;
802 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
803 if (datalen < sizeof(*packfile_size)) {
804 err = got_error(GOT_ERR_PRIVSEP_MSG);
805 break;
807 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
808 break;
809 case GOT_IMSG_FETCH_DONE:
810 if (datalen != SHA1_DIGEST_LENGTH) {
811 err = got_error(GOT_ERR_PRIVSEP_MSG);
812 break;
814 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
815 *done = 1;
816 break;
817 default:
818 err = got_error(GOT_ERR_PRIVSEP_MSG);
819 break;
821 done:
822 if (err) {
823 free(*id);
824 *id = NULL;
825 free(*refname);
826 *refname = NULL;
828 imsg_free(&imsg);
829 return err;
832 static const struct got_error *
833 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
834 int delete, struct imsgbuf *ibuf)
836 size_t len;
837 struct ibuf *wbuf;
839 len = sizeof(struct got_imsg_send_ref) + name_len;
840 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
841 if (wbuf == NULL)
842 return got_error_from_errno("imsg_create SEND_REF");
844 /* Keep in sync with struct got_imsg_send_ref! */
845 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
846 return got_error_from_errno("imsg_add SEND_REF");
847 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
848 return got_error_from_errno("imsg_add SEND_REF");
849 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
850 return got_error_from_errno("imsg_add SEND_REF");
851 if (imsg_add(wbuf, name, name_len) == -1)
852 return got_error_from_errno("imsg_add SEND_REF");
854 wbuf->fd = -1;
855 imsg_close(ibuf, wbuf);
856 return flush_imsg(ibuf);
859 const struct got_error *
860 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
861 struct got_pathlist_head *have_refs,
862 struct got_pathlist_head *delete_refs,
863 int verbosity)
865 const struct got_error *err = NULL;
866 struct got_pathlist_entry *pe;
867 struct got_imsg_send_request sendreq;
868 struct got_object_id zero_id;
870 memset(&zero_id, 0, sizeof(zero_id));
871 memset(&sendreq, 0, sizeof(sendreq));
872 sendreq.verbosity = verbosity;
873 TAILQ_FOREACH(pe, have_refs, entry)
874 sendreq.nrefs++;
875 TAILQ_FOREACH(pe, delete_refs, entry)
876 sendreq.nrefs++;
877 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
878 &sendreq, sizeof(sendreq)) == -1) {
879 err = got_error_from_errno(
880 "imsg_compose FETCH_SERVER_PROGRESS");
881 goto done;
884 err = flush_imsg(ibuf);
885 if (err)
886 goto done;
887 fd = -1;
889 TAILQ_FOREACH(pe, have_refs, entry) {
890 const char *name = pe->path;
891 size_t name_len = pe->path_len;
892 struct got_object_id *id = pe->data;
893 err = send_send_ref(name, name_len, id, 0, ibuf);
894 if (err)
895 goto done;
898 TAILQ_FOREACH(pe, delete_refs, entry) {
899 const char *name = pe->path;
900 size_t name_len = pe->path_len;
901 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
902 if (err)
903 goto done;
905 done:
906 if (fd != -1 && close(fd) == -1 && err == NULL)
907 err = got_error_from_errno("close");
908 return err;
912 const struct got_error *
913 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
914 struct imsgbuf *ibuf)
916 const struct got_error *err = NULL;
917 struct imsg imsg;
918 size_t datalen;
919 int done = 0;
920 struct got_imsg_send_remote_ref iremote_ref;
921 struct got_object_id *id = NULL;
922 char *refname = NULL;
923 struct got_pathlist_entry *new;
925 while (!done) {
926 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
927 if (err)
928 return err;
929 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
930 switch (imsg.hdr.type) {
931 case GOT_IMSG_ERROR:
932 if (datalen < sizeof(struct got_imsg_error)) {
933 err = got_error(GOT_ERR_PRIVSEP_LEN);
934 goto done;
936 err = recv_imsg_error(&imsg, datalen);
937 goto done;
938 case GOT_IMSG_SEND_REMOTE_REF:
939 if (datalen < sizeof(iremote_ref)) {
940 err = got_error(GOT_ERR_PRIVSEP_MSG);
941 goto done;
943 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
944 if (datalen != sizeof(iremote_ref) +
945 iremote_ref.name_len) {
946 err = got_error(GOT_ERR_PRIVSEP_MSG);
947 goto done;
949 id = malloc(sizeof(*id));
950 if (id == NULL) {
951 err = got_error_from_errno("malloc");
952 goto done;
954 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
955 refname = strndup(imsg.data + sizeof(iremote_ref),
956 datalen - sizeof(iremote_ref));
957 if (refname == NULL) {
958 err = got_error_from_errno("strndup");
959 goto done;
961 err = got_pathlist_insert(&new, remote_refs,
962 refname, id);
963 if (err)
964 goto done;
965 if (new == NULL) { /* duplicate which wasn't inserted */
966 free(id);
967 free(refname);
969 id = NULL;
970 refname = NULL;
971 break;
972 case GOT_IMSG_SEND_PACK_REQUEST:
973 if (datalen != 0) {
974 err = got_error(GOT_ERR_PRIVSEP_MSG);
975 goto done;
977 /* got-send-pack is now waiting for a pack file. */
978 done = 1;
979 break;
980 default:
981 err = got_error(GOT_ERR_PRIVSEP_MSG);
982 break;
985 done:
986 free(id);
987 free(refname);
988 imsg_free(&imsg);
989 return err;
992 const struct got_error *
993 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
995 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
998 const struct got_error *
999 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1000 int *success, char **refname, struct imsgbuf *ibuf)
1002 const struct got_error *err = NULL;
1003 struct imsg imsg;
1004 size_t datalen;
1005 struct got_imsg_send_ref_status iref_status;
1007 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1008 *done = 0;
1009 *success = 0;
1010 *refname = NULL;
1012 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1013 if (err)
1014 return err;
1016 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1017 switch (imsg.hdr.type) {
1018 case GOT_IMSG_ERROR:
1019 if (datalen < sizeof(struct got_imsg_error)) {
1020 err = got_error(GOT_ERR_PRIVSEP_LEN);
1021 break;
1023 err = recv_imsg_error(&imsg, datalen);
1024 break;
1025 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1026 if (datalen < sizeof(*bytes_sent)) {
1027 err = got_error(GOT_ERR_PRIVSEP_MSG);
1028 break;
1030 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1031 break;
1032 case GOT_IMSG_SEND_REF_STATUS:
1033 if (datalen < sizeof(iref_status)) {
1034 err = got_error(GOT_ERR_PRIVSEP_MSG);
1035 break;
1037 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1038 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1039 err = got_error(GOT_ERR_PRIVSEP_MSG);
1040 break;
1042 *success = iref_status.success;
1043 *refname = strndup(imsg.data + sizeof(iref_status),
1044 iref_status.name_len);
1045 break;
1046 case GOT_IMSG_SEND_DONE:
1047 if (datalen != 0) {
1048 err = got_error(GOT_ERR_PRIVSEP_MSG);
1049 break;
1051 *done = 1;
1052 break;
1053 default:
1054 err = got_error(GOT_ERR_PRIVSEP_MSG);
1055 break;
1058 imsg_free(&imsg);
1059 return err;
1062 const struct got_error *
1063 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1064 int fd)
1066 const struct got_error *err = NULL;
1068 /* Keep in sync with struct got_imsg_index_pack_request */
1069 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1070 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1071 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1072 close(fd);
1073 return err;
1075 return flush_imsg(ibuf);
1078 const struct got_error *
1079 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1081 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1084 const struct got_error *
1085 got_privsep_recv_index_progress(int *done, int *nobj_total,
1086 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1087 struct imsgbuf *ibuf)
1089 const struct got_error *err = NULL;
1090 struct imsg imsg;
1091 struct got_imsg_index_pack_progress *iprogress;
1092 size_t datalen;
1094 *done = 0;
1095 *nobj_total = 0;
1096 *nobj_indexed = 0;
1097 *nobj_resolved = 0;
1099 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1100 if (err)
1101 return err;
1103 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1104 switch (imsg.hdr.type) {
1105 case GOT_IMSG_ERROR:
1106 if (datalen < sizeof(struct got_imsg_error)) {
1107 err = got_error(GOT_ERR_PRIVSEP_LEN);
1108 break;
1110 err = recv_imsg_error(&imsg, datalen);
1111 break;
1112 case GOT_IMSG_IDXPACK_PROGRESS:
1113 if (datalen < sizeof(*iprogress)) {
1114 err = got_error(GOT_ERR_PRIVSEP_LEN);
1115 break;
1117 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1118 *nobj_total = iprogress->nobj_total;
1119 *nobj_indexed = iprogress->nobj_indexed;
1120 *nobj_loose = iprogress->nobj_loose;
1121 *nobj_resolved = iprogress->nobj_resolved;
1122 break;
1123 case GOT_IMSG_IDXPACK_DONE:
1124 if (datalen != 0) {
1125 err = got_error(GOT_ERR_PRIVSEP_LEN);
1126 break;
1128 *done = 1;
1129 break;
1130 default:
1131 err = got_error(GOT_ERR_PRIVSEP_MSG);
1132 break;
1135 imsg_free(&imsg);
1136 return err;
1139 const struct got_error *
1140 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1141 struct imsgbuf *ibuf)
1143 struct got_imsg_object *iobj;
1144 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1146 if (datalen != sizeof(*iobj))
1147 return got_error(GOT_ERR_PRIVSEP_LEN);
1148 iobj = imsg->data;
1150 *obj = calloc(1, sizeof(**obj));
1151 if (*obj == NULL)
1152 return got_error_from_errno("calloc");
1154 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1155 (*obj)->type = iobj->type;
1156 (*obj)->flags = iobj->flags;
1157 (*obj)->hdrlen = iobj->hdrlen;
1158 (*obj)->size = iobj->size;
1159 /* path_packfile is handled by caller */
1160 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1161 (*obj)->pack_offset = iobj->pack_offset;
1162 (*obj)->pack_idx = iobj->pack_idx;
1164 STAILQ_INIT(&(*obj)->deltas.entries);
1165 return NULL;
1168 const struct got_error *
1169 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1171 const struct got_error *err = NULL;
1172 struct imsg imsg;
1173 const size_t min_datalen =
1174 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1176 *obj = NULL;
1178 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1179 if (err)
1180 return err;
1182 switch (imsg.hdr.type) {
1183 case GOT_IMSG_OBJECT:
1184 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1185 break;
1186 default:
1187 err = got_error(GOT_ERR_PRIVSEP_MSG);
1188 break;
1191 imsg_free(&imsg);
1193 return err;
1196 static const struct got_error *
1197 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1198 size_t logmsg_len)
1200 const struct got_error *err = NULL;
1201 size_t offset, remain;
1203 offset = 0;
1204 remain = logmsg_len;
1205 while (remain > 0) {
1206 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1208 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1209 commit->logmsg + offset, n) == -1) {
1210 err = got_error_from_errno("imsg_compose "
1211 "COMMIT_LOGMSG");
1212 break;
1215 err = flush_imsg(ibuf);
1216 if (err)
1217 break;
1219 offset += n;
1220 remain -= n;
1223 return err;
1226 const struct got_error *
1227 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1229 const struct got_error *err = NULL;
1230 struct got_imsg_commit_object *icommit;
1231 uint8_t *buf;
1232 size_t len, total;
1233 struct got_object_qid *qid;
1234 size_t author_len = strlen(commit->author);
1235 size_t committer_len = strlen(commit->committer);
1236 size_t logmsg_len = strlen(commit->logmsg);
1238 total = sizeof(*icommit) + author_len + committer_len +
1239 commit->nparents * SHA1_DIGEST_LENGTH;
1241 buf = malloc(total);
1242 if (buf == NULL)
1243 return got_error_from_errno("malloc");
1245 icommit = (struct got_imsg_commit_object *)buf;
1246 memcpy(icommit->tree_id, commit->tree_id->sha1,
1247 sizeof(icommit->tree_id));
1248 icommit->author_len = author_len;
1249 icommit->author_time = commit->author_time;
1250 icommit->author_gmtoff = commit->author_gmtoff;
1251 icommit->committer_len = committer_len;
1252 icommit->committer_time = commit->committer_time;
1253 icommit->committer_gmtoff = commit->committer_gmtoff;
1254 icommit->logmsg_len = logmsg_len;
1255 icommit->nparents = commit->nparents;
1257 len = sizeof(*icommit);
1258 memcpy(buf + len, commit->author, author_len);
1259 len += author_len;
1260 memcpy(buf + len, commit->committer, committer_len);
1261 len += committer_len;
1262 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1263 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1264 len += SHA1_DIGEST_LENGTH;
1267 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1268 err = got_error_from_errno("imsg_compose COMMIT");
1269 goto done;
1272 if (logmsg_len == 0 ||
1273 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1274 err = flush_imsg(ibuf);
1275 if (err)
1276 goto done;
1278 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1279 done:
1280 free(buf);
1281 return err;
1284 static const struct got_error *
1285 get_commit_from_imsg(struct got_commit_object **commit,
1286 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1288 const struct got_error *err = NULL;
1289 struct got_imsg_commit_object *icommit;
1290 size_t len = 0;
1291 int i;
1293 if (datalen < sizeof(*icommit))
1294 return got_error(GOT_ERR_PRIVSEP_LEN);
1296 icommit = imsg->data;
1297 if (datalen != sizeof(*icommit) + icommit->author_len +
1298 icommit->committer_len +
1299 icommit->nparents * SHA1_DIGEST_LENGTH)
1300 return got_error(GOT_ERR_PRIVSEP_LEN);
1302 if (icommit->nparents < 0)
1303 return got_error(GOT_ERR_PRIVSEP_LEN);
1305 len += sizeof(*icommit);
1307 *commit = got_object_commit_alloc_partial();
1308 if (*commit == NULL)
1309 return got_error_from_errno(
1310 "got_object_commit_alloc_partial");
1312 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1313 SHA1_DIGEST_LENGTH);
1314 (*commit)->author_time = icommit->author_time;
1315 (*commit)->author_gmtoff = icommit->author_gmtoff;
1316 (*commit)->committer_time = icommit->committer_time;
1317 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1319 if (icommit->author_len == 0) {
1320 (*commit)->author = strdup("");
1321 if ((*commit)->author == NULL) {
1322 err = got_error_from_errno("strdup");
1323 goto done;
1325 } else {
1326 (*commit)->author = malloc(icommit->author_len + 1);
1327 if ((*commit)->author == NULL) {
1328 err = got_error_from_errno("malloc");
1329 goto done;
1331 memcpy((*commit)->author, imsg->data + len,
1332 icommit->author_len);
1333 (*commit)->author[icommit->author_len] = '\0';
1335 len += icommit->author_len;
1337 if (icommit->committer_len == 0) {
1338 (*commit)->committer = strdup("");
1339 if ((*commit)->committer == NULL) {
1340 err = got_error_from_errno("strdup");
1341 goto done;
1343 } else {
1344 (*commit)->committer =
1345 malloc(icommit->committer_len + 1);
1346 if ((*commit)->committer == NULL) {
1347 err = got_error_from_errno("malloc");
1348 goto done;
1350 memcpy((*commit)->committer, imsg->data + len,
1351 icommit->committer_len);
1352 (*commit)->committer[icommit->committer_len] = '\0';
1354 len += icommit->committer_len;
1356 if (icommit->logmsg_len == 0) {
1357 (*commit)->logmsg = strdup("");
1358 if ((*commit)->logmsg == NULL) {
1359 err = got_error_from_errno("strdup");
1360 goto done;
1362 } else {
1363 size_t offset = 0, remain = icommit->logmsg_len;
1365 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1366 if ((*commit)->logmsg == NULL) {
1367 err = got_error_from_errno("malloc");
1368 goto done;
1370 while (remain > 0) {
1371 struct imsg imsg_log;
1372 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1373 remain);
1375 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1376 if (err)
1377 goto done;
1379 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1380 err = got_error(GOT_ERR_PRIVSEP_MSG);
1381 goto done;
1384 memcpy((*commit)->logmsg + offset,
1385 imsg_log.data, n);
1386 imsg_free(&imsg_log);
1387 offset += n;
1388 remain -= n;
1390 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1393 for (i = 0; i < icommit->nparents; i++) {
1394 struct got_object_qid *qid;
1396 err = got_object_qid_alloc_partial(&qid);
1397 if (err)
1398 break;
1399 memcpy(&qid->id, imsg->data + len +
1400 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1401 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1402 (*commit)->nparents++;
1404 done:
1405 if (err) {
1406 got_object_commit_close(*commit);
1407 *commit = NULL;
1409 return err;
1412 const struct got_error *
1413 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1415 const struct got_error *err = NULL;
1416 struct imsg imsg;
1417 size_t datalen;
1418 const size_t min_datalen =
1419 MIN(sizeof(struct got_imsg_error),
1420 sizeof(struct got_imsg_commit_object));
1422 *commit = NULL;
1424 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1425 if (err)
1426 return err;
1428 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1430 switch (imsg.hdr.type) {
1431 case GOT_IMSG_COMMIT:
1432 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1433 break;
1434 default:
1435 err = got_error(GOT_ERR_PRIVSEP_MSG);
1436 break;
1439 imsg_free(&imsg);
1441 return err;
1444 static const struct got_error *
1445 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1446 int idx0, int idxN, size_t len)
1448 struct ibuf *wbuf;
1449 struct got_imsg_tree_entries ientries;
1450 int i;
1452 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1453 if (wbuf == NULL)
1454 return got_error_from_errno("imsg_create TREE_ENTRY");
1456 ientries.nentries = idxN - idx0 + 1;
1457 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1458 return got_error_from_errno("imsg_add TREE_ENTRY");
1460 for (i = idx0; i <= idxN; i++) {
1461 struct got_parsed_tree_entry *pte = &entries[i];
1463 /* Keep in sync with struct got_imsg_tree_object definition! */
1464 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1465 return got_error_from_errno("imsg_add TREE_ENTRY");
1466 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1467 return got_error_from_errno("imsg_add TREE_ENTRY");
1468 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1469 return got_error_from_errno("imsg_add TREE_ENTRY");
1471 /* Remaining bytes are the entry's name. */
1472 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1473 return got_error_from_errno("imsg_add TREE_ENTRY");
1476 wbuf->fd = -1;
1477 imsg_close(ibuf, wbuf);
1478 return NULL;
1481 const struct got_error *
1482 got_privsep_send_tree(struct imsgbuf *ibuf,
1483 struct got_parsed_tree_entry *entries, int nentries)
1485 const struct got_error *err = NULL;
1486 struct got_imsg_tree_object itree;
1487 size_t entries_len;
1488 int i, j;
1490 itree.nentries = nentries;
1491 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1492 == -1)
1493 return got_error_from_errno("imsg_compose TREE");
1495 entries_len = sizeof(struct got_imsg_tree_entries);
1496 i = 0;
1497 for (j = 0; j < nentries; j++) {
1498 struct got_parsed_tree_entry *pte = &entries[j];
1499 size_t len = sizeof(*pte) + pte->namelen;
1501 if (j > 0 &&
1502 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1503 err = send_tree_entries(ibuf, entries,
1504 i, j - 1, entries_len);
1505 if (err)
1506 return err;
1507 i = j;
1508 entries_len = sizeof(struct got_imsg_tree_entries);
1511 entries_len += len;
1514 if (j > 0) {
1515 err = send_tree_entries(ibuf, entries, i, j - 1, entries_len);
1516 if (err)
1517 return err;
1520 return flush_imsg(ibuf);
1523 const struct got_error *
1524 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1526 const struct got_error *err = NULL;
1527 const size_t min_datalen =
1528 MIN(sizeof(struct got_imsg_error),
1529 sizeof(struct got_imsg_tree_object));
1530 struct got_imsg_tree_object *itree;
1531 size_t i;
1532 int nentries = 0;
1534 *tree = NULL;
1536 err = read_imsg(ibuf);
1537 if (err)
1538 goto done;
1540 for (;;) {
1541 struct imsg imsg;
1542 size_t n;
1543 size_t datalen;
1544 struct got_imsg_tree_entries *ientries;
1545 struct got_tree_entry *te = NULL;
1546 size_t te_offset;
1548 n = imsg_get(ibuf, &imsg);
1549 if (n == 0) {
1550 if ((*tree)) {
1551 if (nentries < (*tree)->nentries) {
1552 err = read_imsg(ibuf);
1553 if (err)
1554 break;
1555 continue;
1556 } else
1557 break;
1558 } else {
1559 err = got_error(GOT_ERR_PRIVSEP_MSG);
1560 break;
1564 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1565 imsg_free(&imsg);
1566 err = got_error(GOT_ERR_PRIVSEP_LEN);
1567 break;
1570 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1572 switch (imsg.hdr.type) {
1573 case GOT_IMSG_ERROR:
1574 err = recv_imsg_error(&imsg, datalen);
1575 break;
1576 case GOT_IMSG_TREE:
1577 /* This message should only appear once. */
1578 if (*tree != NULL) {
1579 err = got_error(GOT_ERR_PRIVSEP_MSG);
1580 break;
1582 if (datalen != sizeof(*itree)) {
1583 err = got_error(GOT_ERR_PRIVSEP_LEN);
1584 break;
1586 itree = imsg.data;
1587 if (itree->nentries < 0) {
1588 err = got_error(GOT_ERR_PRIVSEP_LEN);
1589 break;
1591 *tree = malloc(sizeof(**tree));
1592 if (*tree == NULL) {
1593 err = got_error_from_errno("malloc");
1594 break;
1596 (*tree)->entries = calloc(itree->nentries,
1597 sizeof(struct got_tree_entry));
1598 if ((*tree)->entries == NULL) {
1599 err = got_error_from_errno("malloc");
1600 free(*tree);
1601 *tree = NULL;
1602 break;
1604 (*tree)->nentries = itree->nentries;
1605 (*tree)->refcnt = 0;
1606 break;
1607 case GOT_IMSG_TREE_ENTRIES:
1608 /* This message should be preceeded by GOT_IMSG_TREE. */
1609 if (*tree == NULL) {
1610 err = got_error(GOT_ERR_PRIVSEP_MSG);
1611 break;
1613 if (datalen <= sizeof(*ientries) ||
1614 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1615 err = got_error(GOT_ERR_PRIVSEP_LEN);
1616 break;
1619 ientries = imsg.data;
1620 if (ientries->nentries > INT_MAX) {
1621 err = got_error_msg(GOT_ERR_NO_SPACE,
1622 "too many tree entries");
1623 break;
1625 te_offset = sizeof(*ientries);
1626 for (i = 0; i < ientries->nentries; i++) {
1627 struct got_imsg_tree_entry ite;
1628 const char *te_name;
1629 uint8_t *buf = imsg.data + te_offset;
1631 if (te_offset >= datalen) {
1632 err = got_error(GOT_ERR_PRIVSEP_LEN);
1633 break;
1636 /* Might not be aligned, size is ~32 bytes. */
1637 memcpy(&ite, buf, sizeof(ite));
1639 if (ite.namelen >= sizeof(te->name)) {
1640 err = got_error(GOT_ERR_PRIVSEP_LEN);
1641 break;
1643 if (te_offset + sizeof(ite) + ite.namelen >
1644 datalen) {
1645 err = got_error(GOT_ERR_PRIVSEP_LEN);
1646 break;
1648 if (nentries >= (*tree)->nentries) {
1649 err = got_error(GOT_ERR_PRIVSEP_LEN);
1650 break;
1652 te = &(*tree)->entries[nentries];
1653 te_name = buf + sizeof(ite);
1654 memcpy(te->name, te_name, ite.namelen);
1655 te->name[ite.namelen] = '\0';
1656 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1657 te->mode = ite.mode;
1658 te->idx = nentries;
1659 nentries++;
1661 te_offset += sizeof(ite) + ite.namelen;
1663 break;
1664 default:
1665 err = got_error(GOT_ERR_PRIVSEP_MSG);
1666 break;
1669 imsg_free(&imsg);
1670 if (err)
1671 break;
1673 done:
1674 if (*tree && (*tree)->nentries != nentries) {
1675 if (err == NULL)
1676 err = got_error(GOT_ERR_PRIVSEP_LEN);
1677 got_object_tree_close(*tree);
1678 *tree = NULL;
1681 return err;
1684 const struct got_error *
1685 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1686 const uint8_t *data)
1688 struct got_imsg_blob iblob;
1690 iblob.size = size;
1691 iblob.hdrlen = hdrlen;
1693 if (data) {
1694 uint8_t *buf;
1696 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1697 return got_error(GOT_ERR_NO_SPACE);
1699 buf = malloc(sizeof(iblob) + size);
1700 if (buf == NULL)
1701 return got_error_from_errno("malloc");
1703 memcpy(buf, &iblob, sizeof(iblob));
1704 memcpy(buf + sizeof(iblob), data, size);
1705 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1706 sizeof(iblob) + size) == -1) {
1707 free(buf);
1708 return got_error_from_errno("imsg_compose BLOB");
1710 free(buf);
1711 } else {
1712 /* Data has already been written to file descriptor. */
1713 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1714 sizeof(iblob)) == -1)
1715 return got_error_from_errno("imsg_compose BLOB");
1719 return flush_imsg(ibuf);
1722 const struct got_error *
1723 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1724 struct imsgbuf *ibuf)
1726 const struct got_error *err = NULL;
1727 struct imsg imsg;
1728 struct got_imsg_blob *iblob;
1729 size_t datalen;
1731 *outbuf = NULL;
1733 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1734 if (err)
1735 return err;
1737 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1739 switch (imsg.hdr.type) {
1740 case GOT_IMSG_BLOB:
1741 if (datalen < sizeof(*iblob)) {
1742 err = got_error(GOT_ERR_PRIVSEP_LEN);
1743 break;
1745 iblob = imsg.data;
1746 *size = iblob->size;
1747 *hdrlen = iblob->hdrlen;
1749 if (datalen == sizeof(*iblob)) {
1750 /* Data has been written to file descriptor. */
1751 break;
1754 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1755 err = got_error(GOT_ERR_PRIVSEP_LEN);
1756 break;
1759 *outbuf = malloc(*size);
1760 if (*outbuf == NULL) {
1761 err = got_error_from_errno("malloc");
1762 break;
1764 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1765 break;
1766 default:
1767 err = got_error(GOT_ERR_PRIVSEP_MSG);
1768 break;
1771 imsg_free(&imsg);
1773 return err;
1776 static const struct got_error *
1777 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1779 const struct got_error *err = NULL;
1780 size_t offset, remain;
1782 offset = 0;
1783 remain = tagmsg_len;
1784 while (remain > 0) {
1785 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1787 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1788 tag->tagmsg + offset, n) == -1) {
1789 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1790 break;
1793 err = flush_imsg(ibuf);
1794 if (err)
1795 break;
1797 offset += n;
1798 remain -= n;
1801 return err;
1804 const struct got_error *
1805 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1807 const struct got_error *err = NULL;
1808 struct got_imsg_tag_object *itag;
1809 uint8_t *buf;
1810 size_t len, total;
1811 size_t tag_len = strlen(tag->tag);
1812 size_t tagger_len = strlen(tag->tagger);
1813 size_t tagmsg_len = strlen(tag->tagmsg);
1815 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1817 buf = malloc(total);
1818 if (buf == NULL)
1819 return got_error_from_errno("malloc");
1821 itag = (struct got_imsg_tag_object *)buf;
1822 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1823 itag->obj_type = tag->obj_type;
1824 itag->tag_len = tag_len;
1825 itag->tagger_len = tagger_len;
1826 itag->tagger_time = tag->tagger_time;
1827 itag->tagger_gmtoff = tag->tagger_gmtoff;
1828 itag->tagmsg_len = tagmsg_len;
1830 len = sizeof(*itag);
1831 memcpy(buf + len, tag->tag, tag_len);
1832 len += tag_len;
1833 memcpy(buf + len, tag->tagger, tagger_len);
1834 len += tagger_len;
1836 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1837 err = got_error_from_errno("imsg_compose TAG");
1838 goto done;
1841 if (tagmsg_len == 0 ||
1842 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1843 err = flush_imsg(ibuf);
1844 if (err)
1845 goto done;
1847 err = send_tagmsg(ibuf, tag, tagmsg_len);
1848 done:
1849 free(buf);
1850 return err;
1853 const struct got_error *
1854 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1856 const struct got_error *err = NULL;
1857 struct imsg imsg;
1858 struct got_imsg_tag_object *itag;
1859 size_t len, datalen;
1860 const size_t min_datalen =
1861 MIN(sizeof(struct got_imsg_error),
1862 sizeof(struct got_imsg_tag_object));
1864 *tag = NULL;
1866 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1867 if (err)
1868 return err;
1870 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1871 len = 0;
1873 switch (imsg.hdr.type) {
1874 case GOT_IMSG_TAG:
1875 if (datalen < sizeof(*itag)) {
1876 err = got_error(GOT_ERR_PRIVSEP_LEN);
1877 break;
1879 itag = imsg.data;
1880 if (datalen != sizeof(*itag) + itag->tag_len +
1881 itag->tagger_len) {
1882 err = got_error(GOT_ERR_PRIVSEP_LEN);
1883 break;
1885 len += sizeof(*itag);
1887 *tag = calloc(1, sizeof(**tag));
1888 if (*tag == NULL) {
1889 err = got_error_from_errno("calloc");
1890 break;
1893 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1895 if (itag->tag_len == 0) {
1896 (*tag)->tag = strdup("");
1897 if ((*tag)->tag == NULL) {
1898 err = got_error_from_errno("strdup");
1899 break;
1901 } else {
1902 (*tag)->tag = malloc(itag->tag_len + 1);
1903 if ((*tag)->tag == NULL) {
1904 err = got_error_from_errno("malloc");
1905 break;
1907 memcpy((*tag)->tag, imsg.data + len,
1908 itag->tag_len);
1909 (*tag)->tag[itag->tag_len] = '\0';
1911 len += itag->tag_len;
1913 (*tag)->obj_type = itag->obj_type;
1914 (*tag)->tagger_time = itag->tagger_time;
1915 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1917 if (itag->tagger_len == 0) {
1918 (*tag)->tagger = strdup("");
1919 if ((*tag)->tagger == NULL) {
1920 err = got_error_from_errno("strdup");
1921 break;
1923 } else {
1924 (*tag)->tagger = malloc(itag->tagger_len + 1);
1925 if ((*tag)->tagger == NULL) {
1926 err = got_error_from_errno("malloc");
1927 break;
1929 memcpy((*tag)->tagger, imsg.data + len,
1930 itag->tagger_len);
1931 (*tag)->tagger[itag->tagger_len] = '\0';
1933 len += itag->tagger_len;
1935 if (itag->tagmsg_len == 0) {
1936 (*tag)->tagmsg = strdup("");
1937 if ((*tag)->tagmsg == NULL) {
1938 err = got_error_from_errno("strdup");
1939 break;
1941 } else {
1942 size_t offset = 0, remain = itag->tagmsg_len;
1944 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1945 if ((*tag)->tagmsg == NULL) {
1946 err = got_error_from_errno("malloc");
1947 break;
1949 while (remain > 0) {
1950 struct imsg imsg_log;
1951 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1952 remain);
1954 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1955 if (err)
1956 return err;
1958 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1959 return got_error(GOT_ERR_PRIVSEP_MSG);
1961 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1962 n);
1963 imsg_free(&imsg_log);
1964 offset += n;
1965 remain -= n;
1967 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1970 break;
1971 default:
1972 err = got_error(GOT_ERR_PRIVSEP_MSG);
1973 break;
1976 imsg_free(&imsg);
1978 return err;
1981 const struct got_error *
1982 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1983 struct got_packidx *packidx)
1985 const struct got_error *err = NULL;
1986 struct got_imsg_packidx ipackidx;
1987 struct got_imsg_pack ipack;
1988 int fd;
1990 ipackidx.len = packidx->len;
1991 ipackidx.packfile_size = pack->filesize;
1992 fd = dup(packidx->fd);
1993 if (fd == -1)
1994 return got_error_from_errno("dup");
1996 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1997 sizeof(ipackidx)) == -1) {
1998 err = got_error_from_errno("imsg_compose PACKIDX");
1999 close(fd);
2000 return err;
2003 if (strlcpy(ipack.path_packfile, pack->path_packfile,
2004 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
2005 return got_error(GOT_ERR_NO_SPACE);
2006 ipack.filesize = pack->filesize;
2008 fd = dup(pack->fd);
2009 if (fd == -1)
2010 return got_error_from_errno("dup");
2012 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2013 == -1) {
2014 err = got_error_from_errno("imsg_compose PACK");
2015 close(fd);
2016 return err;
2019 return flush_imsg(ibuf);
2022 const struct got_error *
2023 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2024 struct got_object_id *id)
2026 struct got_imsg_packed_object iobj;
2028 iobj.idx = idx;
2029 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2031 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2032 &iobj, sizeof(iobj)) == -1)
2033 return got_error_from_errno("imsg_compose "
2034 "PACKED_OBJECT_REQUEST");
2036 return flush_imsg(ibuf);
2039 const struct got_error *
2040 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2041 struct got_object_id *id)
2043 struct got_imsg_packed_object iobj;
2045 iobj.idx = idx;
2046 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2048 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2049 &iobj, sizeof(iobj)) == -1)
2050 return got_error_from_errno("imsg_compose "
2051 "PACKED_OBJECT_REQUEST");
2053 return flush_imsg(ibuf);
2056 const struct got_error *
2057 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2059 const struct got_error *err = NULL;
2061 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2062 NULL, 0) == -1) {
2063 err = got_error_from_errno("imsg_compose "
2064 "GITCONFIG_PARSE_REQUEST");
2065 close(fd);
2066 return err;
2069 return flush_imsg(ibuf);
2072 const struct got_error *
2073 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2075 if (imsg_compose(ibuf,
2076 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2077 NULL, 0) == -1)
2078 return got_error_from_errno("imsg_compose "
2079 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2081 return flush_imsg(ibuf);
2084 const struct got_error *
2085 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2087 if (imsg_compose(ibuf,
2088 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2089 NULL, 0) == -1)
2090 return got_error_from_errno("imsg_compose "
2091 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2093 return flush_imsg(ibuf);
2097 const struct got_error *
2098 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2100 if (imsg_compose(ibuf,
2101 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2102 return got_error_from_errno("imsg_compose "
2103 "GITCONFIG_AUTHOR_NAME_REQUEST");
2105 return flush_imsg(ibuf);
2108 const struct got_error *
2109 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2111 if (imsg_compose(ibuf,
2112 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2113 return got_error_from_errno("imsg_compose "
2114 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2116 return flush_imsg(ibuf);
2119 const struct got_error *
2120 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2122 if (imsg_compose(ibuf,
2123 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2124 return got_error_from_errno("imsg_compose "
2125 "GITCONFIG_REMOTE_REQUEST");
2127 return flush_imsg(ibuf);
2130 const struct got_error *
2131 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2133 if (imsg_compose(ibuf,
2134 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2135 return got_error_from_errno("imsg_compose "
2136 "GITCONFIG_OWNER_REQUEST");
2138 return flush_imsg(ibuf);
2141 const struct got_error *
2142 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2144 const struct got_error *err = NULL;
2145 struct imsg imsg;
2146 size_t datalen;
2147 const size_t min_datalen = 0;
2149 *str = NULL;
2151 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2152 if (err)
2153 return err;
2154 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2156 switch (imsg.hdr.type) {
2157 case GOT_IMSG_GITCONFIG_STR_VAL:
2158 if (datalen == 0)
2159 break;
2160 /* datalen does not include terminating \0 */
2161 *str = malloc(datalen + 1);
2162 if (*str == NULL) {
2163 err = got_error_from_errno("malloc");
2164 break;
2166 memcpy(*str, imsg.data, datalen);
2167 (*str)[datalen] = '\0';
2168 break;
2169 default:
2170 err = got_error(GOT_ERR_PRIVSEP_MSG);
2171 break;
2174 imsg_free(&imsg);
2175 return err;
2178 const struct got_error *
2179 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2181 const struct got_error *err = NULL;
2182 struct imsg imsg;
2183 size_t datalen;
2184 const size_t min_datalen =
2185 MIN(sizeof(struct got_imsg_error), sizeof(int));
2187 *val = 0;
2189 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2190 if (err)
2191 return err;
2192 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2194 switch (imsg.hdr.type) {
2195 case GOT_IMSG_GITCONFIG_INT_VAL:
2196 if (datalen != sizeof(*val)) {
2197 err = got_error(GOT_ERR_PRIVSEP_LEN);
2198 break;
2200 memcpy(val, imsg.data, sizeof(*val));
2201 break;
2202 default:
2203 err = got_error(GOT_ERR_PRIVSEP_MSG);
2204 break;
2207 imsg_free(&imsg);
2208 return err;
2211 static void
2212 free_remote_data(struct got_remote_repo *remote)
2214 int i;
2216 free(remote->name);
2217 free(remote->fetch_url);
2218 free(remote->send_url);
2219 for (i = 0; i < remote->nfetch_branches; i++)
2220 free(remote->fetch_branches[i]);
2221 free(remote->fetch_branches);
2222 for (i = 0; i < remote->nsend_branches; i++)
2223 free(remote->send_branches[i]);
2224 free(remote->send_branches);
2225 for (i = 0; i < remote->nfetch_refs; i++)
2226 free(remote->fetch_refs[i]);
2227 free(remote->fetch_refs);
2230 const struct got_error *
2231 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2232 int *nremotes, struct imsgbuf *ibuf)
2234 const struct got_error *err = NULL;
2235 struct imsg imsg;
2236 size_t datalen;
2237 struct got_imsg_remotes iremotes;
2238 struct got_imsg_remote iremote;
2240 *remotes = NULL;
2241 *nremotes = 0;
2242 iremotes.nremotes = 0;
2244 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2245 if (err)
2246 return err;
2247 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2249 switch (imsg.hdr.type) {
2250 case GOT_IMSG_GITCONFIG_REMOTES:
2251 if (datalen != sizeof(iremotes)) {
2252 err = got_error(GOT_ERR_PRIVSEP_LEN);
2253 break;
2255 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2256 if (iremotes.nremotes == 0) {
2257 imsg_free(&imsg);
2258 return NULL;
2260 break;
2261 default:
2262 imsg_free(&imsg);
2263 return got_error(GOT_ERR_PRIVSEP_MSG);
2266 imsg_free(&imsg);
2268 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2269 if (*remotes == NULL)
2270 return got_error_from_errno("recallocarray");
2272 while (*nremotes < iremotes.nremotes) {
2273 struct got_remote_repo *remote;
2275 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2276 if (err)
2277 break;
2278 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2280 switch (imsg.hdr.type) {
2281 case GOT_IMSG_GITCONFIG_REMOTE:
2282 remote = &(*remotes)[*nremotes];
2283 memset(remote, 0, sizeof(*remote));
2284 if (datalen < sizeof(iremote)) {
2285 err = got_error(GOT_ERR_PRIVSEP_LEN);
2286 break;
2288 memcpy(&iremote, imsg.data, sizeof(iremote));
2289 if (iremote.name_len == 0 ||
2290 iremote.fetch_url_len == 0 ||
2291 iremote.send_url_len == 0 ||
2292 (sizeof(iremote) + iremote.name_len +
2293 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2294 err = got_error(GOT_ERR_PRIVSEP_LEN);
2295 break;
2297 remote->name = strndup(imsg.data + sizeof(iremote),
2298 iremote.name_len);
2299 if (remote->name == NULL) {
2300 err = got_error_from_errno("strndup");
2301 break;
2303 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2304 iremote.name_len, iremote.fetch_url_len);
2305 if (remote->fetch_url == NULL) {
2306 err = got_error_from_errno("strndup");
2307 free_remote_data(remote);
2308 break;
2310 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2311 iremote.name_len + iremote.fetch_url_len,
2312 iremote.send_url_len);
2313 if (remote->send_url == NULL) {
2314 err = got_error_from_errno("strndup");
2315 free_remote_data(remote);
2316 break;
2318 remote->mirror_references = iremote.mirror_references;
2319 remote->fetch_all_branches = iremote.fetch_all_branches;
2320 remote->nfetch_branches = 0;
2321 remote->fetch_branches = NULL;
2322 remote->nsend_branches = 0;
2323 remote->send_branches = NULL;
2324 remote->nfetch_refs = 0;
2325 remote->fetch_refs = NULL;
2326 (*nremotes)++;
2327 break;
2328 default:
2329 err = got_error(GOT_ERR_PRIVSEP_MSG);
2330 break;
2333 imsg_free(&imsg);
2334 if (err)
2335 break;
2338 if (err) {
2339 int i;
2340 for (i = 0; i < *nremotes; i++)
2341 free_remote_data(&(*remotes)[i]);
2342 free(*remotes);
2343 *remotes = NULL;
2344 *nremotes = 0;
2346 return err;
2349 const struct got_error *
2350 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2352 const struct got_error *err = NULL;
2354 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2355 NULL, 0) == -1) {
2356 err = got_error_from_errno("imsg_compose "
2357 "GOTCONFIG_PARSE_REQUEST");
2358 close(fd);
2359 return err;
2362 return flush_imsg(ibuf);
2365 const struct got_error *
2366 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2368 if (imsg_compose(ibuf,
2369 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2370 return got_error_from_errno("imsg_compose "
2371 "GOTCONFIG_AUTHOR_REQUEST");
2373 return flush_imsg(ibuf);
2376 const struct got_error *
2377 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2379 if (imsg_compose(ibuf,
2380 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2381 return got_error_from_errno("imsg_compose "
2382 "GOTCONFIG_REMOTE_REQUEST");
2384 return flush_imsg(ibuf);
2387 const struct got_error *
2388 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2390 const struct got_error *err = NULL;
2391 struct imsg imsg;
2392 size_t datalen;
2393 const size_t min_datalen = 0;
2395 *str = NULL;
2397 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2398 if (err)
2399 return err;
2400 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2402 switch (imsg.hdr.type) {
2403 case GOT_IMSG_ERROR:
2404 if (datalen < sizeof(struct got_imsg_error)) {
2405 err = got_error(GOT_ERR_PRIVSEP_LEN);
2406 break;
2408 err = recv_imsg_error(&imsg, datalen);
2409 break;
2410 case GOT_IMSG_GOTCONFIG_STR_VAL:
2411 if (datalen == 0)
2412 break;
2413 /* datalen does not include terminating \0 */
2414 *str = malloc(datalen + 1);
2415 if (*str == NULL) {
2416 err = got_error_from_errno("malloc");
2417 break;
2419 memcpy(*str, imsg.data, datalen);
2420 (*str)[datalen] = '\0';
2421 break;
2422 default:
2423 err = got_error(GOT_ERR_PRIVSEP_MSG);
2424 break;
2427 imsg_free(&imsg);
2428 return err;
2431 const struct got_error *
2432 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2433 int *nremotes, struct imsgbuf *ibuf)
2435 const struct got_error *err = NULL;
2436 struct imsg imsg;
2437 size_t datalen;
2438 struct got_imsg_remotes iremotes;
2439 struct got_imsg_remote iremote;
2440 const size_t min_datalen =
2441 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2443 *remotes = NULL;
2444 *nremotes = 0;
2445 iremotes.nremotes = 0;
2447 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2448 if (err)
2449 return err;
2450 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2452 switch (imsg.hdr.type) {
2453 case GOT_IMSG_ERROR:
2454 if (datalen < sizeof(struct got_imsg_error)) {
2455 err = got_error(GOT_ERR_PRIVSEP_LEN);
2456 break;
2458 err = recv_imsg_error(&imsg, datalen);
2459 break;
2460 case GOT_IMSG_GOTCONFIG_REMOTES:
2461 if (datalen != sizeof(iremotes)) {
2462 err = got_error(GOT_ERR_PRIVSEP_LEN);
2463 break;
2465 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2466 if (iremotes.nremotes == 0) {
2467 imsg_free(&imsg);
2468 return NULL;
2470 break;
2471 default:
2472 imsg_free(&imsg);
2473 return got_error(GOT_ERR_PRIVSEP_MSG);
2476 imsg_free(&imsg);
2478 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2479 if (*remotes == NULL)
2480 return got_error_from_errno("recallocarray");
2482 while (*nremotes < iremotes.nremotes) {
2483 struct got_remote_repo *remote;
2484 const size_t min_datalen =
2485 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2486 int i;
2488 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2489 if (err)
2490 break;
2491 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2493 switch (imsg.hdr.type) {
2494 case GOT_IMSG_ERROR:
2495 if (datalen < sizeof(struct got_imsg_error)) {
2496 err = got_error(GOT_ERR_PRIVSEP_LEN);
2497 break;
2499 err = recv_imsg_error(&imsg, datalen);
2500 break;
2501 case GOT_IMSG_GOTCONFIG_REMOTE:
2502 remote = &(*remotes)[*nremotes];
2503 memset(remote, 0, sizeof(*remote));
2504 if (datalen < sizeof(iremote)) {
2505 err = got_error(GOT_ERR_PRIVSEP_LEN);
2506 break;
2508 memcpy(&iremote, imsg.data, sizeof(iremote));
2509 if (iremote.name_len == 0 ||
2510 (iremote.fetch_url_len == 0 &&
2511 iremote.send_url_len == 0) ||
2512 (sizeof(iremote) + iremote.name_len +
2513 iremote.fetch_url_len + iremote.send_url_len) >
2514 datalen) {
2515 err = got_error(GOT_ERR_PRIVSEP_LEN);
2516 break;
2518 remote->name = strndup(imsg.data + sizeof(iremote),
2519 iremote.name_len);
2520 if (remote->name == NULL) {
2521 err = got_error_from_errno("strndup");
2522 break;
2524 remote->fetch_url = strndup(imsg.data +
2525 sizeof(iremote) + iremote.name_len,
2526 iremote.fetch_url_len);
2527 if (remote->fetch_url == NULL) {
2528 err = got_error_from_errno("strndup");
2529 free_remote_data(remote);
2530 break;
2532 remote->send_url = strndup(imsg.data +
2533 sizeof(iremote) + iremote.name_len +
2534 iremote.fetch_url_len, iremote.send_url_len);
2535 if (remote->send_url == NULL) {
2536 err = got_error_from_errno("strndup");
2537 free_remote_data(remote);
2538 break;
2540 remote->mirror_references = iremote.mirror_references;
2541 remote->fetch_all_branches = iremote.fetch_all_branches;
2542 if (iremote.nfetch_branches > 0) {
2543 remote->fetch_branches = recallocarray(NULL, 0,
2544 iremote.nfetch_branches, sizeof(char *));
2545 if (remote->fetch_branches == NULL) {
2546 err = got_error_from_errno("calloc");
2547 free_remote_data(remote);
2548 break;
2551 remote->nfetch_branches = 0;
2552 for (i = 0; i < iremote.nfetch_branches; i++) {
2553 char *branch;
2554 err = got_privsep_recv_gotconfig_str(&branch,
2555 ibuf);
2556 if (err) {
2557 free_remote_data(remote);
2558 goto done;
2560 remote->fetch_branches[i] = branch;
2561 remote->nfetch_branches++;
2563 if (iremote.nsend_branches > 0) {
2564 remote->send_branches = recallocarray(NULL, 0,
2565 iremote.nsend_branches, sizeof(char *));
2566 if (remote->send_branches == NULL) {
2567 err = got_error_from_errno("calloc");
2568 free_remote_data(remote);
2569 break;
2572 remote->nsend_branches = 0;
2573 for (i = 0; i < iremote.nsend_branches; i++) {
2574 char *branch;
2575 err = got_privsep_recv_gotconfig_str(&branch,
2576 ibuf);
2577 if (err) {
2578 free_remote_data(remote);
2579 goto done;
2581 remote->send_branches[i] = branch;
2582 remote->nsend_branches++;
2584 if (iremote.nfetch_refs > 0) {
2585 remote->fetch_refs = recallocarray(NULL, 0,
2586 iremote.nfetch_refs, sizeof(char *));
2587 if (remote->fetch_refs == NULL) {
2588 err = got_error_from_errno("calloc");
2589 free_remote_data(remote);
2590 break;
2593 remote->nfetch_refs = 0;
2594 for (i = 0; i < iremote.nfetch_refs; i++) {
2595 char *ref;
2596 err = got_privsep_recv_gotconfig_str(&ref,
2597 ibuf);
2598 if (err) {
2599 free_remote_data(remote);
2600 goto done;
2602 remote->fetch_refs[i] = ref;
2603 remote->nfetch_refs++;
2605 (*nremotes)++;
2606 break;
2607 default:
2608 err = got_error(GOT_ERR_PRIVSEP_MSG);
2609 break;
2612 imsg_free(&imsg);
2613 if (err)
2614 break;
2616 done:
2617 if (err) {
2618 int i;
2619 for (i = 0; i < *nremotes; i++)
2620 free_remote_data(&(*remotes)[i]);
2621 free(*remotes);
2622 *remotes = NULL;
2623 *nremotes = 0;
2625 return err;
2628 const struct got_error *
2629 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2630 struct got_object_id *id, int idx, const char *path)
2632 struct ibuf *wbuf;
2633 size_t path_len = strlen(path) + 1;
2635 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2636 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2637 if (wbuf == NULL)
2638 return got_error_from_errno(
2639 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2640 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2641 return got_error_from_errno("imsg_add "
2642 "COMMIT_TRAVERSAL_REQUEST");
2643 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2644 return got_error_from_errno("imsg_add "
2645 "COMMIT_TRAVERSAL_REQUEST");
2646 if (imsg_add(wbuf, path, path_len) == -1)
2647 return got_error_from_errno("imsg_add "
2648 "COMMIT_TRAVERSAL_REQUEST");
2650 wbuf->fd = -1;
2651 imsg_close(ibuf, wbuf);
2653 return flush_imsg(ibuf);
2656 const struct got_error *
2657 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2658 struct got_object_id **changed_commit_id,
2659 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2661 const struct got_error *err = NULL;
2662 struct imsg imsg;
2663 struct got_imsg_traversed_commits *icommits;
2664 size_t datalen;
2665 int i, done = 0;
2667 *changed_commit = NULL;
2668 *changed_commit_id = NULL;
2670 while (!done) {
2671 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2672 if (err)
2673 return err;
2675 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2676 switch (imsg.hdr.type) {
2677 case GOT_IMSG_TRAVERSED_COMMITS:
2678 icommits = imsg.data;
2679 if (datalen != sizeof(*icommits) +
2680 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2681 err = got_error(GOT_ERR_PRIVSEP_LEN);
2682 break;
2684 for (i = 0; i < icommits->ncommits; i++) {
2685 struct got_object_qid *qid;
2686 uint8_t *sha1 = (uint8_t *)imsg.data +
2687 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2688 err = got_object_qid_alloc_partial(&qid);
2689 if (err)
2690 break;
2691 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2692 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2694 /* The last commit may contain a change. */
2695 if (i == icommits->ncommits - 1) {
2696 *changed_commit_id =
2697 got_object_id_dup(&qid->id);
2698 if (*changed_commit_id == NULL) {
2699 err = got_error_from_errno(
2700 "got_object_id_dup");
2701 break;
2705 break;
2706 case GOT_IMSG_COMMIT:
2707 if (*changed_commit_id == NULL) {
2708 err = got_error(GOT_ERR_PRIVSEP_MSG);
2709 break;
2711 err = get_commit_from_imsg(changed_commit, &imsg,
2712 datalen, ibuf);
2713 break;
2714 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2715 done = 1;
2716 break;
2717 default:
2718 err = got_error(GOT_ERR_PRIVSEP_MSG);
2719 break;
2722 imsg_free(&imsg);
2723 if (err)
2724 break;
2727 if (err)
2728 got_object_id_queue_free(commit_ids);
2729 return err;
2732 const struct got_error *
2733 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2734 struct got_object_id *id)
2736 struct got_imsg_raw_delta_request dreq;
2738 dreq.idx = idx;
2739 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2741 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2742 &dreq, sizeof(dreq)) == -1)
2743 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2745 return flush_imsg(ibuf);
2748 const struct got_error *
2749 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2751 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2754 const struct got_error *
2755 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2756 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2757 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2759 struct got_imsg_raw_delta idelta;
2760 int ret;
2762 idelta.base_size = base_size;
2763 idelta.result_size = result_size;
2764 idelta.delta_size = delta_size;
2765 idelta.delta_compressed_size = delta_compressed_size;
2766 idelta.delta_offset = delta_offset;
2767 idelta.delta_out_offset = delta_out_offset;
2768 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2770 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2771 &idelta, sizeof(idelta));
2772 if (ret == -1)
2773 return got_error_from_errno("imsg_compose RAW_DELTA");
2775 return flush_imsg(ibuf);
2778 const struct got_error *
2779 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2780 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2781 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
2783 const struct got_error *err = NULL;
2784 struct imsg imsg;
2785 struct got_imsg_raw_delta *delta;
2786 size_t datalen;
2788 *base_size = 0;
2789 *result_size = 0;
2790 *delta_size = 0;
2791 *delta_compressed_size = 0;
2792 *delta_offset = 0;
2793 *delta_out_offset = 0;
2794 *base_id = NULL;
2796 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2797 if (err)
2798 return err;
2800 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2802 switch (imsg.hdr.type) {
2803 case GOT_IMSG_RAW_DELTA:
2804 if (datalen != sizeof(*delta)) {
2805 err = got_error(GOT_ERR_PRIVSEP_LEN);
2806 break;
2808 delta = imsg.data;
2809 *base_size = delta->base_size;
2810 *result_size = delta->result_size;
2811 *delta_size = delta->delta_size;
2812 *delta_compressed_size = delta->delta_compressed_size;
2813 *delta_offset = delta->delta_offset;
2814 *delta_out_offset = delta->delta_out_offset;
2815 *base_id = calloc(1, sizeof(**base_id));
2816 if (*base_id == NULL) {
2817 err = got_error_from_errno("malloc");
2818 break;
2820 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
2821 break;
2822 default:
2823 err = got_error(GOT_ERR_PRIVSEP_MSG);
2824 break;
2827 imsg_free(&imsg);
2829 if (err) {
2830 free(*base_id);
2831 *base_id = NULL;
2833 return err;
2836 const struct got_error *
2837 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
2838 struct got_object_id **ids, size_t nids)
2840 const struct got_error *err = NULL;
2841 struct got_imsg_object_idlist idlist;
2842 struct ibuf *wbuf;
2843 size_t i;
2845 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
2846 return got_error(GOT_ERR_NO_SPACE);
2848 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
2849 sizeof(idlist) + nids * sizeof(**ids));
2850 if (wbuf == NULL) {
2851 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
2852 return err;
2855 idlist.nids = nids;
2856 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
2857 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2859 for (i = 0; i < nids; i++) {
2860 struct got_object_id *id = ids[i];
2861 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
2862 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2865 wbuf->fd = -1;
2866 imsg_close(ibuf, wbuf);
2868 return flush_imsg(ibuf);
2871 const struct got_error *
2872 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
2874 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
2875 == -1)
2876 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
2878 return flush_imsg(ibuf);
2881 const struct got_error *
2882 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
2883 size_t *nids, struct imsgbuf *ibuf)
2885 const struct got_error *err = NULL;
2886 struct imsg imsg;
2887 struct got_imsg_object_idlist *idlist;
2888 size_t datalen;
2890 *ids = NULL;
2891 *done = 0;
2892 *nids = 0;
2894 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2895 if (err)
2896 return err;
2898 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2899 switch (imsg.hdr.type) {
2900 case GOT_IMSG_OBJ_ID_LIST:
2901 if (datalen < sizeof(*idlist)) {
2902 err = got_error(GOT_ERR_PRIVSEP_LEN);
2903 break;
2905 idlist = imsg.data;
2906 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
2907 err = got_error(GOT_ERR_PRIVSEP_LEN);
2908 break;
2910 *nids = idlist->nids;
2911 *ids = calloc(*nids, sizeof(**ids));
2912 if (*ids == NULL) {
2913 err = got_error_from_errno("calloc");
2914 break;
2916 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
2917 *nids * sizeof(**ids));
2918 break;
2919 case GOT_IMSG_OBJ_ID_LIST_DONE:
2920 *done = 1;
2921 break;
2922 default:
2923 err = got_error(GOT_ERR_PRIVSEP_MSG);
2924 break;
2927 imsg_free(&imsg);
2929 return err;
2932 const struct got_error *
2933 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
2935 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
2936 == -1)
2937 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
2939 return flush_imsg(ibuf);
2942 const struct got_error *
2943 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
2944 struct got_imsg_reused_delta *deltas, size_t ndeltas)
2946 const struct got_error *err = NULL;
2947 struct ibuf *wbuf;
2948 struct got_imsg_reused_deltas ideltas;
2949 size_t i;
2951 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
2952 return got_error(GOT_ERR_NO_SPACE);
2954 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
2955 sizeof(ideltas) + ndeltas * sizeof(*deltas));
2956 if (wbuf == NULL) {
2957 err = got_error_from_errno("imsg_create REUSED_DELTAS");
2958 return err;
2961 ideltas.ndeltas = ndeltas;
2962 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
2963 return got_error_from_errno("imsg_add REUSED_DELTAS");
2965 for (i = 0; i < ndeltas; i++) {
2966 struct got_imsg_reused_delta *delta = &deltas[i];
2967 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
2968 return got_error_from_errno("imsg_add REUSED_DELTAS");
2971 wbuf->fd = -1;
2972 imsg_close(ibuf, wbuf);
2974 return flush_imsg(ibuf);
2977 const struct got_error *
2978 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
2980 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
2981 == -1)
2982 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
2984 return flush_imsg(ibuf);
2987 const struct got_error *
2988 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
2989 size_t *ndeltas, struct imsgbuf *ibuf)
2991 const struct got_error *err = NULL;
2992 struct imsg imsg;
2993 struct got_imsg_reused_deltas *ideltas;
2994 size_t datalen;
2996 *done = 0;
2997 *ndeltas = 0;
2999 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3000 if (err)
3001 return err;
3003 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3004 switch (imsg.hdr.type) {
3005 case GOT_IMSG_REUSED_DELTAS:
3006 if (datalen < sizeof(*ideltas)) {
3007 err = got_error(GOT_ERR_PRIVSEP_LEN);
3008 break;
3010 ideltas = imsg.data;
3011 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3012 err = got_error(GOT_ERR_PRIVSEP_LEN);
3013 break;
3015 *ndeltas = ideltas->ndeltas;
3016 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3017 *ndeltas * sizeof(*deltas));
3018 break;
3019 case GOT_IMSG_DELTA_REUSE_DONE:
3020 *done = 1;
3021 break;
3022 default:
3023 err = got_error(GOT_ERR_PRIVSEP_MSG);
3024 break;
3027 imsg_free(&imsg);
3029 return err;
3032 const struct got_error *
3033 got_privsep_unveil_exec_helpers(void)
3035 const char *helpers[] = {
3036 GOT_PATH_PROG_READ_PACK,
3037 GOT_PATH_PROG_READ_OBJECT,
3038 GOT_PATH_PROG_READ_COMMIT,
3039 GOT_PATH_PROG_READ_TREE,
3040 GOT_PATH_PROG_READ_BLOB,
3041 GOT_PATH_PROG_READ_TAG,
3042 GOT_PATH_PROG_READ_GITCONFIG,
3043 GOT_PATH_PROG_READ_GOTCONFIG,
3044 GOT_PATH_PROG_READ_PATCH,
3045 GOT_PATH_PROG_FETCH_PACK,
3046 GOT_PATH_PROG_INDEX_PACK,
3047 GOT_PATH_PROG_SEND_PACK,
3049 size_t i;
3051 for (i = 0; i < nitems(helpers); i++) {
3052 if (unveil(helpers[i], "x") == 0)
3053 continue;
3054 return got_error_from_errno2("unveil", helpers[i]);
3057 return NULL;
3060 void
3061 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3063 if (close(imsg_fds[0]) == -1) {
3064 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3065 _exit(1);
3068 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3069 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3070 _exit(1);
3073 closefrom(GOT_IMSG_FD_CHILD + 1);
3075 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3076 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3077 strerror(errno));
3078 _exit(1);