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 return;
214 static const struct got_error *
215 flush_imsg(struct imsgbuf *ibuf)
217 const struct got_error *err;
219 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
220 if (err)
221 return err;
223 if (imsg_flush(ibuf) == -1)
224 return got_error_from_errno("imsg_flush");
226 return NULL;
229 const struct got_error *
230 got_privsep_flush_imsg(struct imsgbuf *ibuf)
232 return flush_imsg(ibuf);
235 const struct got_error *
236 got_privsep_send_stop(int fd)
238 const struct got_error *err = NULL;
239 struct imsgbuf ibuf;
241 imsg_init(&ibuf, fd);
243 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
244 return got_error_from_errno("imsg_compose STOP");
246 err = flush_imsg(&ibuf);
247 imsg_clear(&ibuf);
248 return err;
251 const struct got_error *
252 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
253 struct got_object_id *id)
255 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
256 id, sizeof(*id)) == -1)
257 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
259 return flush_imsg(ibuf);
262 const struct got_error *
263 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
264 struct got_object_id *id)
266 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
267 id, sizeof(*id)) == -1)
268 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
270 return flush_imsg(ibuf);
273 const struct got_error *
274 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
276 const struct got_error *err = NULL;
278 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
279 == -1) {
280 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
281 close(outfd);
282 return err;
285 return flush_imsg(ibuf);
288 const struct got_error *
289 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
290 uint8_t *data)
292 const struct got_error *err = NULL;
293 struct got_imsg_raw_obj iobj;
294 size_t len = sizeof(iobj);
295 struct ibuf *wbuf;
297 iobj.hdrlen = hdrlen;
298 iobj.size = size;
300 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
301 len += (size_t)size + hdrlen;
303 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
304 if (wbuf == NULL) {
305 err = got_error_from_errno("imsg_create RAW_OBJECT");
306 return err;
309 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1) {
310 err = got_error_from_errno("imsg_add RAW_OBJECT");
311 ibuf_free(wbuf);
312 return err;
315 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
316 if (imsg_add(wbuf, data, size + hdrlen) == -1) {
317 err = got_error_from_errno("imsg_add RAW_OBJECT");
318 ibuf_free(wbuf);
319 return err;
323 wbuf->fd = -1;
324 imsg_close(ibuf, wbuf);
326 return flush_imsg(ibuf);
329 const struct got_error *
330 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
331 struct imsgbuf *ibuf)
333 const struct got_error *err = NULL;
334 struct imsg imsg;
335 struct got_imsg_raw_obj *iobj;
336 size_t datalen;
338 *outbuf = NULL;
340 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
341 if (err)
342 return err;
344 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
346 switch (imsg.hdr.type) {
347 case GOT_IMSG_RAW_OBJECT:
348 if (datalen < sizeof(*iobj)) {
349 err = got_error(GOT_ERR_PRIVSEP_LEN);
350 break;
352 iobj = imsg.data;
353 *size = iobj->size;
354 *hdrlen = iobj->hdrlen;
356 if (datalen == sizeof(*iobj)) {
357 /* Data has been written to file descriptor. */
358 break;
361 if (*size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
362 err = got_error(GOT_ERR_PRIVSEP_LEN);
363 break;
366 *outbuf = malloc(*size + *hdrlen);
367 if (*outbuf == NULL) {
368 err = got_error_from_errno("malloc");
369 break;
371 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
372 break;
373 default:
374 err = got_error(GOT_ERR_PRIVSEP_MSG);
375 break;
378 imsg_free(&imsg);
380 return err;
383 const struct got_error *
384 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
385 struct got_object_id *id, int pack_idx)
387 const struct got_error *err = NULL;
388 struct got_imsg_packed_object iobj;
389 void *data;
390 size_t len;
392 if (pack_idx != -1) { /* commit is packed */
393 iobj.idx = pack_idx;
394 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
395 data = &iobj;
396 len = sizeof(iobj);
397 } else {
398 data = id;
399 len = sizeof(*id);
402 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
403 == -1) {
404 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
405 close(fd);
406 return err;
409 return flush_imsg(ibuf);
412 const struct got_error *
413 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
414 struct got_object_id *id, int pack_idx)
416 const struct got_error *err = NULL;
417 struct ibuf *wbuf;
418 size_t len;
420 if (pack_idx != -1)
421 len = sizeof(struct got_imsg_packed_object);
422 else
423 len = sizeof(*id);
425 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
426 if (wbuf == NULL)
427 return got_error_from_errno("imsg_create TREE_REQUEST");
429 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
430 err = got_error_from_errno("imsg_add TREE_REQUEST");
431 ibuf_free(wbuf);
432 return err;
435 if (pack_idx != -1) { /* tree is packed */
436 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
437 err = got_error_from_errno("imsg_add TREE_REQUEST");
438 ibuf_free(wbuf);
439 return err;
443 wbuf->fd = fd;
444 imsg_close(ibuf, wbuf);
446 return flush_imsg(ibuf);
449 const struct got_error *
450 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
451 struct got_object_id *id, int pack_idx)
453 struct got_imsg_packed_object iobj;
454 void *data;
455 size_t len;
457 if (pack_idx != -1) { /* tag is packed */
458 iobj.idx = pack_idx;
459 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
460 data = &iobj;
461 len = sizeof(iobj);
462 } else {
463 data = id;
464 len = sizeof(*id);
467 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
468 == -1)
469 return got_error_from_errno("imsg_compose TAG_REQUEST");
471 return flush_imsg(ibuf);
474 const struct got_error *
475 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
476 struct got_object_id *id, int pack_idx)
478 const struct got_error *err = NULL;
479 struct got_imsg_packed_object iobj;
480 void *data;
481 size_t len;
483 if (pack_idx != -1) { /* blob is packed */
484 iobj.idx = pack_idx;
485 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
486 data = &iobj;
487 len = sizeof(iobj);
488 } else {
489 data = id;
490 len = sizeof(*id);
493 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
494 == -1) {
495 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
496 close(infd);
497 return err;
500 return flush_imsg(ibuf);
503 const struct got_error *
504 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
506 const struct got_error *err = NULL;
508 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
509 == -1) {
510 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
511 close(outfd);
512 return err;
515 return flush_imsg(ibuf);
518 static const struct got_error *
519 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
521 const struct got_error *err = NULL;
523 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
524 err = got_error_from_errno("imsg_compose TMPFD");
525 close(fd);
526 return err;
529 return flush_imsg(ibuf);
532 const struct got_error *
533 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
535 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
538 const struct got_error *
539 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
541 struct got_imsg_object iobj;
543 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
544 iobj.type = obj->type;
545 iobj.flags = obj->flags;
546 iobj.hdrlen = obj->hdrlen;
547 iobj.size = obj->size;
548 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
549 iobj.pack_offset = obj->pack_offset;
550 iobj.pack_idx = obj->pack_idx;
553 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
554 == -1)
555 return got_error_from_errno("imsg_compose OBJECT");
557 return flush_imsg(ibuf);
560 const struct got_error *
561 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
562 struct got_pathlist_head *have_refs, int fetch_all_branches,
563 struct got_pathlist_head *wanted_branches,
564 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
566 const struct got_error *err = NULL;
567 struct ibuf *wbuf;
568 size_t len;
569 struct got_pathlist_entry *pe;
570 struct got_imsg_fetch_request fetchreq;
572 memset(&fetchreq, 0, sizeof(fetchreq));
573 fetchreq.fetch_all_branches = fetch_all_branches;
574 fetchreq.list_refs_only = list_refs_only;
575 fetchreq.verbosity = verbosity;
576 TAILQ_FOREACH(pe, have_refs, entry)
577 fetchreq.n_have_refs++;
578 TAILQ_FOREACH(pe, wanted_branches, entry)
579 fetchreq.n_wanted_branches++;
580 TAILQ_FOREACH(pe, wanted_refs, entry)
581 fetchreq.n_wanted_refs++;
582 len = sizeof(struct got_imsg_fetch_request);
583 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
584 close(fd);
585 return got_error(GOT_ERR_NO_SPACE);
588 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
589 &fetchreq, sizeof(fetchreq)) == -1)
590 return got_error_from_errno(
591 "imsg_compose FETCH_SERVER_PROGRESS");
593 err = flush_imsg(ibuf);
594 if (err) {
595 close(fd);
596 return err;
598 fd = -1;
600 TAILQ_FOREACH(pe, have_refs, entry) {
601 const char *name = pe->path;
602 size_t name_len = pe->path_len;
603 struct got_object_id *id = pe->data;
605 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
606 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
607 if (wbuf == NULL)
608 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
610 /* Keep in sync with struct got_imsg_fetch_have_ref! */
611 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
612 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
613 ibuf_free(wbuf);
614 return err;
616 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
617 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
618 ibuf_free(wbuf);
619 return err;
621 if (imsg_add(wbuf, name, name_len) == -1) {
622 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
623 ibuf_free(wbuf);
624 return err;
627 wbuf->fd = -1;
628 imsg_close(ibuf, wbuf);
629 err = flush_imsg(ibuf);
630 if (err)
631 return err;
634 TAILQ_FOREACH(pe, wanted_branches, entry) {
635 const char *name = pe->path;
636 size_t name_len = pe->path_len;
638 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
639 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
640 len);
641 if (wbuf == NULL)
642 return got_error_from_errno(
643 "imsg_create FETCH_WANTED_BRANCH");
645 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
646 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
647 err = got_error_from_errno(
648 "imsg_add FETCH_WANTED_BRANCH");
649 ibuf_free(wbuf);
650 return err;
652 if (imsg_add(wbuf, name, name_len) == -1) {
653 err = got_error_from_errno(
654 "imsg_add FETCH_WANTED_BRANCH");
655 ibuf_free(wbuf);
656 return err;
659 wbuf->fd = -1;
660 imsg_close(ibuf, wbuf);
661 err = flush_imsg(ibuf);
662 if (err)
663 return err;
666 TAILQ_FOREACH(pe, wanted_refs, entry) {
667 const char *name = pe->path;
668 size_t name_len = pe->path_len;
670 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
671 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
672 len);
673 if (wbuf == NULL)
674 return got_error_from_errno(
675 "imsg_create FETCH_WANTED_REF");
677 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
678 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
679 err = got_error_from_errno(
680 "imsg_add FETCH_WANTED_REF");
681 ibuf_free(wbuf);
682 return err;
684 if (imsg_add(wbuf, name, name_len) == -1) {
685 err = got_error_from_errno(
686 "imsg_add FETCH_WANTED_REF");
687 ibuf_free(wbuf);
688 return err;
691 wbuf->fd = -1;
692 imsg_close(ibuf, wbuf);
693 err = flush_imsg(ibuf);
694 if (err)
695 return err;
699 return NULL;
703 const struct got_error *
704 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
706 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
709 const struct got_error *
710 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
711 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
712 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
714 const struct got_error *err = NULL;
715 struct imsg imsg;
716 size_t datalen;
717 struct got_imsg_fetch_symrefs *isymrefs = NULL;
718 size_t n, remain;
719 off_t off;
720 int i;
722 *done = 0;
723 *id = NULL;
724 *refname = NULL;
725 *server_progress = NULL;
726 *packfile_size = 0;
727 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
729 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
730 if (err)
731 return err;
733 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
734 switch (imsg.hdr.type) {
735 case GOT_IMSG_ERROR:
736 if (datalen < sizeof(struct got_imsg_error)) {
737 err = got_error(GOT_ERR_PRIVSEP_LEN);
738 break;
740 err = recv_imsg_error(&imsg, datalen);
741 break;
742 case GOT_IMSG_FETCH_SYMREFS:
743 if (datalen < sizeof(*isymrefs)) {
744 err = got_error(GOT_ERR_PRIVSEP_LEN);
745 break;
747 if (isymrefs != NULL) {
748 err = got_error(GOT_ERR_PRIVSEP_MSG);
749 break;
751 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
752 off = sizeof(*isymrefs);
753 remain = datalen - off;
754 for (n = 0; n < isymrefs->nsymrefs; n++) {
755 struct got_imsg_fetch_symref *s;
756 char *name, *target;
757 if (remain < sizeof(struct got_imsg_fetch_symref)) {
758 err = got_error(GOT_ERR_PRIVSEP_LEN);
759 goto done;
761 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
762 off += sizeof(*s);
763 remain -= sizeof(*s);
764 if (remain < s->name_len) {
765 err = got_error(GOT_ERR_PRIVSEP_LEN);
766 goto done;
768 name = strndup(imsg.data + off, s->name_len);
769 if (name == NULL) {
770 err = got_error_from_errno("strndup");
771 goto done;
773 off += s->name_len;
774 remain -= s->name_len;
775 if (remain < s->target_len) {
776 err = got_error(GOT_ERR_PRIVSEP_LEN);
777 free(name);
778 goto done;
780 target = strndup(imsg.data + off, s->target_len);
781 if (target == NULL) {
782 err = got_error_from_errno("strndup");
783 free(name);
784 goto done;
786 off += s->target_len;
787 remain -= s->target_len;
788 err = got_pathlist_append(symrefs, name, target);
789 if (err) {
790 free(name);
791 free(target);
792 goto done;
795 break;
796 case GOT_IMSG_FETCH_REF:
797 if (datalen <= SHA1_DIGEST_LENGTH) {
798 err = got_error(GOT_ERR_PRIVSEP_MSG);
799 break;
801 *id = malloc(sizeof(**id));
802 if (*id == NULL) {
803 err = got_error_from_errno("malloc");
804 break;
806 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
807 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
808 datalen - SHA1_DIGEST_LENGTH);
809 if (*refname == NULL) {
810 err = got_error_from_errno("strndup");
811 break;
813 break;
814 case GOT_IMSG_FETCH_SERVER_PROGRESS:
815 if (datalen == 0) {
816 err = got_error(GOT_ERR_PRIVSEP_LEN);
817 break;
819 *server_progress = strndup(imsg.data, datalen);
820 if (*server_progress == NULL) {
821 err = got_error_from_errno("strndup");
822 break;
824 for (i = 0; i < datalen; i++) {
825 if (!isprint((unsigned char)(*server_progress)[i]) &&
826 !isspace((unsigned char)(*server_progress)[i])) {
827 err = got_error(GOT_ERR_PRIVSEP_MSG);
828 free(*server_progress);
829 *server_progress = NULL;
830 goto done;
833 break;
834 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
835 if (datalen < sizeof(*packfile_size)) {
836 err = got_error(GOT_ERR_PRIVSEP_MSG);
837 break;
839 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
840 break;
841 case GOT_IMSG_FETCH_DONE:
842 if (datalen != SHA1_DIGEST_LENGTH) {
843 err = got_error(GOT_ERR_PRIVSEP_MSG);
844 break;
846 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
847 *done = 1;
848 break;
849 default:
850 err = got_error(GOT_ERR_PRIVSEP_MSG);
851 break;
853 done:
854 if (err) {
855 free(*id);
856 *id = NULL;
857 free(*refname);
858 *refname = NULL;
860 imsg_free(&imsg);
861 return err;
864 static const struct got_error *
865 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
866 int delete, struct imsgbuf *ibuf)
868 const struct got_error *err = NULL;
869 size_t len;
870 struct ibuf *wbuf;
872 len = sizeof(struct got_imsg_send_ref) + name_len;
873 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
874 if (wbuf == NULL)
875 return got_error_from_errno("imsg_create SEND_REF");
877 /* Keep in sync with struct got_imsg_send_ref! */
878 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
879 err = got_error_from_errno("imsg_add SEND_REF");
880 ibuf_free(wbuf);
881 return err;
883 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1) {
884 err = got_error_from_errno("imsg_add SEND_REF");
885 ibuf_free(wbuf);
886 return err;
888 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
889 err = got_error_from_errno("imsg_add SEND_REF");
890 ibuf_free(wbuf);
891 return err;
893 if (imsg_add(wbuf, name, name_len) == -1) {
894 err = got_error_from_errno("imsg_add SEND_REF");
895 ibuf_free(wbuf);
896 return err;
899 wbuf->fd = -1;
900 imsg_close(ibuf, wbuf);
901 return flush_imsg(ibuf);
904 const struct got_error *
905 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
906 struct got_pathlist_head *have_refs,
907 struct got_pathlist_head *delete_refs,
908 int verbosity)
910 const struct got_error *err = NULL;
911 struct got_pathlist_entry *pe;
912 struct got_imsg_send_request sendreq;
913 struct got_object_id zero_id;
915 memset(&zero_id, 0, sizeof(zero_id));
916 memset(&sendreq, 0, sizeof(sendreq));
917 sendreq.verbosity = verbosity;
918 TAILQ_FOREACH(pe, have_refs, entry)
919 sendreq.nrefs++;
920 TAILQ_FOREACH(pe, delete_refs, entry)
921 sendreq.nrefs++;
922 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
923 &sendreq, sizeof(sendreq)) == -1) {
924 err = got_error_from_errno(
925 "imsg_compose FETCH_SERVER_PROGRESS");
926 goto done;
929 err = flush_imsg(ibuf);
930 if (err)
931 goto done;
932 fd = -1;
934 TAILQ_FOREACH(pe, have_refs, entry) {
935 const char *name = pe->path;
936 size_t name_len = pe->path_len;
937 struct got_object_id *id = pe->data;
938 err = send_send_ref(name, name_len, id, 0, ibuf);
939 if (err)
940 goto done;
943 TAILQ_FOREACH(pe, delete_refs, entry) {
944 const char *name = pe->path;
945 size_t name_len = pe->path_len;
946 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
947 if (err)
948 goto done;
950 done:
951 if (fd != -1 && close(fd) == -1 && err == NULL)
952 err = got_error_from_errno("close");
953 return err;
957 const struct got_error *
958 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
959 struct imsgbuf *ibuf)
961 const struct got_error *err = NULL;
962 struct imsg imsg;
963 size_t datalen;
964 int done = 0;
965 struct got_imsg_send_remote_ref iremote_ref;
966 struct got_object_id *id = NULL;
967 char *refname = NULL;
968 struct got_pathlist_entry *new;
970 while (!done) {
971 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
972 if (err)
973 return err;
974 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
975 switch (imsg.hdr.type) {
976 case GOT_IMSG_ERROR:
977 if (datalen < sizeof(struct got_imsg_error)) {
978 err = got_error(GOT_ERR_PRIVSEP_LEN);
979 goto done;
981 err = recv_imsg_error(&imsg, datalen);
982 goto done;
983 case GOT_IMSG_SEND_REMOTE_REF:
984 if (datalen < sizeof(iremote_ref)) {
985 err = got_error(GOT_ERR_PRIVSEP_MSG);
986 goto done;
988 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
989 if (datalen != sizeof(iremote_ref) +
990 iremote_ref.name_len) {
991 err = got_error(GOT_ERR_PRIVSEP_MSG);
992 goto done;
994 id = malloc(sizeof(*id));
995 if (id == NULL) {
996 err = got_error_from_errno("malloc");
997 goto done;
999 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
1000 refname = strndup(imsg.data + sizeof(iremote_ref),
1001 datalen - sizeof(iremote_ref));
1002 if (refname == NULL) {
1003 err = got_error_from_errno("strndup");
1004 goto done;
1006 err = got_pathlist_insert(&new, remote_refs,
1007 refname, id);
1008 if (err)
1009 goto done;
1010 if (new == NULL) { /* duplicate which wasn't inserted */
1011 free(id);
1012 free(refname);
1014 id = NULL;
1015 refname = NULL;
1016 break;
1017 case GOT_IMSG_SEND_PACK_REQUEST:
1018 if (datalen != 0) {
1019 err = got_error(GOT_ERR_PRIVSEP_MSG);
1020 goto done;
1022 /* got-send-pack is now waiting for a pack file. */
1023 done = 1;
1024 break;
1025 default:
1026 err = got_error(GOT_ERR_PRIVSEP_MSG);
1027 break;
1030 done:
1031 free(id);
1032 free(refname);
1033 imsg_free(&imsg);
1034 return err;
1037 const struct got_error *
1038 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
1040 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
1043 const struct got_error *
1044 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1045 int *success, char **refname, struct imsgbuf *ibuf)
1047 const struct got_error *err = NULL;
1048 struct imsg imsg;
1049 size_t datalen;
1050 struct got_imsg_send_ref_status iref_status;
1052 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1053 *done = 0;
1054 *success = 0;
1055 *refname = NULL;
1057 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1058 if (err)
1059 return err;
1061 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1062 switch (imsg.hdr.type) {
1063 case GOT_IMSG_ERROR:
1064 if (datalen < sizeof(struct got_imsg_error)) {
1065 err = got_error(GOT_ERR_PRIVSEP_LEN);
1066 break;
1068 err = recv_imsg_error(&imsg, datalen);
1069 break;
1070 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1071 if (datalen < sizeof(*bytes_sent)) {
1072 err = got_error(GOT_ERR_PRIVSEP_MSG);
1073 break;
1075 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1076 break;
1077 case GOT_IMSG_SEND_REF_STATUS:
1078 if (datalen < sizeof(iref_status)) {
1079 err = got_error(GOT_ERR_PRIVSEP_MSG);
1080 break;
1082 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1083 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1084 err = got_error(GOT_ERR_PRIVSEP_MSG);
1085 break;
1087 *success = iref_status.success;
1088 *refname = strndup(imsg.data + sizeof(iref_status),
1089 iref_status.name_len);
1090 break;
1091 case GOT_IMSG_SEND_DONE:
1092 if (datalen != 0) {
1093 err = got_error(GOT_ERR_PRIVSEP_MSG);
1094 break;
1096 *done = 1;
1097 break;
1098 default:
1099 err = got_error(GOT_ERR_PRIVSEP_MSG);
1100 break;
1103 imsg_free(&imsg);
1104 return err;
1107 const struct got_error *
1108 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1109 int fd)
1111 const struct got_error *err = NULL;
1113 /* Keep in sync with struct got_imsg_index_pack_request */
1114 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1115 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1116 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1117 close(fd);
1118 return err;
1120 return flush_imsg(ibuf);
1123 const struct got_error *
1124 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1126 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1129 const struct got_error *
1130 got_privsep_recv_index_progress(int *done, int *nobj_total,
1131 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1132 struct imsgbuf *ibuf)
1134 const struct got_error *err = NULL;
1135 struct imsg imsg;
1136 struct got_imsg_index_pack_progress *iprogress;
1137 size_t datalen;
1139 *done = 0;
1140 *nobj_total = 0;
1141 *nobj_indexed = 0;
1142 *nobj_resolved = 0;
1144 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1145 if (err)
1146 return err;
1148 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1149 switch (imsg.hdr.type) {
1150 case GOT_IMSG_ERROR:
1151 if (datalen < sizeof(struct got_imsg_error)) {
1152 err = got_error(GOT_ERR_PRIVSEP_LEN);
1153 break;
1155 err = recv_imsg_error(&imsg, datalen);
1156 break;
1157 case GOT_IMSG_IDXPACK_PROGRESS:
1158 if (datalen < sizeof(*iprogress)) {
1159 err = got_error(GOT_ERR_PRIVSEP_LEN);
1160 break;
1162 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1163 *nobj_total = iprogress->nobj_total;
1164 *nobj_indexed = iprogress->nobj_indexed;
1165 *nobj_loose = iprogress->nobj_loose;
1166 *nobj_resolved = iprogress->nobj_resolved;
1167 break;
1168 case GOT_IMSG_IDXPACK_DONE:
1169 if (datalen != 0) {
1170 err = got_error(GOT_ERR_PRIVSEP_LEN);
1171 break;
1173 *done = 1;
1174 break;
1175 default:
1176 err = got_error(GOT_ERR_PRIVSEP_MSG);
1177 break;
1180 imsg_free(&imsg);
1181 return err;
1184 const struct got_error *
1185 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1186 struct imsgbuf *ibuf)
1188 struct got_imsg_object *iobj;
1189 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1191 if (datalen != sizeof(*iobj))
1192 return got_error(GOT_ERR_PRIVSEP_LEN);
1193 iobj = imsg->data;
1195 *obj = calloc(1, sizeof(**obj));
1196 if (*obj == NULL)
1197 return got_error_from_errno("calloc");
1199 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1200 (*obj)->type = iobj->type;
1201 (*obj)->flags = iobj->flags;
1202 (*obj)->hdrlen = iobj->hdrlen;
1203 (*obj)->size = iobj->size;
1204 /* path_packfile is handled by caller */
1205 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1206 (*obj)->pack_offset = iobj->pack_offset;
1207 (*obj)->pack_idx = iobj->pack_idx;
1209 STAILQ_INIT(&(*obj)->deltas.entries);
1210 return NULL;
1213 const struct got_error *
1214 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1216 const struct got_error *err = NULL;
1217 struct imsg imsg;
1218 const size_t min_datalen =
1219 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1221 *obj = NULL;
1223 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1224 if (err)
1225 return err;
1227 switch (imsg.hdr.type) {
1228 case GOT_IMSG_OBJECT:
1229 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1230 break;
1231 default:
1232 err = got_error(GOT_ERR_PRIVSEP_MSG);
1233 break;
1236 imsg_free(&imsg);
1238 return err;
1241 static const struct got_error *
1242 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1243 size_t logmsg_len)
1245 const struct got_error *err = NULL;
1246 size_t offset, remain;
1248 offset = 0;
1249 remain = logmsg_len;
1250 while (remain > 0) {
1251 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1253 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1254 commit->logmsg + offset, n) == -1) {
1255 err = got_error_from_errno("imsg_compose "
1256 "COMMIT_LOGMSG");
1257 break;
1260 err = flush_imsg(ibuf);
1261 if (err)
1262 break;
1264 offset += n;
1265 remain -= n;
1268 return err;
1271 const struct got_error *
1272 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1274 const struct got_error *err = NULL;
1275 struct got_imsg_commit_object *icommit;
1276 uint8_t *buf;
1277 size_t len, total;
1278 struct got_object_qid *qid;
1279 size_t author_len = strlen(commit->author);
1280 size_t committer_len = strlen(commit->committer);
1281 size_t logmsg_len = strlen(commit->logmsg);
1283 total = sizeof(*icommit) + author_len + committer_len +
1284 commit->nparents * SHA1_DIGEST_LENGTH;
1286 buf = malloc(total);
1287 if (buf == NULL)
1288 return got_error_from_errno("malloc");
1290 icommit = (struct got_imsg_commit_object *)buf;
1291 memcpy(icommit->tree_id, commit->tree_id->sha1,
1292 sizeof(icommit->tree_id));
1293 icommit->author_len = author_len;
1294 icommit->author_time = commit->author_time;
1295 icommit->author_gmtoff = commit->author_gmtoff;
1296 icommit->committer_len = committer_len;
1297 icommit->committer_time = commit->committer_time;
1298 icommit->committer_gmtoff = commit->committer_gmtoff;
1299 icommit->logmsg_len = logmsg_len;
1300 icommit->nparents = commit->nparents;
1302 len = sizeof(*icommit);
1303 memcpy(buf + len, commit->author, author_len);
1304 len += author_len;
1305 memcpy(buf + len, commit->committer, committer_len);
1306 len += committer_len;
1307 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1308 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1309 len += SHA1_DIGEST_LENGTH;
1312 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1313 err = got_error_from_errno("imsg_compose COMMIT");
1314 goto done;
1317 if (logmsg_len == 0 ||
1318 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1319 err = flush_imsg(ibuf);
1320 if (err)
1321 goto done;
1323 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1324 done:
1325 free(buf);
1326 return err;
1329 static const struct got_error *
1330 get_commit_from_imsg(struct got_commit_object **commit,
1331 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1333 const struct got_error *err = NULL;
1334 struct got_imsg_commit_object *icommit;
1335 size_t len = 0;
1336 int i;
1338 if (datalen < sizeof(*icommit))
1339 return got_error(GOT_ERR_PRIVSEP_LEN);
1341 icommit = imsg->data;
1342 if (datalen != sizeof(*icommit) + icommit->author_len +
1343 icommit->committer_len +
1344 icommit->nparents * SHA1_DIGEST_LENGTH)
1345 return got_error(GOT_ERR_PRIVSEP_LEN);
1347 if (icommit->nparents < 0)
1348 return got_error(GOT_ERR_PRIVSEP_LEN);
1350 len += sizeof(*icommit);
1352 *commit = got_object_commit_alloc_partial();
1353 if (*commit == NULL)
1354 return got_error_from_errno(
1355 "got_object_commit_alloc_partial");
1357 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1358 SHA1_DIGEST_LENGTH);
1359 (*commit)->author_time = icommit->author_time;
1360 (*commit)->author_gmtoff = icommit->author_gmtoff;
1361 (*commit)->committer_time = icommit->committer_time;
1362 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1364 if (icommit->author_len == 0) {
1365 (*commit)->author = strdup("");
1366 if ((*commit)->author == NULL) {
1367 err = got_error_from_errno("strdup");
1368 goto done;
1370 } else {
1371 (*commit)->author = malloc(icommit->author_len + 1);
1372 if ((*commit)->author == NULL) {
1373 err = got_error_from_errno("malloc");
1374 goto done;
1376 memcpy((*commit)->author, imsg->data + len,
1377 icommit->author_len);
1378 (*commit)->author[icommit->author_len] = '\0';
1380 len += icommit->author_len;
1382 if (icommit->committer_len == 0) {
1383 (*commit)->committer = strdup("");
1384 if ((*commit)->committer == NULL) {
1385 err = got_error_from_errno("strdup");
1386 goto done;
1388 } else {
1389 (*commit)->committer =
1390 malloc(icommit->committer_len + 1);
1391 if ((*commit)->committer == NULL) {
1392 err = got_error_from_errno("malloc");
1393 goto done;
1395 memcpy((*commit)->committer, imsg->data + len,
1396 icommit->committer_len);
1397 (*commit)->committer[icommit->committer_len] = '\0';
1399 len += icommit->committer_len;
1401 if (icommit->logmsg_len == 0) {
1402 (*commit)->logmsg = strdup("");
1403 if ((*commit)->logmsg == NULL) {
1404 err = got_error_from_errno("strdup");
1405 goto done;
1407 } else {
1408 size_t offset = 0, remain = icommit->logmsg_len;
1410 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1411 if ((*commit)->logmsg == NULL) {
1412 err = got_error_from_errno("malloc");
1413 goto done;
1415 while (remain > 0) {
1416 struct imsg imsg_log;
1417 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1418 remain);
1420 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1421 if (err)
1422 goto done;
1424 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1425 err = got_error(GOT_ERR_PRIVSEP_MSG);
1426 goto done;
1429 memcpy((*commit)->logmsg + offset,
1430 imsg_log.data, n);
1431 imsg_free(&imsg_log);
1432 offset += n;
1433 remain -= n;
1435 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1438 for (i = 0; i < icommit->nparents; i++) {
1439 struct got_object_qid *qid;
1441 err = got_object_qid_alloc_partial(&qid);
1442 if (err)
1443 break;
1444 memcpy(qid->id, imsg->data + len +
1445 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1446 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1447 (*commit)->nparents++;
1449 done:
1450 if (err) {
1451 got_object_commit_close(*commit);
1452 *commit = NULL;
1454 return err;
1457 const struct got_error *
1458 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1460 const struct got_error *err = NULL;
1461 struct imsg imsg;
1462 size_t datalen;
1463 const size_t min_datalen =
1464 MIN(sizeof(struct got_imsg_error),
1465 sizeof(struct got_imsg_commit_object));
1467 *commit = NULL;
1469 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1470 if (err)
1471 return err;
1473 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1475 switch (imsg.hdr.type) {
1476 case GOT_IMSG_COMMIT:
1477 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1478 break;
1479 default:
1480 err = got_error(GOT_ERR_PRIVSEP_MSG);
1481 break;
1484 imsg_free(&imsg);
1486 return err;
1489 const struct got_error *
1490 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1491 int nentries)
1493 const struct got_error *err = NULL;
1494 struct got_imsg_tree_object itree;
1495 struct got_pathlist_entry *pe;
1496 size_t totlen;
1497 int nimsg; /* number of imsg queued in ibuf */
1499 itree.nentries = nentries;
1500 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1501 == -1)
1502 return got_error_from_errno("imsg_compose TREE");
1504 totlen = sizeof(itree);
1505 nimsg = 1;
1506 TAILQ_FOREACH(pe, entries, entry) {
1507 const char *name = pe->path;
1508 struct got_parsed_tree_entry *pte = pe->data;
1509 struct ibuf *wbuf;
1510 size_t namelen = strlen(name);
1511 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1513 if (len > MAX_IMSGSIZE)
1514 return got_error(GOT_ERR_NO_SPACE);
1516 nimsg++;
1517 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1518 err = flush_imsg(ibuf);
1519 if (err)
1520 return err;
1521 nimsg = 0;
1524 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1525 if (wbuf == NULL)
1526 return got_error_from_errno("imsg_create TREE_ENTRY");
1528 /* Keep in sync with struct got_imsg_tree_object definition! */
1529 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1530 err = got_error_from_errno("imsg_add TREE_ENTRY");
1531 ibuf_free(wbuf);
1532 return err;
1534 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1535 err = got_error_from_errno("imsg_add TREE_ENTRY");
1536 ibuf_free(wbuf);
1537 return err;
1540 if (imsg_add(wbuf, name, namelen) == -1) {
1541 err = got_error_from_errno("imsg_add TREE_ENTRY");
1542 ibuf_free(wbuf);
1543 return err;
1546 wbuf->fd = -1;
1547 imsg_close(ibuf, wbuf);
1549 totlen += len;
1552 return flush_imsg(ibuf);
1555 const struct got_error *
1556 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1558 const struct got_error *err = NULL;
1559 const size_t min_datalen =
1560 MIN(sizeof(struct got_imsg_error),
1561 sizeof(struct got_imsg_tree_object));
1562 struct got_imsg_tree_object *itree;
1563 int nentries = 0;
1565 *tree = NULL;
1566 get_more:
1567 err = read_imsg(ibuf);
1568 if (err)
1569 goto done;
1571 for (;;) {
1572 struct imsg imsg;
1573 size_t n;
1574 size_t datalen;
1575 struct got_imsg_tree_entry *ite;
1576 struct got_tree_entry *te = NULL;
1578 n = imsg_get(ibuf, &imsg);
1579 if (n == 0) {
1580 if (*tree && (*tree)->nentries != nentries)
1581 goto get_more;
1582 break;
1585 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1586 imsg_free(&imsg);
1587 err = got_error(GOT_ERR_PRIVSEP_LEN);
1588 break;
1591 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1593 switch (imsg.hdr.type) {
1594 case GOT_IMSG_ERROR:
1595 err = recv_imsg_error(&imsg, datalen);
1596 break;
1597 case GOT_IMSG_TREE:
1598 /* This message should only appear once. */
1599 if (*tree != NULL) {
1600 err = got_error(GOT_ERR_PRIVSEP_MSG);
1601 break;
1603 if (datalen != sizeof(*itree)) {
1604 err = got_error(GOT_ERR_PRIVSEP_LEN);
1605 break;
1607 itree = imsg.data;
1608 *tree = malloc(sizeof(**tree));
1609 if (*tree == NULL) {
1610 err = got_error_from_errno("malloc");
1611 break;
1613 (*tree)->entries = calloc(itree->nentries,
1614 sizeof(struct got_tree_entry));
1615 if ((*tree)->entries == NULL) {
1616 err = got_error_from_errno("malloc");
1617 break;
1619 (*tree)->nentries = itree->nentries;
1620 (*tree)->refcnt = 0;
1621 break;
1622 case GOT_IMSG_TREE_ENTRY:
1623 /* This message should be preceeded by GOT_IMSG_TREE. */
1624 if (*tree == NULL) {
1625 err = got_error(GOT_ERR_PRIVSEP_MSG);
1626 break;
1628 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1629 err = got_error(GOT_ERR_PRIVSEP_LEN);
1630 break;
1633 /* Remaining data contains the entry's name. */
1634 datalen -= sizeof(*ite);
1635 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1636 err = got_error(GOT_ERR_PRIVSEP_LEN);
1637 break;
1639 ite = imsg.data;
1641 if (datalen + 1 > sizeof(te->name)) {
1642 err = got_error(GOT_ERR_NO_SPACE);
1643 break;
1645 te = &(*tree)->entries[nentries];
1646 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1647 te->name[datalen] = '\0';
1649 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1650 te->mode = ite->mode;
1651 te->idx = nentries;
1652 nentries++;
1653 break;
1654 default:
1655 err = got_error(GOT_ERR_PRIVSEP_MSG);
1656 break;
1659 imsg_free(&imsg);
1660 if (err)
1661 break;
1663 done:
1664 if (*tree && (*tree)->nentries != nentries) {
1665 if (err == NULL)
1666 err = got_error(GOT_ERR_PRIVSEP_LEN);
1667 got_object_tree_close(*tree);
1668 *tree = NULL;
1671 return err;
1674 const struct got_error *
1675 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1676 const uint8_t *data)
1678 struct got_imsg_blob iblob;
1680 iblob.size = size;
1681 iblob.hdrlen = hdrlen;
1683 if (data) {
1684 uint8_t *buf;
1686 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1687 return got_error(GOT_ERR_NO_SPACE);
1689 buf = malloc(sizeof(iblob) + size);
1690 if (buf == NULL)
1691 return got_error_from_errno("malloc");
1693 memcpy(buf, &iblob, sizeof(iblob));
1694 memcpy(buf + sizeof(iblob), data, size);
1695 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1696 sizeof(iblob) + size) == -1) {
1697 free(buf);
1698 return got_error_from_errno("imsg_compose BLOB");
1700 free(buf);
1701 } else {
1702 /* Data has already been written to file descriptor. */
1703 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1704 sizeof(iblob)) == -1)
1705 return got_error_from_errno("imsg_compose BLOB");
1709 return flush_imsg(ibuf);
1712 const struct got_error *
1713 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1714 struct imsgbuf *ibuf)
1716 const struct got_error *err = NULL;
1717 struct imsg imsg;
1718 struct got_imsg_blob *iblob;
1719 size_t datalen;
1721 *outbuf = NULL;
1723 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1724 if (err)
1725 return err;
1727 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1729 switch (imsg.hdr.type) {
1730 case GOT_IMSG_BLOB:
1731 if (datalen < sizeof(*iblob)) {
1732 err = got_error(GOT_ERR_PRIVSEP_LEN);
1733 break;
1735 iblob = imsg.data;
1736 *size = iblob->size;
1737 *hdrlen = iblob->hdrlen;
1739 if (datalen == sizeof(*iblob)) {
1740 /* Data has been written to file descriptor. */
1741 break;
1744 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1745 err = got_error(GOT_ERR_PRIVSEP_LEN);
1746 break;
1749 *outbuf = malloc(*size);
1750 if (*outbuf == NULL) {
1751 err = got_error_from_errno("malloc");
1752 break;
1754 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1755 break;
1756 default:
1757 err = got_error(GOT_ERR_PRIVSEP_MSG);
1758 break;
1761 imsg_free(&imsg);
1763 return err;
1766 static const struct got_error *
1767 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1769 const struct got_error *err = NULL;
1770 size_t offset, remain;
1772 offset = 0;
1773 remain = tagmsg_len;
1774 while (remain > 0) {
1775 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1777 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1778 tag->tagmsg + offset, n) == -1) {
1779 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1780 break;
1783 err = flush_imsg(ibuf);
1784 if (err)
1785 break;
1787 offset += n;
1788 remain -= n;
1791 return err;
1794 const struct got_error *
1795 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1797 const struct got_error *err = NULL;
1798 struct got_imsg_tag_object *itag;
1799 uint8_t *buf;
1800 size_t len, total;
1801 size_t tag_len = strlen(tag->tag);
1802 size_t tagger_len = strlen(tag->tagger);
1803 size_t tagmsg_len = strlen(tag->tagmsg);
1805 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1807 buf = malloc(total);
1808 if (buf == NULL)
1809 return got_error_from_errno("malloc");
1811 itag = (struct got_imsg_tag_object *)buf;
1812 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1813 itag->obj_type = tag->obj_type;
1814 itag->tag_len = tag_len;
1815 itag->tagger_len = tagger_len;
1816 itag->tagger_time = tag->tagger_time;
1817 itag->tagger_gmtoff = tag->tagger_gmtoff;
1818 itag->tagmsg_len = tagmsg_len;
1820 len = sizeof(*itag);
1821 memcpy(buf + len, tag->tag, tag_len);
1822 len += tag_len;
1823 memcpy(buf + len, tag->tagger, tagger_len);
1824 len += tagger_len;
1826 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1827 err = got_error_from_errno("imsg_compose TAG");
1828 goto done;
1831 if (tagmsg_len == 0 ||
1832 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1833 err = flush_imsg(ibuf);
1834 if (err)
1835 goto done;
1837 err = send_tagmsg(ibuf, tag, tagmsg_len);
1838 done:
1839 free(buf);
1840 return err;
1843 const struct got_error *
1844 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1846 const struct got_error *err = NULL;
1847 struct imsg imsg;
1848 struct got_imsg_tag_object *itag;
1849 size_t len, datalen;
1850 const size_t min_datalen =
1851 MIN(sizeof(struct got_imsg_error),
1852 sizeof(struct got_imsg_tag_object));
1854 *tag = NULL;
1856 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1857 if (err)
1858 return err;
1860 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1861 len = 0;
1863 switch (imsg.hdr.type) {
1864 case GOT_IMSG_TAG:
1865 if (datalen < sizeof(*itag)) {
1866 err = got_error(GOT_ERR_PRIVSEP_LEN);
1867 break;
1869 itag = imsg.data;
1870 if (datalen != sizeof(*itag) + itag->tag_len +
1871 itag->tagger_len) {
1872 err = got_error(GOT_ERR_PRIVSEP_LEN);
1873 break;
1875 len += sizeof(*itag);
1877 *tag = calloc(1, sizeof(**tag));
1878 if (*tag == NULL) {
1879 err = got_error_from_errno("calloc");
1880 break;
1883 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1885 if (itag->tag_len == 0) {
1886 (*tag)->tag = strdup("");
1887 if ((*tag)->tag == NULL) {
1888 err = got_error_from_errno("strdup");
1889 break;
1891 } else {
1892 (*tag)->tag = malloc(itag->tag_len + 1);
1893 if ((*tag)->tag == NULL) {
1894 err = got_error_from_errno("malloc");
1895 break;
1897 memcpy((*tag)->tag, imsg.data + len,
1898 itag->tag_len);
1899 (*tag)->tag[itag->tag_len] = '\0';
1901 len += itag->tag_len;
1903 (*tag)->obj_type = itag->obj_type;
1904 (*tag)->tagger_time = itag->tagger_time;
1905 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1907 if (itag->tagger_len == 0) {
1908 (*tag)->tagger = strdup("");
1909 if ((*tag)->tagger == NULL) {
1910 err = got_error_from_errno("strdup");
1911 break;
1913 } else {
1914 (*tag)->tagger = malloc(itag->tagger_len + 1);
1915 if ((*tag)->tagger == NULL) {
1916 err = got_error_from_errno("malloc");
1917 break;
1919 memcpy((*tag)->tagger, imsg.data + len,
1920 itag->tagger_len);
1921 (*tag)->tagger[itag->tagger_len] = '\0';
1923 len += itag->tagger_len;
1925 if (itag->tagmsg_len == 0) {
1926 (*tag)->tagmsg = strdup("");
1927 if ((*tag)->tagmsg == NULL) {
1928 err = got_error_from_errno("strdup");
1929 break;
1931 } else {
1932 size_t offset = 0, remain = itag->tagmsg_len;
1934 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1935 if ((*tag)->tagmsg == NULL) {
1936 err = got_error_from_errno("malloc");
1937 break;
1939 while (remain > 0) {
1940 struct imsg imsg_log;
1941 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1942 remain);
1944 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1945 if (err)
1946 return err;
1948 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1949 return got_error(GOT_ERR_PRIVSEP_MSG);
1951 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1952 n);
1953 imsg_free(&imsg_log);
1954 offset += n;
1955 remain -= n;
1957 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1960 break;
1961 default:
1962 err = got_error(GOT_ERR_PRIVSEP_MSG);
1963 break;
1966 imsg_free(&imsg);
1968 return err;
1971 const struct got_error *
1972 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1973 struct got_packidx *packidx)
1975 const struct got_error *err = NULL;
1976 struct got_imsg_packidx ipackidx;
1977 struct got_imsg_pack ipack;
1978 int fd;
1980 ipackidx.len = packidx->len;
1981 ipackidx.packfile_size = pack->filesize;
1982 fd = dup(packidx->fd);
1983 if (fd == -1)
1984 return got_error_from_errno("dup");
1986 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1987 sizeof(ipackidx)) == -1) {
1988 err = got_error_from_errno("imsg_compose PACKIDX");
1989 close(fd);
1990 return err;
1993 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1994 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1995 return got_error(GOT_ERR_NO_SPACE);
1996 ipack.filesize = pack->filesize;
1998 fd = dup(pack->fd);
1999 if (fd == -1)
2000 return got_error_from_errno("dup");
2002 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2003 == -1) {
2004 err = got_error_from_errno("imsg_compose PACK");
2005 close(fd);
2006 return err;
2009 return flush_imsg(ibuf);
2012 const struct got_error *
2013 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2014 struct got_object_id *id)
2016 struct got_imsg_packed_object iobj;
2018 iobj.idx = idx;
2019 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2021 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2022 &iobj, sizeof(iobj)) == -1)
2023 return got_error_from_errno("imsg_compose "
2024 "PACKED_OBJECT_REQUEST");
2026 return flush_imsg(ibuf);
2029 const struct got_error *
2030 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2031 struct got_object_id *id)
2033 struct got_imsg_packed_object iobj;
2035 iobj.idx = idx;
2036 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2038 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2039 &iobj, sizeof(iobj)) == -1)
2040 return got_error_from_errno("imsg_compose "
2041 "PACKED_OBJECT_REQUEST");
2043 return flush_imsg(ibuf);
2046 const struct got_error *
2047 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2049 const struct got_error *err = NULL;
2051 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2052 NULL, 0) == -1) {
2053 err = got_error_from_errno("imsg_compose "
2054 "GITCONFIG_PARSE_REQUEST");
2055 close(fd);
2056 return err;
2059 return flush_imsg(ibuf);
2062 const struct got_error *
2063 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2065 if (imsg_compose(ibuf,
2066 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2067 NULL, 0) == -1)
2068 return got_error_from_errno("imsg_compose "
2069 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2071 return flush_imsg(ibuf);
2074 const struct got_error *
2075 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2077 if (imsg_compose(ibuf,
2078 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2079 NULL, 0) == -1)
2080 return got_error_from_errno("imsg_compose "
2081 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2083 return flush_imsg(ibuf);
2087 const struct got_error *
2088 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2090 if (imsg_compose(ibuf,
2091 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2092 return got_error_from_errno("imsg_compose "
2093 "GITCONFIG_AUTHOR_NAME_REQUEST");
2095 return flush_imsg(ibuf);
2098 const struct got_error *
2099 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2101 if (imsg_compose(ibuf,
2102 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2103 return got_error_from_errno("imsg_compose "
2104 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2106 return flush_imsg(ibuf);
2109 const struct got_error *
2110 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2112 if (imsg_compose(ibuf,
2113 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2114 return got_error_from_errno("imsg_compose "
2115 "GITCONFIG_REMOTE_REQUEST");
2117 return flush_imsg(ibuf);
2120 const struct got_error *
2121 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2123 if (imsg_compose(ibuf,
2124 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2125 return got_error_from_errno("imsg_compose "
2126 "GITCONFIG_OWNER_REQUEST");
2128 return flush_imsg(ibuf);
2131 const struct got_error *
2132 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2134 const struct got_error *err = NULL;
2135 struct imsg imsg;
2136 size_t datalen;
2137 const size_t min_datalen = 0;
2139 *str = NULL;
2141 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2142 if (err)
2143 return err;
2144 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2146 switch (imsg.hdr.type) {
2147 case GOT_IMSG_GITCONFIG_STR_VAL:
2148 if (datalen == 0)
2149 break;
2150 /* datalen does not include terminating \0 */
2151 *str = malloc(datalen + 1);
2152 if (*str == NULL) {
2153 err = got_error_from_errno("malloc");
2154 break;
2156 memcpy(*str, imsg.data, datalen);
2157 (*str)[datalen] = '\0';
2158 break;
2159 default:
2160 err = got_error(GOT_ERR_PRIVSEP_MSG);
2161 break;
2164 imsg_free(&imsg);
2165 return err;
2168 const struct got_error *
2169 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2171 const struct got_error *err = NULL;
2172 struct imsg imsg;
2173 size_t datalen;
2174 const size_t min_datalen =
2175 MIN(sizeof(struct got_imsg_error), sizeof(int));
2177 *val = 0;
2179 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2180 if (err)
2181 return err;
2182 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2184 switch (imsg.hdr.type) {
2185 case GOT_IMSG_GITCONFIG_INT_VAL:
2186 if (datalen != sizeof(*val)) {
2187 err = got_error(GOT_ERR_PRIVSEP_LEN);
2188 break;
2190 memcpy(val, imsg.data, sizeof(*val));
2191 break;
2192 default:
2193 err = got_error(GOT_ERR_PRIVSEP_MSG);
2194 break;
2197 imsg_free(&imsg);
2198 return err;
2201 static void
2202 free_remote_data(struct got_remote_repo *remote)
2204 int i;
2206 free(remote->name);
2207 free(remote->fetch_url);
2208 free(remote->send_url);
2209 for (i = 0; i < remote->nfetch_branches; i++)
2210 free(remote->fetch_branches[i]);
2211 free(remote->fetch_branches);
2212 for (i = 0; i < remote->nsend_branches; i++)
2213 free(remote->send_branches[i]);
2214 free(remote->send_branches);
2215 for (i = 0; i < remote->nfetch_refs; i++)
2216 free(remote->fetch_refs[i]);
2217 free(remote->fetch_refs);
2220 const struct got_error *
2221 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2222 int *nremotes, struct imsgbuf *ibuf)
2224 const struct got_error *err = NULL;
2225 struct imsg imsg;
2226 size_t datalen;
2227 struct got_imsg_remotes iremotes;
2228 struct got_imsg_remote iremote;
2230 *remotes = NULL;
2231 *nremotes = 0;
2232 iremotes.nremotes = 0;
2234 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2235 if (err)
2236 return err;
2237 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2239 switch (imsg.hdr.type) {
2240 case GOT_IMSG_GITCONFIG_REMOTES:
2241 if (datalen != sizeof(iremotes)) {
2242 err = got_error(GOT_ERR_PRIVSEP_LEN);
2243 break;
2245 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2246 if (iremotes.nremotes == 0) {
2247 imsg_free(&imsg);
2248 return NULL;
2250 break;
2251 default:
2252 imsg_free(&imsg);
2253 return got_error(GOT_ERR_PRIVSEP_MSG);
2256 imsg_free(&imsg);
2258 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2259 if (*remotes == NULL)
2260 return got_error_from_errno("recallocarray");
2262 while (*nremotes < iremotes.nremotes) {
2263 struct got_remote_repo *remote;
2265 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2266 if (err)
2267 break;
2268 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2270 switch (imsg.hdr.type) {
2271 case GOT_IMSG_GITCONFIG_REMOTE:
2272 remote = &(*remotes)[*nremotes];
2273 memset(remote, 0, sizeof(*remote));
2274 if (datalen < sizeof(iremote)) {
2275 err = got_error(GOT_ERR_PRIVSEP_LEN);
2276 break;
2278 memcpy(&iremote, imsg.data, sizeof(iremote));
2279 if (iremote.name_len == 0 ||
2280 iremote.fetch_url_len == 0 ||
2281 iremote.send_url_len == 0 ||
2282 (sizeof(iremote) + iremote.name_len +
2283 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2284 err = got_error(GOT_ERR_PRIVSEP_LEN);
2285 break;
2287 remote->name = strndup(imsg.data + sizeof(iremote),
2288 iremote.name_len);
2289 if (remote->name == NULL) {
2290 err = got_error_from_errno("strndup");
2291 break;
2293 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2294 iremote.name_len, iremote.fetch_url_len);
2295 if (remote->fetch_url == NULL) {
2296 err = got_error_from_errno("strndup");
2297 free_remote_data(remote);
2298 break;
2300 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2301 iremote.name_len + iremote.fetch_url_len,
2302 iremote.send_url_len);
2303 if (remote->send_url == NULL) {
2304 err = got_error_from_errno("strndup");
2305 free_remote_data(remote);
2306 break;
2308 remote->mirror_references = iremote.mirror_references;
2309 remote->fetch_all_branches = iremote.fetch_all_branches;
2310 remote->nfetch_branches = 0;
2311 remote->fetch_branches = NULL;
2312 remote->nsend_branches = 0;
2313 remote->send_branches = NULL;
2314 remote->nfetch_refs = 0;
2315 remote->fetch_refs = NULL;
2316 (*nremotes)++;
2317 break;
2318 default:
2319 err = got_error(GOT_ERR_PRIVSEP_MSG);
2320 break;
2323 imsg_free(&imsg);
2324 if (err)
2325 break;
2328 if (err) {
2329 int i;
2330 for (i = 0; i < *nremotes; i++)
2331 free_remote_data(&(*remotes)[i]);
2332 free(*remotes);
2333 *remotes = NULL;
2334 *nremotes = 0;
2336 return err;
2339 const struct got_error *
2340 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2342 const struct got_error *err = NULL;
2344 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2345 NULL, 0) == -1) {
2346 err = got_error_from_errno("imsg_compose "
2347 "GOTCONFIG_PARSE_REQUEST");
2348 close(fd);
2349 return err;
2352 return flush_imsg(ibuf);
2355 const struct got_error *
2356 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2358 if (imsg_compose(ibuf,
2359 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2360 return got_error_from_errno("imsg_compose "
2361 "GOTCONFIG_AUTHOR_REQUEST");
2363 return flush_imsg(ibuf);
2366 const struct got_error *
2367 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2369 if (imsg_compose(ibuf,
2370 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2371 return got_error_from_errno("imsg_compose "
2372 "GOTCONFIG_REMOTE_REQUEST");
2374 return flush_imsg(ibuf);
2377 const struct got_error *
2378 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2380 const struct got_error *err = NULL;
2381 struct imsg imsg;
2382 size_t datalen;
2383 const size_t min_datalen = 0;
2385 *str = NULL;
2387 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2388 if (err)
2389 return err;
2390 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2392 switch (imsg.hdr.type) {
2393 case GOT_IMSG_ERROR:
2394 if (datalen < sizeof(struct got_imsg_error)) {
2395 err = got_error(GOT_ERR_PRIVSEP_LEN);
2396 break;
2398 err = recv_imsg_error(&imsg, datalen);
2399 break;
2400 case GOT_IMSG_GOTCONFIG_STR_VAL:
2401 if (datalen == 0)
2402 break;
2403 /* datalen does not include terminating \0 */
2404 *str = malloc(datalen + 1);
2405 if (*str == NULL) {
2406 err = got_error_from_errno("malloc");
2407 break;
2409 memcpy(*str, imsg.data, datalen);
2410 (*str)[datalen] = '\0';
2411 break;
2412 default:
2413 err = got_error(GOT_ERR_PRIVSEP_MSG);
2414 break;
2417 imsg_free(&imsg);
2418 return err;
2421 const struct got_error *
2422 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2423 int *nremotes, struct imsgbuf *ibuf)
2425 const struct got_error *err = NULL;
2426 struct imsg imsg;
2427 size_t datalen;
2428 struct got_imsg_remotes iremotes;
2429 struct got_imsg_remote iremote;
2430 const size_t min_datalen =
2431 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2433 *remotes = NULL;
2434 *nremotes = 0;
2435 iremotes.nremotes = 0;
2437 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2438 if (err)
2439 return err;
2440 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2442 switch (imsg.hdr.type) {
2443 case GOT_IMSG_ERROR:
2444 if (datalen < sizeof(struct got_imsg_error)) {
2445 err = got_error(GOT_ERR_PRIVSEP_LEN);
2446 break;
2448 err = recv_imsg_error(&imsg, datalen);
2449 break;
2450 case GOT_IMSG_GOTCONFIG_REMOTES:
2451 if (datalen != sizeof(iremotes)) {
2452 err = got_error(GOT_ERR_PRIVSEP_LEN);
2453 break;
2455 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2456 if (iremotes.nremotes == 0) {
2457 imsg_free(&imsg);
2458 return NULL;
2460 break;
2461 default:
2462 imsg_free(&imsg);
2463 return got_error(GOT_ERR_PRIVSEP_MSG);
2466 imsg_free(&imsg);
2468 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2469 if (*remotes == NULL)
2470 return got_error_from_errno("recallocarray");
2472 while (*nremotes < iremotes.nremotes) {
2473 struct got_remote_repo *remote;
2474 const size_t min_datalen =
2475 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2476 int i;
2478 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2479 if (err)
2480 break;
2481 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2483 switch (imsg.hdr.type) {
2484 case GOT_IMSG_ERROR:
2485 if (datalen < sizeof(struct got_imsg_error)) {
2486 err = got_error(GOT_ERR_PRIVSEP_LEN);
2487 break;
2489 err = recv_imsg_error(&imsg, datalen);
2490 break;
2491 case GOT_IMSG_GOTCONFIG_REMOTE:
2492 remote = &(*remotes)[*nremotes];
2493 memset(remote, 0, sizeof(*remote));
2494 if (datalen < sizeof(iremote)) {
2495 err = got_error(GOT_ERR_PRIVSEP_LEN);
2496 break;
2498 memcpy(&iremote, imsg.data, sizeof(iremote));
2499 if (iremote.name_len == 0 ||
2500 (iremote.fetch_url_len == 0 &&
2501 iremote.send_url_len == 0) ||
2502 (sizeof(iremote) + iremote.name_len +
2503 iremote.fetch_url_len + iremote.send_url_len) >
2504 datalen) {
2505 err = got_error(GOT_ERR_PRIVSEP_LEN);
2506 break;
2508 remote->name = strndup(imsg.data + sizeof(iremote),
2509 iremote.name_len);
2510 if (remote->name == NULL) {
2511 err = got_error_from_errno("strndup");
2512 break;
2514 remote->fetch_url = strndup(imsg.data +
2515 sizeof(iremote) + iremote.name_len,
2516 iremote.fetch_url_len);
2517 if (remote->fetch_url == NULL) {
2518 err = got_error_from_errno("strndup");
2519 free_remote_data(remote);
2520 break;
2522 remote->send_url = strndup(imsg.data +
2523 sizeof(iremote) + iremote.name_len +
2524 iremote.fetch_url_len, iremote.send_url_len);
2525 if (remote->send_url == NULL) {
2526 err = got_error_from_errno("strndup");
2527 free_remote_data(remote);
2528 break;
2530 remote->mirror_references = iremote.mirror_references;
2531 remote->fetch_all_branches = iremote.fetch_all_branches;
2532 if (iremote.nfetch_branches > 0) {
2533 remote->fetch_branches = recallocarray(NULL, 0,
2534 iremote.nfetch_branches, sizeof(char *));
2535 if (remote->fetch_branches == NULL) {
2536 err = got_error_from_errno("calloc");
2537 free_remote_data(remote);
2538 break;
2541 remote->nfetch_branches = 0;
2542 for (i = 0; i < iremote.nfetch_branches; i++) {
2543 char *branch;
2544 err = got_privsep_recv_gotconfig_str(&branch,
2545 ibuf);
2546 if (err) {
2547 free_remote_data(remote);
2548 goto done;
2550 remote->fetch_branches[i] = branch;
2551 remote->nfetch_branches++;
2553 if (iremote.nsend_branches > 0) {
2554 remote->send_branches = recallocarray(NULL, 0,
2555 iremote.nsend_branches, sizeof(char *));
2556 if (remote->send_branches == NULL) {
2557 err = got_error_from_errno("calloc");
2558 free_remote_data(remote);
2559 break;
2562 remote->nsend_branches = 0;
2563 for (i = 0; i < iremote.nsend_branches; i++) {
2564 char *branch;
2565 err = got_privsep_recv_gotconfig_str(&branch,
2566 ibuf);
2567 if (err) {
2568 free_remote_data(remote);
2569 goto done;
2571 remote->send_branches[i] = branch;
2572 remote->nsend_branches++;
2574 if (iremote.nfetch_refs > 0) {
2575 remote->fetch_refs = recallocarray(NULL, 0,
2576 iremote.nfetch_refs, sizeof(char *));
2577 if (remote->fetch_refs == NULL) {
2578 err = got_error_from_errno("calloc");
2579 free_remote_data(remote);
2580 break;
2583 remote->nfetch_refs = 0;
2584 for (i = 0; i < iremote.nfetch_refs; i++) {
2585 char *ref;
2586 err = got_privsep_recv_gotconfig_str(&ref,
2587 ibuf);
2588 if (err) {
2589 free_remote_data(remote);
2590 goto done;
2592 remote->fetch_refs[i] = ref;
2593 remote->nfetch_refs++;
2595 (*nremotes)++;
2596 break;
2597 default:
2598 err = got_error(GOT_ERR_PRIVSEP_MSG);
2599 break;
2602 imsg_free(&imsg);
2603 if (err)
2604 break;
2606 done:
2607 if (err) {
2608 int i;
2609 for (i = 0; i < *nremotes; i++)
2610 free_remote_data(&(*remotes)[i]);
2611 free(*remotes);
2612 *remotes = NULL;
2613 *nremotes = 0;
2615 return err;
2618 const struct got_error *
2619 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2620 struct got_object_id *id, int idx, const char *path)
2622 const struct got_error *err = NULL;
2623 struct ibuf *wbuf;
2624 size_t path_len = strlen(path) + 1;
2626 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2627 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2628 if (wbuf == NULL)
2629 return got_error_from_errno(
2630 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2631 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2632 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2633 ibuf_free(wbuf);
2634 return err;
2636 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2637 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2638 ibuf_free(wbuf);
2639 return err;
2641 if (imsg_add(wbuf, path, path_len) == -1) {
2642 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2643 ibuf_free(wbuf);
2644 return err;
2647 wbuf->fd = -1;
2648 imsg_close(ibuf, wbuf);
2650 return flush_imsg(ibuf);
2653 const struct got_error *
2654 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2655 struct got_object_id **changed_commit_id,
2656 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2658 const struct got_error *err = NULL;
2659 struct imsg imsg;
2660 struct got_imsg_traversed_commits *icommits;
2661 size_t datalen;
2662 int i, done = 0;
2664 *changed_commit = NULL;
2665 *changed_commit_id = NULL;
2667 while (!done) {
2668 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2669 if (err)
2670 return err;
2672 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2673 switch (imsg.hdr.type) {
2674 case GOT_IMSG_TRAVERSED_COMMITS:
2675 icommits = imsg.data;
2676 if (datalen != sizeof(*icommits) +
2677 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2678 err = got_error(GOT_ERR_PRIVSEP_LEN);
2679 break;
2681 for (i = 0; i < icommits->ncommits; i++) {
2682 struct got_object_qid *qid;
2683 uint8_t *sha1 = (uint8_t *)imsg.data +
2684 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2685 err = got_object_qid_alloc_partial(&qid);
2686 if (err)
2687 break;
2688 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2689 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2691 /* The last commit may contain a change. */
2692 if (i == icommits->ncommits - 1) {
2693 *changed_commit_id =
2694 got_object_id_dup(qid->id);
2695 if (*changed_commit_id == NULL) {
2696 err = got_error_from_errno(
2697 "got_object_id_dup");
2698 break;
2702 break;
2703 case GOT_IMSG_COMMIT:
2704 if (*changed_commit_id == NULL) {
2705 err = got_error(GOT_ERR_PRIVSEP_MSG);
2706 break;
2708 err = get_commit_from_imsg(changed_commit, &imsg,
2709 datalen, ibuf);
2710 break;
2711 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2712 done = 1;
2713 break;
2714 default:
2715 err = got_error(GOT_ERR_PRIVSEP_MSG);
2716 break;
2719 imsg_free(&imsg);
2720 if (err)
2721 break;
2724 if (err)
2725 got_object_id_queue_free(commit_ids);
2726 return err;
2729 const struct got_error *
2730 got_privsep_unveil_exec_helpers(void)
2732 const char *helpers[] = {
2733 GOT_PATH_PROG_READ_PACK,
2734 GOT_PATH_PROG_READ_OBJECT,
2735 GOT_PATH_PROG_READ_COMMIT,
2736 GOT_PATH_PROG_READ_TREE,
2737 GOT_PATH_PROG_READ_BLOB,
2738 GOT_PATH_PROG_READ_TAG,
2739 GOT_PATH_PROG_READ_GITCONFIG,
2740 GOT_PATH_PROG_READ_GOTCONFIG,
2741 GOT_PATH_PROG_FETCH_PACK,
2742 GOT_PATH_PROG_INDEX_PACK,
2743 GOT_PATH_PROG_SEND_PACK,
2745 size_t i;
2747 for (i = 0; i < nitems(helpers); i++) {
2748 if (unveil(helpers[i], "x") == 0)
2749 continue;
2750 return got_error_from_errno2("unveil", helpers[i]);
2753 return NULL;
2756 void
2757 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2759 if (close(imsg_fds[0]) == -1) {
2760 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2761 _exit(1);
2764 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2765 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2766 _exit(1);
2769 closefrom(GOT_IMSG_FD_CHILD + 1);
2771 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2772 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2773 strerror(errno));
2774 _exit(1);