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 if (datalen < sizeof(struct got_imsg_error)) {
706 err = got_error(GOT_ERR_PRIVSEP_LEN);
707 break;
709 err = recv_imsg_error(&imsg, datalen);
710 break;
711 case GOT_IMSG_FETCH_SYMREFS:
712 if (datalen < sizeof(*isymrefs)) {
713 err = got_error(GOT_ERR_PRIVSEP_LEN);
714 break;
716 if (isymrefs != NULL) {
717 err = got_error(GOT_ERR_PRIVSEP_MSG);
718 break;
720 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
721 off = sizeof(*isymrefs);
722 remain = datalen - off;
723 for (n = 0; n < isymrefs->nsymrefs; n++) {
724 struct got_imsg_fetch_symref *s;
725 char *name, *target;
726 if (remain < sizeof(struct got_imsg_fetch_symref)) {
727 err = got_error(GOT_ERR_PRIVSEP_LEN);
728 goto done;
730 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
731 off += sizeof(*s);
732 remain -= sizeof(*s);
733 if (remain < s->name_len) {
734 err = got_error(GOT_ERR_PRIVSEP_LEN);
735 goto done;
737 name = strndup(imsg.data + off, s->name_len);
738 if (name == NULL) {
739 err = got_error_from_errno("strndup");
740 goto done;
742 off += s->name_len;
743 remain -= s->name_len;
744 if (remain < s->target_len) {
745 err = got_error(GOT_ERR_PRIVSEP_LEN);
746 free(name);
747 goto done;
749 target = strndup(imsg.data + off, s->target_len);
750 if (target == NULL) {
751 err = got_error_from_errno("strndup");
752 free(name);
753 goto done;
755 off += s->target_len;
756 remain -= s->target_len;
757 err = got_pathlist_append(symrefs, name, target);
758 if (err) {
759 free(name);
760 free(target);
761 goto done;
764 break;
765 case GOT_IMSG_FETCH_REF:
766 if (datalen <= SHA1_DIGEST_LENGTH) {
767 err = got_error(GOT_ERR_PRIVSEP_MSG);
768 break;
770 *id = malloc(sizeof(**id));
771 if (*id == NULL) {
772 err = got_error_from_errno("malloc");
773 break;
775 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
776 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
777 datalen - SHA1_DIGEST_LENGTH);
778 if (*refname == NULL) {
779 err = got_error_from_errno("strndup");
780 break;
782 break;
783 case GOT_IMSG_FETCH_SERVER_PROGRESS:
784 if (datalen == 0) {
785 err = got_error(GOT_ERR_PRIVSEP_LEN);
786 break;
788 *server_progress = strndup(imsg.data, datalen);
789 if (*server_progress == NULL) {
790 err = got_error_from_errno("strndup");
791 break;
793 for (i = 0; i < datalen; i++) {
794 if (!isprint((unsigned char)(*server_progress)[i]) &&
795 !isspace((unsigned char)(*server_progress)[i])) {
796 err = got_error(GOT_ERR_PRIVSEP_MSG);
797 free(*server_progress);
798 *server_progress = NULL;
799 goto done;
802 break;
803 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
804 if (datalen < sizeof(*packfile_size)) {
805 err = got_error(GOT_ERR_PRIVSEP_MSG);
806 break;
808 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
809 break;
810 case GOT_IMSG_FETCH_DONE:
811 if (datalen != SHA1_DIGEST_LENGTH) {
812 err = got_error(GOT_ERR_PRIVSEP_MSG);
813 break;
815 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
816 *done = 1;
817 break;
818 default:
819 err = got_error(GOT_ERR_PRIVSEP_MSG);
820 break;
822 done:
823 if (err) {
824 free(*id);
825 *id = NULL;
826 free(*refname);
827 *refname = NULL;
829 imsg_free(&imsg);
830 return err;
833 static const struct got_error *
834 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
835 int delete, struct imsgbuf *ibuf)
837 size_t len;
838 struct ibuf *wbuf;
840 len = sizeof(struct got_imsg_send_ref) + name_len;
841 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
842 if (wbuf == NULL)
843 return got_error_from_errno("imsg_create SEND_REF");
845 /* Keep in sync with struct got_imsg_send_ref! */
846 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
847 return got_error_from_errno("imsg_add SEND_REF");
848 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
849 return got_error_from_errno("imsg_add SEND_REF");
850 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
851 return got_error_from_errno("imsg_add SEND_REF");
852 if (imsg_add(wbuf, name, name_len) == -1)
853 return got_error_from_errno("imsg_add SEND_REF");
855 wbuf->fd = -1;
856 imsg_close(ibuf, wbuf);
857 return flush_imsg(ibuf);
860 const struct got_error *
861 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
862 struct got_pathlist_head *have_refs,
863 struct got_pathlist_head *delete_refs,
864 int verbosity)
866 const struct got_error *err = NULL;
867 struct got_pathlist_entry *pe;
868 struct got_imsg_send_request sendreq;
869 struct got_object_id zero_id;
871 memset(&zero_id, 0, sizeof(zero_id));
872 memset(&sendreq, 0, sizeof(sendreq));
873 sendreq.verbosity = verbosity;
874 TAILQ_FOREACH(pe, have_refs, entry)
875 sendreq.nrefs++;
876 TAILQ_FOREACH(pe, delete_refs, entry)
877 sendreq.nrefs++;
878 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
879 &sendreq, sizeof(sendreq)) == -1) {
880 err = got_error_from_errno(
881 "imsg_compose FETCH_SERVER_PROGRESS");
882 goto done;
885 err = flush_imsg(ibuf);
886 if (err)
887 goto done;
888 fd = -1;
890 TAILQ_FOREACH(pe, have_refs, entry) {
891 const char *name = pe->path;
892 size_t name_len = pe->path_len;
893 struct got_object_id *id = pe->data;
894 err = send_send_ref(name, name_len, id, 0, ibuf);
895 if (err)
896 goto done;
899 TAILQ_FOREACH(pe, delete_refs, entry) {
900 const char *name = pe->path;
901 size_t name_len = pe->path_len;
902 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
903 if (err)
904 goto done;
906 done:
907 if (fd != -1 && close(fd) == -1 && err == NULL)
908 err = got_error_from_errno("close");
909 return err;
913 const struct got_error *
914 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
915 struct imsgbuf *ibuf)
917 const struct got_error *err = NULL;
918 struct imsg imsg;
919 size_t datalen;
920 int done = 0;
921 struct got_imsg_send_remote_ref iremote_ref;
922 struct got_object_id *id = NULL;
923 char *refname = NULL;
924 struct got_pathlist_entry *new;
926 while (!done) {
927 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
928 if (err)
929 return err;
930 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
931 switch (imsg.hdr.type) {
932 case GOT_IMSG_ERROR:
933 if (datalen < sizeof(struct got_imsg_error)) {
934 err = got_error(GOT_ERR_PRIVSEP_LEN);
935 goto done;
937 err = recv_imsg_error(&imsg, datalen);
938 goto done;
939 case GOT_IMSG_SEND_REMOTE_REF:
940 if (datalen < sizeof(iremote_ref)) {
941 err = got_error(GOT_ERR_PRIVSEP_MSG);
942 goto done;
944 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
945 if (datalen != sizeof(iremote_ref) +
946 iremote_ref.name_len) {
947 err = got_error(GOT_ERR_PRIVSEP_MSG);
948 goto done;
950 id = malloc(sizeof(*id));
951 if (id == NULL) {
952 err = got_error_from_errno("malloc");
953 goto done;
955 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
956 refname = strndup(imsg.data + sizeof(iremote_ref),
957 datalen - sizeof(iremote_ref));
958 if (refname == NULL) {
959 err = got_error_from_errno("strndup");
960 goto done;
962 err = got_pathlist_insert(&new, remote_refs,
963 refname, id);
964 if (err)
965 goto done;
966 if (new == NULL) { /* duplicate which wasn't inserted */
967 free(id);
968 free(refname);
970 id = NULL;
971 refname = NULL;
972 break;
973 case GOT_IMSG_SEND_PACK_REQUEST:
974 if (datalen != 0) {
975 err = got_error(GOT_ERR_PRIVSEP_MSG);
976 goto done;
978 /* got-send-pack is now waiting for a pack file. */
979 done = 1;
980 break;
981 default:
982 err = got_error(GOT_ERR_PRIVSEP_MSG);
983 break;
986 done:
987 free(id);
988 free(refname);
989 imsg_free(&imsg);
990 return err;
993 const struct got_error *
994 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
996 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
999 const struct got_error *
1000 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1001 int *success, char **refname, struct imsgbuf *ibuf)
1003 const struct got_error *err = NULL;
1004 struct imsg imsg;
1005 size_t datalen;
1006 struct got_imsg_send_ref_status iref_status;
1008 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1009 *done = 0;
1010 *success = 0;
1011 *refname = NULL;
1013 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1014 if (err)
1015 return err;
1017 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1018 switch (imsg.hdr.type) {
1019 case GOT_IMSG_ERROR:
1020 if (datalen < sizeof(struct got_imsg_error)) {
1021 err = got_error(GOT_ERR_PRIVSEP_LEN);
1022 break;
1024 err = recv_imsg_error(&imsg, datalen);
1025 break;
1026 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1027 if (datalen < sizeof(*bytes_sent)) {
1028 err = got_error(GOT_ERR_PRIVSEP_MSG);
1029 break;
1031 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1032 break;
1033 case GOT_IMSG_SEND_REF_STATUS:
1034 if (datalen < sizeof(iref_status)) {
1035 err = got_error(GOT_ERR_PRIVSEP_MSG);
1036 break;
1038 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1039 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1040 err = got_error(GOT_ERR_PRIVSEP_MSG);
1041 break;
1043 *success = iref_status.success;
1044 *refname = strndup(imsg.data + sizeof(iref_status),
1045 iref_status.name_len);
1046 break;
1047 case GOT_IMSG_SEND_DONE:
1048 if (datalen != 0) {
1049 err = got_error(GOT_ERR_PRIVSEP_MSG);
1050 break;
1052 *done = 1;
1053 break;
1054 default:
1055 err = got_error(GOT_ERR_PRIVSEP_MSG);
1056 break;
1059 imsg_free(&imsg);
1060 return err;
1063 const struct got_error *
1064 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1065 int fd)
1067 const struct got_error *err = NULL;
1069 /* Keep in sync with struct got_imsg_index_pack_request */
1070 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1071 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1072 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1073 close(fd);
1074 return err;
1076 return flush_imsg(ibuf);
1079 const struct got_error *
1080 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1082 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1085 const struct got_error *
1086 got_privsep_recv_index_progress(int *done, int *nobj_total,
1087 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1088 struct imsgbuf *ibuf)
1090 const struct got_error *err = NULL;
1091 struct imsg imsg;
1092 struct got_imsg_index_pack_progress *iprogress;
1093 size_t datalen;
1095 *done = 0;
1096 *nobj_total = 0;
1097 *nobj_indexed = 0;
1098 *nobj_resolved = 0;
1100 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1101 if (err)
1102 return err;
1104 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1105 switch (imsg.hdr.type) {
1106 case GOT_IMSG_ERROR:
1107 if (datalen < sizeof(struct got_imsg_error)) {
1108 err = got_error(GOT_ERR_PRIVSEP_LEN);
1109 break;
1111 err = recv_imsg_error(&imsg, datalen);
1112 break;
1113 case GOT_IMSG_IDXPACK_PROGRESS:
1114 if (datalen < sizeof(*iprogress)) {
1115 err = got_error(GOT_ERR_PRIVSEP_LEN);
1116 break;
1118 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1119 *nobj_total = iprogress->nobj_total;
1120 *nobj_indexed = iprogress->nobj_indexed;
1121 *nobj_loose = iprogress->nobj_loose;
1122 *nobj_resolved = iprogress->nobj_resolved;
1123 break;
1124 case GOT_IMSG_IDXPACK_DONE:
1125 if (datalen != 0) {
1126 err = got_error(GOT_ERR_PRIVSEP_LEN);
1127 break;
1129 *done = 1;
1130 break;
1131 default:
1132 err = got_error(GOT_ERR_PRIVSEP_MSG);
1133 break;
1136 imsg_free(&imsg);
1137 return err;
1140 const struct got_error *
1141 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1142 struct imsgbuf *ibuf)
1144 struct got_imsg_object *iobj;
1145 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1147 if (datalen != sizeof(*iobj))
1148 return got_error(GOT_ERR_PRIVSEP_LEN);
1149 iobj = imsg->data;
1151 *obj = calloc(1, sizeof(**obj));
1152 if (*obj == NULL)
1153 return got_error_from_errno("calloc");
1155 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1156 (*obj)->type = iobj->type;
1157 (*obj)->flags = iobj->flags;
1158 (*obj)->hdrlen = iobj->hdrlen;
1159 (*obj)->size = iobj->size;
1160 /* path_packfile is handled by caller */
1161 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1162 (*obj)->pack_offset = iobj->pack_offset;
1163 (*obj)->pack_idx = iobj->pack_idx;
1165 STAILQ_INIT(&(*obj)->deltas.entries);
1166 return NULL;
1169 const struct got_error *
1170 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1172 const struct got_error *err = NULL;
1173 struct imsg imsg;
1174 const size_t min_datalen =
1175 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1177 *obj = NULL;
1179 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1180 if (err)
1181 return err;
1183 switch (imsg.hdr.type) {
1184 case GOT_IMSG_OBJECT:
1185 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1186 break;
1187 default:
1188 err = got_error(GOT_ERR_PRIVSEP_MSG);
1189 break;
1192 imsg_free(&imsg);
1194 return err;
1197 static const struct got_error *
1198 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1199 size_t logmsg_len)
1201 const struct got_error *err = NULL;
1202 size_t offset, remain;
1204 offset = 0;
1205 remain = logmsg_len;
1206 while (remain > 0) {
1207 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1209 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1210 commit->logmsg + offset, n) == -1) {
1211 err = got_error_from_errno("imsg_compose "
1212 "COMMIT_LOGMSG");
1213 break;
1216 err = flush_imsg(ibuf);
1217 if (err)
1218 break;
1220 offset += n;
1221 remain -= n;
1224 return err;
1227 const struct got_error *
1228 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1230 const struct got_error *err = NULL;
1231 struct got_imsg_commit_object *icommit;
1232 uint8_t *buf;
1233 size_t len, total;
1234 struct got_object_qid *qid;
1235 size_t author_len = strlen(commit->author);
1236 size_t committer_len = strlen(commit->committer);
1237 size_t logmsg_len = strlen(commit->logmsg);
1239 total = sizeof(*icommit) + author_len + committer_len +
1240 commit->nparents * SHA1_DIGEST_LENGTH;
1242 buf = malloc(total);
1243 if (buf == NULL)
1244 return got_error_from_errno("malloc");
1246 icommit = (struct got_imsg_commit_object *)buf;
1247 memcpy(icommit->tree_id, commit->tree_id->sha1,
1248 sizeof(icommit->tree_id));
1249 icommit->author_len = author_len;
1250 icommit->author_time = commit->author_time;
1251 icommit->author_gmtoff = commit->author_gmtoff;
1252 icommit->committer_len = committer_len;
1253 icommit->committer_time = commit->committer_time;
1254 icommit->committer_gmtoff = commit->committer_gmtoff;
1255 icommit->logmsg_len = logmsg_len;
1256 icommit->nparents = commit->nparents;
1258 len = sizeof(*icommit);
1259 memcpy(buf + len, commit->author, author_len);
1260 len += author_len;
1261 memcpy(buf + len, commit->committer, committer_len);
1262 len += committer_len;
1263 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1264 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1265 len += SHA1_DIGEST_LENGTH;
1268 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1269 err = got_error_from_errno("imsg_compose COMMIT");
1270 goto done;
1273 if (logmsg_len == 0 ||
1274 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1275 err = flush_imsg(ibuf);
1276 if (err)
1277 goto done;
1279 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1280 done:
1281 free(buf);
1282 return err;
1285 static const struct got_error *
1286 get_commit_from_imsg(struct got_commit_object **commit,
1287 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1289 const struct got_error *err = NULL;
1290 struct got_imsg_commit_object *icommit;
1291 size_t len = 0;
1292 int i;
1294 if (datalen < sizeof(*icommit))
1295 return got_error(GOT_ERR_PRIVSEP_LEN);
1297 icommit = imsg->data;
1298 if (datalen != sizeof(*icommit) + icommit->author_len +
1299 icommit->committer_len +
1300 icommit->nparents * SHA1_DIGEST_LENGTH)
1301 return got_error(GOT_ERR_PRIVSEP_LEN);
1303 if (icommit->nparents < 0)
1304 return got_error(GOT_ERR_PRIVSEP_LEN);
1306 len += sizeof(*icommit);
1308 *commit = got_object_commit_alloc_partial();
1309 if (*commit == NULL)
1310 return got_error_from_errno(
1311 "got_object_commit_alloc_partial");
1313 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1314 SHA1_DIGEST_LENGTH);
1315 (*commit)->author_time = icommit->author_time;
1316 (*commit)->author_gmtoff = icommit->author_gmtoff;
1317 (*commit)->committer_time = icommit->committer_time;
1318 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1320 if (icommit->author_len == 0) {
1321 (*commit)->author = strdup("");
1322 if ((*commit)->author == NULL) {
1323 err = got_error_from_errno("strdup");
1324 goto done;
1326 } else {
1327 (*commit)->author = malloc(icommit->author_len + 1);
1328 if ((*commit)->author == NULL) {
1329 err = got_error_from_errno("malloc");
1330 goto done;
1332 memcpy((*commit)->author, imsg->data + len,
1333 icommit->author_len);
1334 (*commit)->author[icommit->author_len] = '\0';
1336 len += icommit->author_len;
1338 if (icommit->committer_len == 0) {
1339 (*commit)->committer = strdup("");
1340 if ((*commit)->committer == NULL) {
1341 err = got_error_from_errno("strdup");
1342 goto done;
1344 } else {
1345 (*commit)->committer =
1346 malloc(icommit->committer_len + 1);
1347 if ((*commit)->committer == NULL) {
1348 err = got_error_from_errno("malloc");
1349 goto done;
1351 memcpy((*commit)->committer, imsg->data + len,
1352 icommit->committer_len);
1353 (*commit)->committer[icommit->committer_len] = '\0';
1355 len += icommit->committer_len;
1357 if (icommit->logmsg_len == 0) {
1358 (*commit)->logmsg = strdup("");
1359 if ((*commit)->logmsg == NULL) {
1360 err = got_error_from_errno("strdup");
1361 goto done;
1363 } else {
1364 size_t offset = 0, remain = icommit->logmsg_len;
1366 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1367 if ((*commit)->logmsg == NULL) {
1368 err = got_error_from_errno("malloc");
1369 goto done;
1371 while (remain > 0) {
1372 struct imsg imsg_log;
1373 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1374 remain);
1376 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1377 if (err)
1378 goto done;
1380 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1381 err = got_error(GOT_ERR_PRIVSEP_MSG);
1382 goto done;
1385 memcpy((*commit)->logmsg + offset,
1386 imsg_log.data, n);
1387 imsg_free(&imsg_log);
1388 offset += n;
1389 remain -= n;
1391 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1394 for (i = 0; i < icommit->nparents; i++) {
1395 struct got_object_qid *qid;
1397 err = got_object_qid_alloc_partial(&qid);
1398 if (err)
1399 break;
1400 memcpy(&qid->id, imsg->data + len +
1401 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1402 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1403 (*commit)->nparents++;
1405 done:
1406 if (err) {
1407 got_object_commit_close(*commit);
1408 *commit = NULL;
1410 return err;
1413 const struct got_error *
1414 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1416 const struct got_error *err = NULL;
1417 struct imsg imsg;
1418 size_t datalen;
1419 const size_t min_datalen =
1420 MIN(sizeof(struct got_imsg_error),
1421 sizeof(struct got_imsg_commit_object));
1423 *commit = NULL;
1425 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1426 if (err)
1427 return err;
1429 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1431 switch (imsg.hdr.type) {
1432 case GOT_IMSG_COMMIT:
1433 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1434 break;
1435 default:
1436 err = got_error(GOT_ERR_PRIVSEP_MSG);
1437 break;
1440 imsg_free(&imsg);
1442 return err;
1445 static const struct got_error *
1446 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1447 int idx0, int idxN, size_t len)
1449 struct ibuf *wbuf;
1450 struct got_imsg_tree_entries ientries;
1451 int i;
1453 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1454 if (wbuf == NULL)
1455 return got_error_from_errno("imsg_create TREE_ENTRY");
1457 ientries.nentries = idxN - idx0 + 1;
1458 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1459 return got_error_from_errno("imsg_add TREE_ENTRY");
1461 for (i = idx0; i <= idxN; i++) {
1462 struct got_parsed_tree_entry *pte = &entries[i];
1464 /* Keep in sync with struct got_imsg_tree_object definition! */
1465 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1466 return got_error_from_errno("imsg_add TREE_ENTRY");
1467 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1468 return got_error_from_errno("imsg_add TREE_ENTRY");
1469 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1470 return got_error_from_errno("imsg_add TREE_ENTRY");
1472 /* Remaining bytes are the entry's name. */
1473 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1474 return got_error_from_errno("imsg_add TREE_ENTRY");
1477 wbuf->fd = -1;
1478 imsg_close(ibuf, wbuf);
1479 return NULL;
1482 const struct got_error *
1483 got_privsep_send_tree(struct imsgbuf *ibuf,
1484 struct got_parsed_tree_entry *entries, int nentries)
1486 const struct got_error *err = NULL;
1487 struct got_imsg_tree_object itree;
1488 size_t entries_len;
1489 int i, j;
1491 itree.nentries = nentries;
1492 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1493 == -1)
1494 return got_error_from_errno("imsg_compose TREE");
1496 entries_len = sizeof(struct got_imsg_tree_entries);
1497 i = 0;
1498 for (j = 0; j < nentries; j++) {
1499 struct got_parsed_tree_entry *pte = &entries[j];
1500 size_t len = sizeof(*pte) + pte->namelen;
1502 if (j > 0 &&
1503 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1504 err = send_tree_entries(ibuf, entries,
1505 i, j - 1, entries_len);
1506 if (err)
1507 return err;
1508 i = j;
1509 entries_len = sizeof(struct got_imsg_tree_entries);
1512 entries_len += len;
1515 if (j > 0) {
1516 err = send_tree_entries(ibuf, entries, i, j - 1, entries_len);
1517 if (err)
1518 return err;
1521 return flush_imsg(ibuf);
1524 const struct got_error *
1525 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1527 const struct got_error *err = NULL;
1528 const size_t min_datalen =
1529 MIN(sizeof(struct got_imsg_error),
1530 sizeof(struct got_imsg_tree_object));
1531 struct got_imsg_tree_object *itree;
1532 size_t i;
1533 int nentries = 0;
1535 *tree = NULL;
1537 err = read_imsg(ibuf);
1538 if (err)
1539 goto done;
1541 for (;;) {
1542 struct imsg imsg;
1543 size_t n;
1544 size_t datalen;
1545 struct got_imsg_tree_entries *ientries;
1546 struct got_tree_entry *te = NULL;
1547 size_t te_offset;
1549 n = imsg_get(ibuf, &imsg);
1550 if (n == 0) {
1551 if ((*tree)) {
1552 if (nentries < (*tree)->nentries) {
1553 err = read_imsg(ibuf);
1554 if (err)
1555 break;
1556 continue;
1557 } else
1558 break;
1559 } else {
1560 err = got_error(GOT_ERR_PRIVSEP_MSG);
1561 break;
1565 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1566 imsg_free(&imsg);
1567 err = got_error(GOT_ERR_PRIVSEP_LEN);
1568 break;
1571 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1573 switch (imsg.hdr.type) {
1574 case GOT_IMSG_ERROR:
1575 err = recv_imsg_error(&imsg, datalen);
1576 break;
1577 case GOT_IMSG_TREE:
1578 /* This message should only appear once. */
1579 if (*tree != NULL) {
1580 err = got_error(GOT_ERR_PRIVSEP_MSG);
1581 break;
1583 if (datalen != sizeof(*itree)) {
1584 err = got_error(GOT_ERR_PRIVSEP_LEN);
1585 break;
1587 itree = imsg.data;
1588 if (itree->nentries < 0) {
1589 err = got_error(GOT_ERR_PRIVSEP_LEN);
1590 break;
1592 *tree = malloc(sizeof(**tree));
1593 if (*tree == NULL) {
1594 err = got_error_from_errno("malloc");
1595 break;
1597 (*tree)->entries = calloc(itree->nentries,
1598 sizeof(struct got_tree_entry));
1599 if ((*tree)->entries == NULL) {
1600 err = got_error_from_errno("malloc");
1601 free(*tree);
1602 *tree = NULL;
1603 break;
1605 (*tree)->nentries = itree->nentries;
1606 (*tree)->refcnt = 0;
1607 break;
1608 case GOT_IMSG_TREE_ENTRIES:
1609 /* This message should be preceeded by GOT_IMSG_TREE. */
1610 if (*tree == NULL) {
1611 err = got_error(GOT_ERR_PRIVSEP_MSG);
1612 break;
1614 if (datalen <= sizeof(*ientries) ||
1615 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1616 err = got_error(GOT_ERR_PRIVSEP_LEN);
1617 break;
1620 ientries = imsg.data;
1621 if (ientries->nentries > INT_MAX) {
1622 err = got_error_msg(GOT_ERR_NO_SPACE,
1623 "too many tree entries");
1624 break;
1626 te_offset = sizeof(*ientries);
1627 for (i = 0; i < ientries->nentries; i++) {
1628 struct got_imsg_tree_entry ite;
1629 const char *te_name;
1630 uint8_t *buf = imsg.data + te_offset;
1632 if (te_offset >= datalen) {
1633 err = got_error(GOT_ERR_PRIVSEP_LEN);
1634 break;
1637 /* Might not be aligned, size is ~32 bytes. */
1638 memcpy(&ite, buf, sizeof(ite));
1640 if (ite.namelen >= sizeof(te->name)) {
1641 err = got_error(GOT_ERR_PRIVSEP_LEN);
1642 break;
1644 if (te_offset + sizeof(ite) + ite.namelen >
1645 datalen) {
1646 err = got_error(GOT_ERR_PRIVSEP_LEN);
1647 break;
1649 if (nentries >= (*tree)->nentries) {
1650 err = got_error(GOT_ERR_PRIVSEP_LEN);
1651 break;
1653 te = &(*tree)->entries[nentries];
1654 te_name = buf + sizeof(ite);
1655 memcpy(te->name, te_name, ite.namelen);
1656 te->name[ite.namelen] = '\0';
1657 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1658 te->mode = ite.mode;
1659 te->idx = nentries;
1660 nentries++;
1662 te_offset += sizeof(ite) + ite.namelen;
1664 break;
1665 default:
1666 err = got_error(GOT_ERR_PRIVSEP_MSG);
1667 break;
1670 imsg_free(&imsg);
1671 if (err)
1672 break;
1674 done:
1675 if (*tree && (*tree)->nentries != nentries) {
1676 if (err == NULL)
1677 err = got_error(GOT_ERR_PRIVSEP_LEN);
1678 got_object_tree_close(*tree);
1679 *tree = NULL;
1682 return err;
1685 const struct got_error *
1686 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1687 const uint8_t *data)
1689 struct got_imsg_blob iblob;
1691 iblob.size = size;
1692 iblob.hdrlen = hdrlen;
1694 if (data) {
1695 uint8_t *buf;
1697 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1698 return got_error(GOT_ERR_NO_SPACE);
1700 buf = malloc(sizeof(iblob) + size);
1701 if (buf == NULL)
1702 return got_error_from_errno("malloc");
1704 memcpy(buf, &iblob, sizeof(iblob));
1705 memcpy(buf + sizeof(iblob), data, size);
1706 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1707 sizeof(iblob) + size) == -1) {
1708 free(buf);
1709 return got_error_from_errno("imsg_compose BLOB");
1711 free(buf);
1712 } else {
1713 /* Data has already been written to file descriptor. */
1714 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1715 sizeof(iblob)) == -1)
1716 return got_error_from_errno("imsg_compose BLOB");
1720 return flush_imsg(ibuf);
1723 const struct got_error *
1724 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1725 struct imsgbuf *ibuf)
1727 const struct got_error *err = NULL;
1728 struct imsg imsg;
1729 struct got_imsg_blob *iblob;
1730 size_t datalen;
1732 *outbuf = NULL;
1734 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1735 if (err)
1736 return err;
1738 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1740 switch (imsg.hdr.type) {
1741 case GOT_IMSG_BLOB:
1742 if (datalen < sizeof(*iblob)) {
1743 err = got_error(GOT_ERR_PRIVSEP_LEN);
1744 break;
1746 iblob = imsg.data;
1747 *size = iblob->size;
1748 *hdrlen = iblob->hdrlen;
1750 if (datalen == sizeof(*iblob)) {
1751 /* Data has been written to file descriptor. */
1752 break;
1755 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1756 err = got_error(GOT_ERR_PRIVSEP_LEN);
1757 break;
1760 *outbuf = malloc(*size);
1761 if (*outbuf == NULL) {
1762 err = got_error_from_errno("malloc");
1763 break;
1765 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1766 break;
1767 default:
1768 err = got_error(GOT_ERR_PRIVSEP_MSG);
1769 break;
1772 imsg_free(&imsg);
1774 return err;
1777 static const struct got_error *
1778 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1780 const struct got_error *err = NULL;
1781 size_t offset, remain;
1783 offset = 0;
1784 remain = tagmsg_len;
1785 while (remain > 0) {
1786 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1788 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1789 tag->tagmsg + offset, n) == -1) {
1790 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1791 break;
1794 err = flush_imsg(ibuf);
1795 if (err)
1796 break;
1798 offset += n;
1799 remain -= n;
1802 return err;
1805 const struct got_error *
1806 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1808 const struct got_error *err = NULL;
1809 struct got_imsg_tag_object *itag;
1810 uint8_t *buf;
1811 size_t len, total;
1812 size_t tag_len = strlen(tag->tag);
1813 size_t tagger_len = strlen(tag->tagger);
1814 size_t tagmsg_len = strlen(tag->tagmsg);
1816 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1818 buf = malloc(total);
1819 if (buf == NULL)
1820 return got_error_from_errno("malloc");
1822 itag = (struct got_imsg_tag_object *)buf;
1823 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1824 itag->obj_type = tag->obj_type;
1825 itag->tag_len = tag_len;
1826 itag->tagger_len = tagger_len;
1827 itag->tagger_time = tag->tagger_time;
1828 itag->tagger_gmtoff = tag->tagger_gmtoff;
1829 itag->tagmsg_len = tagmsg_len;
1831 len = sizeof(*itag);
1832 memcpy(buf + len, tag->tag, tag_len);
1833 len += tag_len;
1834 memcpy(buf + len, tag->tagger, tagger_len);
1835 len += tagger_len;
1837 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1838 err = got_error_from_errno("imsg_compose TAG");
1839 goto done;
1842 if (tagmsg_len == 0 ||
1843 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1844 err = flush_imsg(ibuf);
1845 if (err)
1846 goto done;
1848 err = send_tagmsg(ibuf, tag, tagmsg_len);
1849 done:
1850 free(buf);
1851 return err;
1854 const struct got_error *
1855 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1857 const struct got_error *err = NULL;
1858 struct imsg imsg;
1859 struct got_imsg_tag_object *itag;
1860 size_t len, datalen;
1861 const size_t min_datalen =
1862 MIN(sizeof(struct got_imsg_error),
1863 sizeof(struct got_imsg_tag_object));
1865 *tag = NULL;
1867 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1868 if (err)
1869 return err;
1871 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1872 len = 0;
1874 switch (imsg.hdr.type) {
1875 case GOT_IMSG_TAG:
1876 if (datalen < sizeof(*itag)) {
1877 err = got_error(GOT_ERR_PRIVSEP_LEN);
1878 break;
1880 itag = imsg.data;
1881 if (datalen != sizeof(*itag) + itag->tag_len +
1882 itag->tagger_len) {
1883 err = got_error(GOT_ERR_PRIVSEP_LEN);
1884 break;
1886 len += sizeof(*itag);
1888 *tag = calloc(1, sizeof(**tag));
1889 if (*tag == NULL) {
1890 err = got_error_from_errno("calloc");
1891 break;
1894 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1896 if (itag->tag_len == 0) {
1897 (*tag)->tag = strdup("");
1898 if ((*tag)->tag == NULL) {
1899 err = got_error_from_errno("strdup");
1900 break;
1902 } else {
1903 (*tag)->tag = malloc(itag->tag_len + 1);
1904 if ((*tag)->tag == NULL) {
1905 err = got_error_from_errno("malloc");
1906 break;
1908 memcpy((*tag)->tag, imsg.data + len,
1909 itag->tag_len);
1910 (*tag)->tag[itag->tag_len] = '\0';
1912 len += itag->tag_len;
1914 (*tag)->obj_type = itag->obj_type;
1915 (*tag)->tagger_time = itag->tagger_time;
1916 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1918 if (itag->tagger_len == 0) {
1919 (*tag)->tagger = strdup("");
1920 if ((*tag)->tagger == NULL) {
1921 err = got_error_from_errno("strdup");
1922 break;
1924 } else {
1925 (*tag)->tagger = malloc(itag->tagger_len + 1);
1926 if ((*tag)->tagger == NULL) {
1927 err = got_error_from_errno("malloc");
1928 break;
1930 memcpy((*tag)->tagger, imsg.data + len,
1931 itag->tagger_len);
1932 (*tag)->tagger[itag->tagger_len] = '\0';
1934 len += itag->tagger_len;
1936 if (itag->tagmsg_len == 0) {
1937 (*tag)->tagmsg = strdup("");
1938 if ((*tag)->tagmsg == NULL) {
1939 err = got_error_from_errno("strdup");
1940 break;
1942 } else {
1943 size_t offset = 0, remain = itag->tagmsg_len;
1945 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1946 if ((*tag)->tagmsg == NULL) {
1947 err = got_error_from_errno("malloc");
1948 break;
1950 while (remain > 0) {
1951 struct imsg imsg_log;
1952 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1953 remain);
1955 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1956 if (err)
1957 return err;
1959 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1960 return got_error(GOT_ERR_PRIVSEP_MSG);
1962 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1963 n);
1964 imsg_free(&imsg_log);
1965 offset += n;
1966 remain -= n;
1968 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1971 break;
1972 default:
1973 err = got_error(GOT_ERR_PRIVSEP_MSG);
1974 break;
1977 imsg_free(&imsg);
1979 return err;
1982 const struct got_error *
1983 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1984 struct got_packidx *packidx)
1986 const struct got_error *err = NULL;
1987 struct got_imsg_packidx ipackidx;
1988 struct got_imsg_pack ipack;
1989 int fd;
1991 ipackidx.len = packidx->len;
1992 ipackidx.packfile_size = pack->filesize;
1993 fd = dup(packidx->fd);
1994 if (fd == -1)
1995 return got_error_from_errno("dup");
1997 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1998 sizeof(ipackidx)) == -1) {
1999 err = got_error_from_errno("imsg_compose PACKIDX");
2000 close(fd);
2001 return err;
2004 if (strlcpy(ipack.path_packfile, pack->path_packfile,
2005 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
2006 return got_error(GOT_ERR_NO_SPACE);
2007 ipack.filesize = pack->filesize;
2009 fd = dup(pack->fd);
2010 if (fd == -1)
2011 return got_error_from_errno("dup");
2013 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2014 == -1) {
2015 err = got_error_from_errno("imsg_compose PACK");
2016 close(fd);
2017 return err;
2020 return flush_imsg(ibuf);
2023 const struct got_error *
2024 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2025 struct got_object_id *id)
2027 struct got_imsg_packed_object iobj;
2029 iobj.idx = idx;
2030 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2032 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2033 &iobj, sizeof(iobj)) == -1)
2034 return got_error_from_errno("imsg_compose "
2035 "PACKED_OBJECT_REQUEST");
2037 return flush_imsg(ibuf);
2040 const struct got_error *
2041 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2042 struct got_object_id *id)
2044 struct got_imsg_packed_object iobj;
2046 iobj.idx = idx;
2047 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2049 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2050 &iobj, sizeof(iobj)) == -1)
2051 return got_error_from_errno("imsg_compose "
2052 "PACKED_OBJECT_REQUEST");
2054 return flush_imsg(ibuf);
2057 const struct got_error *
2058 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2060 const struct got_error *err = NULL;
2062 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2063 NULL, 0) == -1) {
2064 err = got_error_from_errno("imsg_compose "
2065 "GITCONFIG_PARSE_REQUEST");
2066 close(fd);
2067 return err;
2070 return flush_imsg(ibuf);
2073 const struct got_error *
2074 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2076 if (imsg_compose(ibuf,
2077 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2078 NULL, 0) == -1)
2079 return got_error_from_errno("imsg_compose "
2080 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2082 return flush_imsg(ibuf);
2085 const struct got_error *
2086 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2088 if (imsg_compose(ibuf,
2089 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2090 NULL, 0) == -1)
2091 return got_error_from_errno("imsg_compose "
2092 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2094 return flush_imsg(ibuf);
2098 const struct got_error *
2099 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2101 if (imsg_compose(ibuf,
2102 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2103 return got_error_from_errno("imsg_compose "
2104 "GITCONFIG_AUTHOR_NAME_REQUEST");
2106 return flush_imsg(ibuf);
2109 const struct got_error *
2110 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2112 if (imsg_compose(ibuf,
2113 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2114 return got_error_from_errno("imsg_compose "
2115 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2117 return flush_imsg(ibuf);
2120 const struct got_error *
2121 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2123 if (imsg_compose(ibuf,
2124 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2125 return got_error_from_errno("imsg_compose "
2126 "GITCONFIG_REMOTE_REQUEST");
2128 return flush_imsg(ibuf);
2131 const struct got_error *
2132 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2134 if (imsg_compose(ibuf,
2135 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2136 return got_error_from_errno("imsg_compose "
2137 "GITCONFIG_OWNER_REQUEST");
2139 return flush_imsg(ibuf);
2142 const struct got_error *
2143 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2145 const struct got_error *err = NULL;
2146 struct imsg imsg;
2147 size_t datalen;
2148 const size_t min_datalen = 0;
2150 *str = NULL;
2152 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2153 if (err)
2154 return err;
2155 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2157 switch (imsg.hdr.type) {
2158 case GOT_IMSG_GITCONFIG_STR_VAL:
2159 if (datalen == 0)
2160 break;
2161 /* datalen does not include terminating \0 */
2162 *str = malloc(datalen + 1);
2163 if (*str == NULL) {
2164 err = got_error_from_errno("malloc");
2165 break;
2167 memcpy(*str, imsg.data, datalen);
2168 (*str)[datalen] = '\0';
2169 break;
2170 default:
2171 err = got_error(GOT_ERR_PRIVSEP_MSG);
2172 break;
2175 imsg_free(&imsg);
2176 return err;
2179 const struct got_error *
2180 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2182 const struct got_error *err = NULL;
2183 struct imsg imsg;
2184 size_t datalen;
2185 const size_t min_datalen =
2186 MIN(sizeof(struct got_imsg_error), sizeof(int));
2188 *val = 0;
2190 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2191 if (err)
2192 return err;
2193 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2195 switch (imsg.hdr.type) {
2196 case GOT_IMSG_GITCONFIG_INT_VAL:
2197 if (datalen != sizeof(*val)) {
2198 err = got_error(GOT_ERR_PRIVSEP_LEN);
2199 break;
2201 memcpy(val, imsg.data, sizeof(*val));
2202 break;
2203 default:
2204 err = got_error(GOT_ERR_PRIVSEP_MSG);
2205 break;
2208 imsg_free(&imsg);
2209 return err;
2212 static void
2213 free_remote_data(struct got_remote_repo *remote)
2215 int i;
2217 free(remote->name);
2218 free(remote->fetch_url);
2219 free(remote->send_url);
2220 for (i = 0; i < remote->nfetch_branches; i++)
2221 free(remote->fetch_branches[i]);
2222 free(remote->fetch_branches);
2223 for (i = 0; i < remote->nsend_branches; i++)
2224 free(remote->send_branches[i]);
2225 free(remote->send_branches);
2226 for (i = 0; i < remote->nfetch_refs; i++)
2227 free(remote->fetch_refs[i]);
2228 free(remote->fetch_refs);
2231 const struct got_error *
2232 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2233 int *nremotes, struct imsgbuf *ibuf)
2235 const struct got_error *err = NULL;
2236 struct imsg imsg;
2237 size_t datalen;
2238 struct got_imsg_remotes iremotes;
2239 struct got_imsg_remote iremote;
2241 *remotes = NULL;
2242 *nremotes = 0;
2243 iremotes.nremotes = 0;
2245 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2246 if (err)
2247 return err;
2248 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2250 switch (imsg.hdr.type) {
2251 case GOT_IMSG_GITCONFIG_REMOTES:
2252 if (datalen != sizeof(iremotes)) {
2253 err = got_error(GOT_ERR_PRIVSEP_LEN);
2254 break;
2256 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2257 if (iremotes.nremotes == 0) {
2258 imsg_free(&imsg);
2259 return NULL;
2261 break;
2262 default:
2263 imsg_free(&imsg);
2264 return got_error(GOT_ERR_PRIVSEP_MSG);
2267 imsg_free(&imsg);
2269 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2270 if (*remotes == NULL)
2271 return got_error_from_errno("recallocarray");
2273 while (*nremotes < iremotes.nremotes) {
2274 struct got_remote_repo *remote;
2276 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2277 if (err)
2278 break;
2279 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2281 switch (imsg.hdr.type) {
2282 case GOT_IMSG_GITCONFIG_REMOTE:
2283 remote = &(*remotes)[*nremotes];
2284 memset(remote, 0, sizeof(*remote));
2285 if (datalen < sizeof(iremote)) {
2286 err = got_error(GOT_ERR_PRIVSEP_LEN);
2287 break;
2289 memcpy(&iremote, imsg.data, sizeof(iremote));
2290 if (iremote.name_len == 0 ||
2291 iremote.fetch_url_len == 0 ||
2292 iremote.send_url_len == 0 ||
2293 (sizeof(iremote) + iremote.name_len +
2294 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2295 err = got_error(GOT_ERR_PRIVSEP_LEN);
2296 break;
2298 remote->name = strndup(imsg.data + sizeof(iremote),
2299 iremote.name_len);
2300 if (remote->name == NULL) {
2301 err = got_error_from_errno("strndup");
2302 break;
2304 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2305 iremote.name_len, iremote.fetch_url_len);
2306 if (remote->fetch_url == NULL) {
2307 err = got_error_from_errno("strndup");
2308 free_remote_data(remote);
2309 break;
2311 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2312 iremote.name_len + iremote.fetch_url_len,
2313 iremote.send_url_len);
2314 if (remote->send_url == NULL) {
2315 err = got_error_from_errno("strndup");
2316 free_remote_data(remote);
2317 break;
2319 remote->mirror_references = iremote.mirror_references;
2320 remote->fetch_all_branches = iremote.fetch_all_branches;
2321 remote->nfetch_branches = 0;
2322 remote->fetch_branches = NULL;
2323 remote->nsend_branches = 0;
2324 remote->send_branches = NULL;
2325 remote->nfetch_refs = 0;
2326 remote->fetch_refs = NULL;
2327 (*nremotes)++;
2328 break;
2329 default:
2330 err = got_error(GOT_ERR_PRIVSEP_MSG);
2331 break;
2334 imsg_free(&imsg);
2335 if (err)
2336 break;
2339 if (err) {
2340 int i;
2341 for (i = 0; i < *nremotes; i++)
2342 free_remote_data(&(*remotes)[i]);
2343 free(*remotes);
2344 *remotes = NULL;
2345 *nremotes = 0;
2347 return err;
2350 const struct got_error *
2351 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2353 const struct got_error *err = NULL;
2355 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2356 NULL, 0) == -1) {
2357 err = got_error_from_errno("imsg_compose "
2358 "GOTCONFIG_PARSE_REQUEST");
2359 close(fd);
2360 return err;
2363 return flush_imsg(ibuf);
2366 const struct got_error *
2367 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2369 if (imsg_compose(ibuf,
2370 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2371 return got_error_from_errno("imsg_compose "
2372 "GOTCONFIG_AUTHOR_REQUEST");
2374 return flush_imsg(ibuf);
2377 const struct got_error *
2378 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2380 if (imsg_compose(ibuf,
2381 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2382 return got_error_from_errno("imsg_compose "
2383 "GOTCONFIG_REMOTE_REQUEST");
2385 return flush_imsg(ibuf);
2388 const struct got_error *
2389 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2391 const struct got_error *err = NULL;
2392 struct imsg imsg;
2393 size_t datalen;
2394 const size_t min_datalen = 0;
2396 *str = NULL;
2398 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2399 if (err)
2400 return err;
2401 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2403 switch (imsg.hdr.type) {
2404 case GOT_IMSG_ERROR:
2405 if (datalen < sizeof(struct got_imsg_error)) {
2406 err = got_error(GOT_ERR_PRIVSEP_LEN);
2407 break;
2409 err = recv_imsg_error(&imsg, datalen);
2410 break;
2411 case GOT_IMSG_GOTCONFIG_STR_VAL:
2412 if (datalen == 0)
2413 break;
2414 /* datalen does not include terminating \0 */
2415 *str = malloc(datalen + 1);
2416 if (*str == NULL) {
2417 err = got_error_from_errno("malloc");
2418 break;
2420 memcpy(*str, imsg.data, datalen);
2421 (*str)[datalen] = '\0';
2422 break;
2423 default:
2424 err = got_error(GOT_ERR_PRIVSEP_MSG);
2425 break;
2428 imsg_free(&imsg);
2429 return err;
2432 const struct got_error *
2433 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2434 int *nremotes, struct imsgbuf *ibuf)
2436 const struct got_error *err = NULL;
2437 struct imsg imsg;
2438 size_t datalen;
2439 struct got_imsg_remotes iremotes;
2440 struct got_imsg_remote iremote;
2441 const size_t min_datalen =
2442 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2444 *remotes = NULL;
2445 *nremotes = 0;
2446 iremotes.nremotes = 0;
2448 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2449 if (err)
2450 return err;
2451 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2453 switch (imsg.hdr.type) {
2454 case GOT_IMSG_ERROR:
2455 if (datalen < sizeof(struct got_imsg_error)) {
2456 err = got_error(GOT_ERR_PRIVSEP_LEN);
2457 break;
2459 err = recv_imsg_error(&imsg, datalen);
2460 break;
2461 case GOT_IMSG_GOTCONFIG_REMOTES:
2462 if (datalen != sizeof(iremotes)) {
2463 err = got_error(GOT_ERR_PRIVSEP_LEN);
2464 break;
2466 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2467 if (iremotes.nremotes == 0) {
2468 imsg_free(&imsg);
2469 return NULL;
2471 break;
2472 default:
2473 imsg_free(&imsg);
2474 return got_error(GOT_ERR_PRIVSEP_MSG);
2477 imsg_free(&imsg);
2479 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2480 if (*remotes == NULL)
2481 return got_error_from_errno("recallocarray");
2483 while (*nremotes < iremotes.nremotes) {
2484 struct got_remote_repo *remote;
2485 const size_t min_datalen =
2486 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2487 int i;
2489 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2490 if (err)
2491 break;
2492 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2494 switch (imsg.hdr.type) {
2495 case GOT_IMSG_ERROR:
2496 if (datalen < sizeof(struct got_imsg_error)) {
2497 err = got_error(GOT_ERR_PRIVSEP_LEN);
2498 break;
2500 err = recv_imsg_error(&imsg, datalen);
2501 break;
2502 case GOT_IMSG_GOTCONFIG_REMOTE:
2503 remote = &(*remotes)[*nremotes];
2504 memset(remote, 0, sizeof(*remote));
2505 if (datalen < sizeof(iremote)) {
2506 err = got_error(GOT_ERR_PRIVSEP_LEN);
2507 break;
2509 memcpy(&iremote, imsg.data, sizeof(iremote));
2510 if (iremote.name_len == 0 ||
2511 (iremote.fetch_url_len == 0 &&
2512 iremote.send_url_len == 0) ||
2513 (sizeof(iremote) + iremote.name_len +
2514 iremote.fetch_url_len + iremote.send_url_len) >
2515 datalen) {
2516 err = got_error(GOT_ERR_PRIVSEP_LEN);
2517 break;
2519 remote->name = strndup(imsg.data + sizeof(iremote),
2520 iremote.name_len);
2521 if (remote->name == NULL) {
2522 err = got_error_from_errno("strndup");
2523 break;
2525 remote->fetch_url = strndup(imsg.data +
2526 sizeof(iremote) + iremote.name_len,
2527 iremote.fetch_url_len);
2528 if (remote->fetch_url == NULL) {
2529 err = got_error_from_errno("strndup");
2530 free_remote_data(remote);
2531 break;
2533 remote->send_url = strndup(imsg.data +
2534 sizeof(iremote) + iremote.name_len +
2535 iremote.fetch_url_len, iremote.send_url_len);
2536 if (remote->send_url == NULL) {
2537 err = got_error_from_errno("strndup");
2538 free_remote_data(remote);
2539 break;
2541 remote->mirror_references = iremote.mirror_references;
2542 remote->fetch_all_branches = iremote.fetch_all_branches;
2543 if (iremote.nfetch_branches > 0) {
2544 remote->fetch_branches = recallocarray(NULL, 0,
2545 iremote.nfetch_branches, sizeof(char *));
2546 if (remote->fetch_branches == NULL) {
2547 err = got_error_from_errno("calloc");
2548 free_remote_data(remote);
2549 break;
2552 remote->nfetch_branches = 0;
2553 for (i = 0; i < iremote.nfetch_branches; i++) {
2554 char *branch;
2555 err = got_privsep_recv_gotconfig_str(&branch,
2556 ibuf);
2557 if (err) {
2558 free_remote_data(remote);
2559 goto done;
2561 remote->fetch_branches[i] = branch;
2562 remote->nfetch_branches++;
2564 if (iremote.nsend_branches > 0) {
2565 remote->send_branches = recallocarray(NULL, 0,
2566 iremote.nsend_branches, sizeof(char *));
2567 if (remote->send_branches == NULL) {
2568 err = got_error_from_errno("calloc");
2569 free_remote_data(remote);
2570 break;
2573 remote->nsend_branches = 0;
2574 for (i = 0; i < iremote.nsend_branches; i++) {
2575 char *branch;
2576 err = got_privsep_recv_gotconfig_str(&branch,
2577 ibuf);
2578 if (err) {
2579 free_remote_data(remote);
2580 goto done;
2582 remote->send_branches[i] = branch;
2583 remote->nsend_branches++;
2585 if (iremote.nfetch_refs > 0) {
2586 remote->fetch_refs = recallocarray(NULL, 0,
2587 iremote.nfetch_refs, sizeof(char *));
2588 if (remote->fetch_refs == NULL) {
2589 err = got_error_from_errno("calloc");
2590 free_remote_data(remote);
2591 break;
2594 remote->nfetch_refs = 0;
2595 for (i = 0; i < iremote.nfetch_refs; i++) {
2596 char *ref;
2597 err = got_privsep_recv_gotconfig_str(&ref,
2598 ibuf);
2599 if (err) {
2600 free_remote_data(remote);
2601 goto done;
2603 remote->fetch_refs[i] = ref;
2604 remote->nfetch_refs++;
2606 (*nremotes)++;
2607 break;
2608 default:
2609 err = got_error(GOT_ERR_PRIVSEP_MSG);
2610 break;
2613 imsg_free(&imsg);
2614 if (err)
2615 break;
2617 done:
2618 if (err) {
2619 int i;
2620 for (i = 0; i < *nremotes; i++)
2621 free_remote_data(&(*remotes)[i]);
2622 free(*remotes);
2623 *remotes = NULL;
2624 *nremotes = 0;
2626 return err;
2629 const struct got_error *
2630 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2631 struct got_object_id *id, int idx, const char *path)
2633 struct ibuf *wbuf;
2634 size_t path_len = strlen(path) + 1;
2636 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2637 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2638 if (wbuf == NULL)
2639 return got_error_from_errno(
2640 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2641 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2642 return got_error_from_errno("imsg_add "
2643 "COMMIT_TRAVERSAL_REQUEST");
2644 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2645 return got_error_from_errno("imsg_add "
2646 "COMMIT_TRAVERSAL_REQUEST");
2647 if (imsg_add(wbuf, path, path_len) == -1)
2648 return got_error_from_errno("imsg_add "
2649 "COMMIT_TRAVERSAL_REQUEST");
2651 wbuf->fd = -1;
2652 imsg_close(ibuf, wbuf);
2654 return flush_imsg(ibuf);
2657 const struct got_error *
2658 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2659 struct got_object_id **changed_commit_id,
2660 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2662 const struct got_error *err = NULL;
2663 struct imsg imsg;
2664 struct got_imsg_traversed_commits *icommits;
2665 size_t datalen;
2666 int i, done = 0;
2668 *changed_commit = NULL;
2669 *changed_commit_id = NULL;
2671 while (!done) {
2672 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2673 if (err)
2674 return err;
2676 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2677 switch (imsg.hdr.type) {
2678 case GOT_IMSG_TRAVERSED_COMMITS:
2679 icommits = imsg.data;
2680 if (datalen != sizeof(*icommits) +
2681 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2682 err = got_error(GOT_ERR_PRIVSEP_LEN);
2683 break;
2685 for (i = 0; i < icommits->ncommits; i++) {
2686 struct got_object_qid *qid;
2687 uint8_t *sha1 = (uint8_t *)imsg.data +
2688 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2689 err = got_object_qid_alloc_partial(&qid);
2690 if (err)
2691 break;
2692 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2693 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2695 /* The last commit may contain a change. */
2696 if (i == icommits->ncommits - 1) {
2697 *changed_commit_id =
2698 got_object_id_dup(&qid->id);
2699 if (*changed_commit_id == NULL) {
2700 err = got_error_from_errno(
2701 "got_object_id_dup");
2702 break;
2706 break;
2707 case GOT_IMSG_COMMIT:
2708 if (*changed_commit_id == NULL) {
2709 err = got_error(GOT_ERR_PRIVSEP_MSG);
2710 break;
2712 err = get_commit_from_imsg(changed_commit, &imsg,
2713 datalen, ibuf);
2714 break;
2715 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2716 done = 1;
2717 break;
2718 default:
2719 err = got_error(GOT_ERR_PRIVSEP_MSG);
2720 break;
2723 imsg_free(&imsg);
2724 if (err)
2725 break;
2728 if (err)
2729 got_object_id_queue_free(commit_ids);
2730 return err;
2733 const struct got_error *
2734 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2735 struct got_object_id *id)
2737 struct got_imsg_raw_delta_request dreq;
2739 dreq.idx = idx;
2740 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2742 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2743 &dreq, sizeof(dreq)) == -1)
2744 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2746 return flush_imsg(ibuf);
2749 const struct got_error *
2750 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2752 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2755 const struct got_error *
2756 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2757 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2758 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2760 struct got_imsg_raw_delta idelta;
2761 int ret;
2763 idelta.base_size = base_size;
2764 idelta.result_size = result_size;
2765 idelta.delta_size = delta_size;
2766 idelta.delta_compressed_size = delta_compressed_size;
2767 idelta.delta_offset = delta_offset;
2768 idelta.delta_out_offset = delta_out_offset;
2769 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2771 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2772 &idelta, sizeof(idelta));
2773 if (ret == -1)
2774 return got_error_from_errno("imsg_compose RAW_DELTA");
2776 return flush_imsg(ibuf);
2779 const struct got_error *
2780 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2781 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2782 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
2784 const struct got_error *err = NULL;
2785 struct imsg imsg;
2786 struct got_imsg_raw_delta *delta;
2787 size_t datalen;
2789 *base_size = 0;
2790 *result_size = 0;
2791 *delta_size = 0;
2792 *delta_compressed_size = 0;
2793 *delta_offset = 0;
2794 *delta_out_offset = 0;
2795 *base_id = NULL;
2797 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2798 if (err)
2799 return err;
2801 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2803 switch (imsg.hdr.type) {
2804 case GOT_IMSG_RAW_DELTA:
2805 if (datalen != sizeof(*delta)) {
2806 err = got_error(GOT_ERR_PRIVSEP_LEN);
2807 break;
2809 delta = imsg.data;
2810 *base_size = delta->base_size;
2811 *result_size = delta->result_size;
2812 *delta_size = delta->delta_size;
2813 *delta_compressed_size = delta->delta_compressed_size;
2814 *delta_offset = delta->delta_offset;
2815 *delta_out_offset = delta->delta_out_offset;
2816 *base_id = calloc(1, sizeof(**base_id));
2817 if (*base_id == NULL) {
2818 err = got_error_from_errno("malloc");
2819 break;
2821 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
2822 break;
2823 default:
2824 err = got_error(GOT_ERR_PRIVSEP_MSG);
2825 break;
2828 imsg_free(&imsg);
2830 if (err) {
2831 free(*base_id);
2832 *base_id = NULL;
2834 return err;
2837 const struct got_error *
2838 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
2839 struct got_object_id **ids, size_t nids)
2841 const struct got_error *err = NULL;
2842 struct got_imsg_object_idlist idlist;
2843 struct ibuf *wbuf;
2844 size_t i;
2846 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
2847 return got_error(GOT_ERR_NO_SPACE);
2849 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
2850 sizeof(idlist) + nids * sizeof(**ids));
2851 if (wbuf == NULL) {
2852 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
2853 return err;
2856 idlist.nids = nids;
2857 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
2858 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2860 for (i = 0; i < nids; i++) {
2861 struct got_object_id *id = ids[i];
2862 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
2863 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2866 wbuf->fd = -1;
2867 imsg_close(ibuf, wbuf);
2869 return flush_imsg(ibuf);
2872 const struct got_error *
2873 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
2875 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
2876 == -1)
2877 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
2879 return flush_imsg(ibuf);
2882 const struct got_error *
2883 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
2884 size_t *nids, struct imsgbuf *ibuf)
2886 const struct got_error *err = NULL;
2887 struct imsg imsg;
2888 struct got_imsg_object_idlist *idlist;
2889 size_t datalen;
2891 *ids = NULL;
2892 *done = 0;
2893 *nids = 0;
2895 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2896 if (err)
2897 return err;
2899 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2900 switch (imsg.hdr.type) {
2901 case GOT_IMSG_OBJ_ID_LIST:
2902 if (datalen < sizeof(*idlist)) {
2903 err = got_error(GOT_ERR_PRIVSEP_LEN);
2904 break;
2906 idlist = imsg.data;
2907 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
2908 err = got_error(GOT_ERR_PRIVSEP_LEN);
2909 break;
2911 *nids = idlist->nids;
2912 *ids = calloc(*nids, sizeof(**ids));
2913 if (*ids == NULL) {
2914 err = got_error_from_errno("calloc");
2915 break;
2917 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
2918 *nids * sizeof(**ids));
2919 break;
2920 case GOT_IMSG_OBJ_ID_LIST_DONE:
2921 *done = 1;
2922 break;
2923 default:
2924 err = got_error(GOT_ERR_PRIVSEP_MSG);
2925 break;
2928 imsg_free(&imsg);
2930 return err;
2933 const struct got_error *
2934 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
2936 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
2937 == -1)
2938 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
2940 return flush_imsg(ibuf);
2943 const struct got_error *
2944 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
2945 struct got_imsg_reused_delta *deltas, size_t ndeltas)
2947 const struct got_error *err = NULL;
2948 struct ibuf *wbuf;
2949 struct got_imsg_reused_deltas ideltas;
2950 size_t i;
2952 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
2953 return got_error(GOT_ERR_NO_SPACE);
2955 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
2956 sizeof(ideltas) + ndeltas * sizeof(*deltas));
2957 if (wbuf == NULL) {
2958 err = got_error_from_errno("imsg_create REUSED_DELTAS");
2959 return err;
2962 ideltas.ndeltas = ndeltas;
2963 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
2964 return got_error_from_errno("imsg_add REUSED_DELTAS");
2966 for (i = 0; i < ndeltas; i++) {
2967 struct got_imsg_reused_delta *delta = &deltas[i];
2968 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
2969 return got_error_from_errno("imsg_add REUSED_DELTAS");
2972 wbuf->fd = -1;
2973 imsg_close(ibuf, wbuf);
2975 return flush_imsg(ibuf);
2978 const struct got_error *
2979 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
2981 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
2982 == -1)
2983 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
2985 return flush_imsg(ibuf);
2988 const struct got_error *
2989 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
2990 size_t *ndeltas, struct imsgbuf *ibuf)
2992 const struct got_error *err = NULL;
2993 struct imsg imsg;
2994 struct got_imsg_reused_deltas *ideltas;
2995 size_t datalen;
2997 *done = 0;
2998 *ndeltas = 0;
3000 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3001 if (err)
3002 return err;
3004 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3005 switch (imsg.hdr.type) {
3006 case GOT_IMSG_REUSED_DELTAS:
3007 if (datalen < sizeof(*ideltas)) {
3008 err = got_error(GOT_ERR_PRIVSEP_LEN);
3009 break;
3011 ideltas = imsg.data;
3012 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3013 err = got_error(GOT_ERR_PRIVSEP_LEN);
3014 break;
3016 *ndeltas = ideltas->ndeltas;
3017 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3018 *ndeltas * sizeof(*deltas));
3019 break;
3020 case GOT_IMSG_DELTA_REUSE_DONE:
3021 *done = 1;
3022 break;
3023 default:
3024 err = got_error(GOT_ERR_PRIVSEP_MSG);
3025 break;
3028 imsg_free(&imsg);
3030 return err;
3033 const struct got_error *
3034 got_privsep_unveil_exec_helpers(void)
3036 const char *helpers[] = {
3037 GOT_PATH_PROG_READ_PACK,
3038 GOT_PATH_PROG_READ_OBJECT,
3039 GOT_PATH_PROG_READ_COMMIT,
3040 GOT_PATH_PROG_READ_TREE,
3041 GOT_PATH_PROG_READ_BLOB,
3042 GOT_PATH_PROG_READ_TAG,
3043 GOT_PATH_PROG_READ_GITCONFIG,
3044 GOT_PATH_PROG_READ_GOTCONFIG,
3045 GOT_PATH_PROG_READ_PATCH,
3046 GOT_PATH_PROG_FETCH_PACK,
3047 GOT_PATH_PROG_INDEX_PACK,
3048 GOT_PATH_PROG_SEND_PACK,
3050 size_t i;
3052 for (i = 0; i < nitems(helpers); i++) {
3053 if (unveil(helpers[i], "x") == 0)
3054 continue;
3055 return got_error_from_errno2("unveil", helpers[i]);
3058 return NULL;
3061 void
3062 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3064 if (close(imsg_fds[0]) == -1) {
3065 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3066 _exit(1);
3069 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3070 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3071 _exit(1);
3074 closefrom(GOT_IMSG_FD_CHILD + 1);
3076 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3077 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3078 strerror(errno));
3079 _exit(1);