2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
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.
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.
18 #include <sys/types.h>
19 #include <sys/queue.h>
38 #include "got_object.h"
39 #include "got_error.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"
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 static const struct got_error *
60 poll_fd(int fd, int events, int timeout)
68 pfd[0].events = events;
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);
80 return got_error_from_errno("ppoll");
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))
88 return got_error(GOT_ERR_INTERRUPT);
91 static const struct got_error *
92 read_imsg(struct imsgbuf *ibuf)
94 const struct got_error *err;
98 * There is no imsg API function to tell us whether the
99 * read buffer still contains pending data :-(
101 if (ibuf->r.wpos < IMSG_HEADER_SIZE) {
102 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
109 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
110 return got_error(GOT_ERR_PRIVSEP_NO_FD);
111 return got_error(GOT_ERR_PRIVSEP_READ);
114 return got_error(GOT_ERR_PRIVSEP_PIPE);
119 const struct got_error *
120 got_privsep_wait_for_child(pid_t pid)
124 if (waitpid(pid, &child_status, 0) == -1)
125 return got_error_from_errno("waitpid");
127 if (!WIFEXITED(child_status))
128 return got_error(GOT_ERR_PRIVSEP_DIED);
130 if (WEXITSTATUS(child_status) != 0)
131 return got_error(GOT_ERR_PRIVSEP_EXIT);
136 static const struct got_error *
137 recv_imsg_error(struct imsg *imsg, size_t datalen)
139 struct got_imsg_error *ierr;
141 if (datalen != sizeof(*ierr))
142 return got_error(GOT_ERR_PRIVSEP_LEN);
145 if (ierr->code == GOT_ERR_ERRNO) {
146 static struct got_error serr;
147 serr.code = GOT_ERR_ERRNO;
148 serr.msg = strerror(ierr->errno_code);
152 return got_error(ierr->code);
155 const struct got_error *
156 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
159 const struct got_error *err;
162 n = imsg_get(ibuf, imsg);
164 return got_error_from_errno("imsg_get");
167 err = read_imsg(ibuf);
170 n = imsg_get(ibuf, imsg);
172 return got_error_from_errno("imsg_get");
175 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
176 return got_error(GOT_ERR_PRIVSEP_LEN);
178 if (imsg->hdr.type == GOT_IMSG_ERROR) {
179 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
180 return recv_imsg_error(imsg, datalen);
186 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
188 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
190 const struct got_error *poll_err;
191 struct got_imsg_error ierr;
194 ierr.code = err->code;
195 if (err->code == GOT_ERR_ERRNO)
196 ierr.errno_code = errno;
199 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
201 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
202 getprogname(), err->code, err->msg, strerror(errno));
206 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
208 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
209 getprogname(), err->code, err->msg, poll_err->msg);
213 ret = imsg_flush(ibuf);
215 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
216 getprogname(), err->code, err->msg, strerror(errno));
222 static const struct got_error *
223 flush_imsg(struct imsgbuf *ibuf)
225 const struct got_error *err;
227 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
231 if (imsg_flush(ibuf) == -1) {
233 return got_error_from_errno("imsg_flush");
239 const struct got_error *
240 got_privsep_flush_imsg(struct imsgbuf *ibuf)
242 return flush_imsg(ibuf);
245 const struct got_error *
246 got_privsep_send_stop(int fd)
248 const struct got_error *err = NULL;
251 imsg_init(&ibuf, fd);
253 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
254 return got_error_from_errno("imsg_compose STOP");
256 err = flush_imsg(&ibuf);
260 const struct got_error *
261 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
262 struct got_object_id *id)
264 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
265 id, sizeof(*id)) == -1)
266 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
268 return flush_imsg(ibuf);
271 const struct got_error *
272 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
273 struct got_object_id *id)
275 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
276 id, sizeof(*id)) == -1)
277 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
279 return flush_imsg(ibuf);
282 const struct got_error *
283 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
285 const struct got_error *err = NULL;
287 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
289 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
294 return flush_imsg(ibuf);
297 const struct got_error *
298 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
301 const struct got_error *err = NULL;
302 struct got_imsg_raw_obj iobj;
303 size_t len = sizeof(iobj);
306 iobj.hdrlen = hdrlen;
309 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
310 len += (size_t)size + hdrlen;
312 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
314 err = got_error_from_errno("imsg_create RAW_OBJECT");
318 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
319 return got_error_from_errno("imsg_add RAW_OBJECT");
321 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
322 if (imsg_add(wbuf, data, size + hdrlen) == -1)
323 return got_error_from_errno("imsg_add RAW_OBJECT");
327 imsg_close(ibuf, wbuf);
329 return flush_imsg(ibuf);
332 const struct got_error *
333 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
334 struct imsgbuf *ibuf)
336 const struct got_error *err = NULL;
338 struct got_imsg_raw_obj *iobj;
343 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
347 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
349 switch (imsg.hdr.type) {
350 case GOT_IMSG_RAW_OBJECT:
351 if (datalen < sizeof(*iobj)) {
352 err = got_error(GOT_ERR_PRIVSEP_LEN);
357 *hdrlen = iobj->hdrlen;
359 if (datalen == sizeof(*iobj)) {
360 /* Data has been written to file descriptor. */
364 if (*size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
365 err = got_error(GOT_ERR_PRIVSEP_LEN);
369 *outbuf = malloc(*size + *hdrlen);
370 if (*outbuf == NULL) {
371 err = got_error_from_errno("malloc");
374 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
377 err = got_error(GOT_ERR_PRIVSEP_MSG);
386 const struct got_error *
387 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
388 struct got_object_id *id, int pack_idx)
390 const struct got_error *err = NULL;
391 struct got_imsg_packed_object iobj;
395 if (pack_idx != -1) { /* commit is packed */
397 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
405 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
407 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
412 return flush_imsg(ibuf);
415 const struct got_error *
416 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
417 struct got_object_id *id, int pack_idx)
423 len = sizeof(struct got_imsg_packed_object);
427 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
429 return got_error_from_errno("imsg_create TREE_REQUEST");
431 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
432 return got_error_from_errno("imsg_add TREE_REQUEST");
434 if (pack_idx != -1) { /* tree is packed */
435 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
436 return got_error_from_errno("imsg_add TREE_REQUEST");
440 imsg_close(ibuf, wbuf);
442 return flush_imsg(ibuf);
445 const struct got_error *
446 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
447 struct got_object_id *id, int pack_idx)
449 struct got_imsg_packed_object iobj;
453 if (pack_idx != -1) { /* tag is packed */
455 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
463 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
465 return got_error_from_errno("imsg_compose TAG_REQUEST");
467 return flush_imsg(ibuf);
470 const struct got_error *
471 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
472 struct got_object_id *id, int pack_idx)
474 const struct got_error *err = NULL;
475 struct got_imsg_packed_object iobj;
479 if (pack_idx != -1) { /* blob is packed */
481 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
489 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
491 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
496 return flush_imsg(ibuf);
499 const struct got_error *
500 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
502 const struct got_error *err = NULL;
504 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
506 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
511 return flush_imsg(ibuf);
514 static const struct got_error *
515 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
517 const struct got_error *err = NULL;
519 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
520 err = got_error_from_errno("imsg_compose TMPFD");
525 return flush_imsg(ibuf);
528 const struct got_error *
529 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
531 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
534 const struct got_error *
535 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
537 struct got_imsg_object iobj;
539 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
540 iobj.type = obj->type;
541 iobj.flags = obj->flags;
542 iobj.hdrlen = obj->hdrlen;
543 iobj.size = obj->size;
544 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
545 iobj.pack_offset = obj->pack_offset;
546 iobj.pack_idx = obj->pack_idx;
549 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
551 return got_error_from_errno("imsg_compose OBJECT");
553 return flush_imsg(ibuf);
556 const struct got_error *
557 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
558 struct got_pathlist_head *have_refs, int fetch_all_branches,
559 struct got_pathlist_head *wanted_branches,
560 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
562 const struct got_error *err = NULL;
565 struct got_pathlist_entry *pe;
566 struct got_imsg_fetch_request fetchreq;
568 memset(&fetchreq, 0, sizeof(fetchreq));
569 fetchreq.fetch_all_branches = fetch_all_branches;
570 fetchreq.list_refs_only = list_refs_only;
571 fetchreq.verbosity = verbosity;
572 TAILQ_FOREACH(pe, have_refs, entry)
573 fetchreq.n_have_refs++;
574 TAILQ_FOREACH(pe, wanted_branches, entry)
575 fetchreq.n_wanted_branches++;
576 TAILQ_FOREACH(pe, wanted_refs, entry)
577 fetchreq.n_wanted_refs++;
578 len = sizeof(struct got_imsg_fetch_request);
579 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
581 return got_error(GOT_ERR_NO_SPACE);
584 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
585 &fetchreq, sizeof(fetchreq)) == -1)
586 return got_error_from_errno(
587 "imsg_compose FETCH_SERVER_PROGRESS");
589 err = flush_imsg(ibuf);
596 TAILQ_FOREACH(pe, have_refs, entry) {
597 const char *name = pe->path;
598 size_t name_len = pe->path_len;
599 struct got_object_id *id = pe->data;
601 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
602 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
604 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
606 /* Keep in sync with struct got_imsg_fetch_have_ref! */
607 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
608 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
609 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
610 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
611 if (imsg_add(wbuf, name, name_len) == -1)
612 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
615 imsg_close(ibuf, wbuf);
616 err = flush_imsg(ibuf);
621 TAILQ_FOREACH(pe, wanted_branches, entry) {
622 const char *name = pe->path;
623 size_t name_len = pe->path_len;
625 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
626 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
629 return got_error_from_errno(
630 "imsg_create FETCH_WANTED_BRANCH");
632 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
633 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
634 return got_error_from_errno(
635 "imsg_add FETCH_WANTED_BRANCH");
636 if (imsg_add(wbuf, name, name_len) == -1)
637 return got_error_from_errno(
638 "imsg_add FETCH_WANTED_BRANCH");
641 imsg_close(ibuf, wbuf);
642 err = flush_imsg(ibuf);
647 TAILQ_FOREACH(pe, wanted_refs, entry) {
648 const char *name = pe->path;
649 size_t name_len = pe->path_len;
651 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
652 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
655 return got_error_from_errno(
656 "imsg_create FETCH_WANTED_REF");
658 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
659 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
660 return got_error_from_errno(
661 "imsg_add FETCH_WANTED_REF");
662 if (imsg_add(wbuf, name, name_len) == -1)
663 return got_error_from_errno(
664 "imsg_add FETCH_WANTED_REF");
667 imsg_close(ibuf, wbuf);
668 err = flush_imsg(ibuf);
678 const struct got_error *
679 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
681 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
684 const struct got_error *
685 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
686 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
687 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
689 const struct got_error *err = NULL;
692 struct got_imsg_fetch_symrefs *isymrefs = NULL;
700 *server_progress = NULL;
702 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
704 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
708 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
709 switch (imsg.hdr.type) {
711 if (datalen < sizeof(struct got_imsg_error)) {
712 err = got_error(GOT_ERR_PRIVSEP_LEN);
715 err = recv_imsg_error(&imsg, datalen);
717 case GOT_IMSG_FETCH_SYMREFS:
718 if (datalen < sizeof(*isymrefs)) {
719 err = got_error(GOT_ERR_PRIVSEP_LEN);
722 if (isymrefs != NULL) {
723 err = got_error(GOT_ERR_PRIVSEP_MSG);
726 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
727 off = sizeof(*isymrefs);
728 remain = datalen - off;
729 for (n = 0; n < isymrefs->nsymrefs; n++) {
730 struct got_imsg_fetch_symref *s;
732 if (remain < sizeof(struct got_imsg_fetch_symref)) {
733 err = got_error(GOT_ERR_PRIVSEP_LEN);
736 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
738 remain -= sizeof(*s);
739 if (remain < s->name_len) {
740 err = got_error(GOT_ERR_PRIVSEP_LEN);
743 name = strndup(imsg.data + off, s->name_len);
745 err = got_error_from_errno("strndup");
749 remain -= s->name_len;
750 if (remain < s->target_len) {
751 err = got_error(GOT_ERR_PRIVSEP_LEN);
755 target = strndup(imsg.data + off, s->target_len);
756 if (target == NULL) {
757 err = got_error_from_errno("strndup");
761 off += s->target_len;
762 remain -= s->target_len;
763 err = got_pathlist_append(symrefs, name, target);
771 case GOT_IMSG_FETCH_REF:
772 if (datalen <= SHA1_DIGEST_LENGTH) {
773 err = got_error(GOT_ERR_PRIVSEP_MSG);
776 *id = malloc(sizeof(**id));
778 err = got_error_from_errno("malloc");
781 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
782 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
783 datalen - SHA1_DIGEST_LENGTH);
784 if (*refname == NULL) {
785 err = got_error_from_errno("strndup");
789 case GOT_IMSG_FETCH_SERVER_PROGRESS:
791 err = got_error(GOT_ERR_PRIVSEP_LEN);
794 *server_progress = strndup(imsg.data, datalen);
795 if (*server_progress == NULL) {
796 err = got_error_from_errno("strndup");
799 for (i = 0; i < datalen; i++) {
800 if (!isprint((unsigned char)(*server_progress)[i]) &&
801 !isspace((unsigned char)(*server_progress)[i])) {
802 err = got_error(GOT_ERR_PRIVSEP_MSG);
803 free(*server_progress);
804 *server_progress = NULL;
809 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
810 if (datalen < sizeof(*packfile_size)) {
811 err = got_error(GOT_ERR_PRIVSEP_MSG);
814 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
816 case GOT_IMSG_FETCH_DONE:
817 if (datalen != SHA1_DIGEST_LENGTH) {
818 err = got_error(GOT_ERR_PRIVSEP_MSG);
821 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
825 err = got_error(GOT_ERR_PRIVSEP_MSG);
839 static const struct got_error *
840 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
841 int delete, struct imsgbuf *ibuf)
846 len = sizeof(struct got_imsg_send_ref) + name_len;
847 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
849 return got_error_from_errno("imsg_create SEND_REF");
851 /* Keep in sync with struct got_imsg_send_ref! */
852 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
853 return got_error_from_errno("imsg_add SEND_REF");
854 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
855 return got_error_from_errno("imsg_add SEND_REF");
856 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
857 return got_error_from_errno("imsg_add SEND_REF");
858 if (imsg_add(wbuf, name, name_len) == -1)
859 return got_error_from_errno("imsg_add SEND_REF");
862 imsg_close(ibuf, wbuf);
863 return flush_imsg(ibuf);
866 const struct got_error *
867 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
868 struct got_pathlist_head *have_refs,
869 struct got_pathlist_head *delete_refs,
872 const struct got_error *err = NULL;
873 struct got_pathlist_entry *pe;
874 struct got_imsg_send_request sendreq;
875 struct got_object_id zero_id;
877 memset(&zero_id, 0, sizeof(zero_id));
878 memset(&sendreq, 0, sizeof(sendreq));
879 sendreq.verbosity = verbosity;
880 TAILQ_FOREACH(pe, have_refs, entry)
882 TAILQ_FOREACH(pe, delete_refs, entry)
884 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
885 &sendreq, sizeof(sendreq)) == -1) {
886 err = got_error_from_errno(
887 "imsg_compose FETCH_SERVER_PROGRESS");
891 err = flush_imsg(ibuf);
896 TAILQ_FOREACH(pe, have_refs, entry) {
897 const char *name = pe->path;
898 size_t name_len = pe->path_len;
899 struct got_object_id *id = pe->data;
900 err = send_send_ref(name, name_len, id, 0, ibuf);
905 TAILQ_FOREACH(pe, delete_refs, entry) {
906 const char *name = pe->path;
907 size_t name_len = pe->path_len;
908 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
913 if (fd != -1 && close(fd) == -1 && err == NULL)
914 err = got_error_from_errno("close");
919 const struct got_error *
920 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
921 struct imsgbuf *ibuf)
923 const struct got_error *err = NULL;
927 struct got_imsg_send_remote_ref iremote_ref;
928 struct got_object_id *id = NULL;
929 char *refname = NULL;
930 struct got_pathlist_entry *new;
933 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
936 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
937 switch (imsg.hdr.type) {
939 if (datalen < sizeof(struct got_imsg_error)) {
940 err = got_error(GOT_ERR_PRIVSEP_LEN);
943 err = recv_imsg_error(&imsg, datalen);
945 case GOT_IMSG_SEND_REMOTE_REF:
946 if (datalen < sizeof(iremote_ref)) {
947 err = got_error(GOT_ERR_PRIVSEP_MSG);
950 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
951 if (datalen != sizeof(iremote_ref) +
952 iremote_ref.name_len) {
953 err = got_error(GOT_ERR_PRIVSEP_MSG);
956 id = malloc(sizeof(*id));
958 err = got_error_from_errno("malloc");
961 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
962 refname = strndup(imsg.data + sizeof(iremote_ref),
963 datalen - sizeof(iremote_ref));
964 if (refname == NULL) {
965 err = got_error_from_errno("strndup");
968 err = got_pathlist_insert(&new, remote_refs,
972 if (new == NULL) { /* duplicate which wasn't inserted */
979 case GOT_IMSG_SEND_PACK_REQUEST:
981 err = got_error(GOT_ERR_PRIVSEP_MSG);
984 /* got-send-pack is now waiting for a pack file. */
988 err = got_error(GOT_ERR_PRIVSEP_MSG);
999 const struct got_error *
1000 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
1002 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
1005 const struct got_error *
1006 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1007 int *success, char **refname, struct imsgbuf *ibuf)
1009 const struct got_error *err = NULL;
1012 struct got_imsg_send_ref_status iref_status;
1014 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1019 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1023 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1024 switch (imsg.hdr.type) {
1025 case GOT_IMSG_ERROR:
1026 if (datalen < sizeof(struct got_imsg_error)) {
1027 err = got_error(GOT_ERR_PRIVSEP_LEN);
1030 err = recv_imsg_error(&imsg, datalen);
1032 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1033 if (datalen < sizeof(*bytes_sent)) {
1034 err = got_error(GOT_ERR_PRIVSEP_MSG);
1037 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1039 case GOT_IMSG_SEND_REF_STATUS:
1040 if (datalen < sizeof(iref_status)) {
1041 err = got_error(GOT_ERR_PRIVSEP_MSG);
1044 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1045 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1046 err = got_error(GOT_ERR_PRIVSEP_MSG);
1049 *success = iref_status.success;
1050 *refname = strndup(imsg.data + sizeof(iref_status),
1051 iref_status.name_len);
1053 case GOT_IMSG_SEND_DONE:
1055 err = got_error(GOT_ERR_PRIVSEP_MSG);
1061 err = got_error(GOT_ERR_PRIVSEP_MSG);
1069 const struct got_error *
1070 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1073 const struct got_error *err = NULL;
1075 /* Keep in sync with struct got_imsg_index_pack_request */
1076 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1077 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1078 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1082 return flush_imsg(ibuf);
1085 const struct got_error *
1086 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1088 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1091 const struct got_error *
1092 got_privsep_recv_index_progress(int *done, int *nobj_total,
1093 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1094 struct imsgbuf *ibuf)
1096 const struct got_error *err = NULL;
1098 struct got_imsg_index_pack_progress *iprogress;
1106 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1110 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1111 switch (imsg.hdr.type) {
1112 case GOT_IMSG_ERROR:
1113 if (datalen < sizeof(struct got_imsg_error)) {
1114 err = got_error(GOT_ERR_PRIVSEP_LEN);
1117 err = recv_imsg_error(&imsg, datalen);
1119 case GOT_IMSG_IDXPACK_PROGRESS:
1120 if (datalen < sizeof(*iprogress)) {
1121 err = got_error(GOT_ERR_PRIVSEP_LEN);
1124 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1125 *nobj_total = iprogress->nobj_total;
1126 *nobj_indexed = iprogress->nobj_indexed;
1127 *nobj_loose = iprogress->nobj_loose;
1128 *nobj_resolved = iprogress->nobj_resolved;
1130 case GOT_IMSG_IDXPACK_DONE:
1132 err = got_error(GOT_ERR_PRIVSEP_LEN);
1138 err = got_error(GOT_ERR_PRIVSEP_MSG);
1146 const struct got_error *
1147 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1148 struct imsgbuf *ibuf)
1150 struct got_imsg_object *iobj;
1151 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1153 if (datalen != sizeof(*iobj))
1154 return got_error(GOT_ERR_PRIVSEP_LEN);
1157 *obj = calloc(1, sizeof(**obj));
1159 return got_error_from_errno("calloc");
1161 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1162 (*obj)->type = iobj->type;
1163 (*obj)->flags = iobj->flags;
1164 (*obj)->hdrlen = iobj->hdrlen;
1165 (*obj)->size = iobj->size;
1166 /* path_packfile is handled by caller */
1167 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1168 (*obj)->pack_offset = iobj->pack_offset;
1169 (*obj)->pack_idx = iobj->pack_idx;
1171 STAILQ_INIT(&(*obj)->deltas.entries);
1175 const struct got_error *
1176 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1178 const struct got_error *err = NULL;
1180 const size_t min_datalen =
1181 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1185 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1189 switch (imsg.hdr.type) {
1190 case GOT_IMSG_OBJECT:
1191 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1194 err = got_error(GOT_ERR_PRIVSEP_MSG);
1203 static const struct got_error *
1204 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1207 const struct got_error *err = NULL;
1208 size_t offset, remain;
1211 remain = logmsg_len;
1212 while (remain > 0) {
1213 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1215 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1216 commit->logmsg + offset, n) == -1) {
1217 err = got_error_from_errno("imsg_compose "
1222 err = flush_imsg(ibuf);
1233 const struct got_error *
1234 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1236 const struct got_error *err = NULL;
1237 struct got_imsg_commit_object *icommit;
1240 struct got_object_qid *qid;
1241 size_t author_len = strlen(commit->author);
1242 size_t committer_len = strlen(commit->committer);
1243 size_t logmsg_len = strlen(commit->logmsg);
1245 total = sizeof(*icommit) + author_len + committer_len +
1246 commit->nparents * SHA1_DIGEST_LENGTH;
1248 buf = malloc(total);
1250 return got_error_from_errno("malloc");
1252 icommit = (struct got_imsg_commit_object *)buf;
1253 memcpy(icommit->tree_id, commit->tree_id->sha1,
1254 sizeof(icommit->tree_id));
1255 icommit->author_len = author_len;
1256 icommit->author_time = commit->author_time;
1257 icommit->author_gmtoff = commit->author_gmtoff;
1258 icommit->committer_len = committer_len;
1259 icommit->committer_time = commit->committer_time;
1260 icommit->committer_gmtoff = commit->committer_gmtoff;
1261 icommit->logmsg_len = logmsg_len;
1262 icommit->nparents = commit->nparents;
1264 len = sizeof(*icommit);
1265 memcpy(buf + len, commit->author, author_len);
1267 memcpy(buf + len, commit->committer, committer_len);
1268 len += committer_len;
1269 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1270 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1271 len += SHA1_DIGEST_LENGTH;
1274 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1275 err = got_error_from_errno("imsg_compose COMMIT");
1279 if (logmsg_len == 0 ||
1280 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1281 err = flush_imsg(ibuf);
1285 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1291 static const struct got_error *
1292 get_commit_from_imsg(struct got_commit_object **commit,
1293 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1295 const struct got_error *err = NULL;
1296 struct got_imsg_commit_object *icommit;
1300 if (datalen < sizeof(*icommit))
1301 return got_error(GOT_ERR_PRIVSEP_LEN);
1303 icommit = imsg->data;
1304 if (datalen != sizeof(*icommit) + icommit->author_len +
1305 icommit->committer_len +
1306 icommit->nparents * SHA1_DIGEST_LENGTH)
1307 return got_error(GOT_ERR_PRIVSEP_LEN);
1309 if (icommit->nparents < 0)
1310 return got_error(GOT_ERR_PRIVSEP_LEN);
1312 len += sizeof(*icommit);
1314 *commit = got_object_commit_alloc_partial();
1315 if (*commit == NULL)
1316 return got_error_from_errno(
1317 "got_object_commit_alloc_partial");
1319 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1320 SHA1_DIGEST_LENGTH);
1321 (*commit)->author_time = icommit->author_time;
1322 (*commit)->author_gmtoff = icommit->author_gmtoff;
1323 (*commit)->committer_time = icommit->committer_time;
1324 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1326 if (icommit->author_len == 0) {
1327 (*commit)->author = strdup("");
1328 if ((*commit)->author == NULL) {
1329 err = got_error_from_errno("strdup");
1333 (*commit)->author = malloc(icommit->author_len + 1);
1334 if ((*commit)->author == NULL) {
1335 err = got_error_from_errno("malloc");
1338 memcpy((*commit)->author, imsg->data + len,
1339 icommit->author_len);
1340 (*commit)->author[icommit->author_len] = '\0';
1342 len += icommit->author_len;
1344 if (icommit->committer_len == 0) {
1345 (*commit)->committer = strdup("");
1346 if ((*commit)->committer == NULL) {
1347 err = got_error_from_errno("strdup");
1351 (*commit)->committer =
1352 malloc(icommit->committer_len + 1);
1353 if ((*commit)->committer == NULL) {
1354 err = got_error_from_errno("malloc");
1357 memcpy((*commit)->committer, imsg->data + len,
1358 icommit->committer_len);
1359 (*commit)->committer[icommit->committer_len] = '\0';
1361 len += icommit->committer_len;
1363 if (icommit->logmsg_len == 0) {
1364 (*commit)->logmsg = strdup("");
1365 if ((*commit)->logmsg == NULL) {
1366 err = got_error_from_errno("strdup");
1370 size_t offset = 0, remain = icommit->logmsg_len;
1372 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1373 if ((*commit)->logmsg == NULL) {
1374 err = got_error_from_errno("malloc");
1377 while (remain > 0) {
1378 struct imsg imsg_log;
1379 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1382 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1386 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1387 err = got_error(GOT_ERR_PRIVSEP_MSG);
1391 memcpy((*commit)->logmsg + offset,
1393 imsg_free(&imsg_log);
1397 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1400 for (i = 0; i < icommit->nparents; i++) {
1401 struct got_object_qid *qid;
1403 err = got_object_qid_alloc_partial(&qid);
1406 memcpy(&qid->id, imsg->data + len +
1407 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1408 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1409 (*commit)->nparents++;
1413 got_object_commit_close(*commit);
1419 const struct got_error *
1420 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1422 const struct got_error *err = NULL;
1425 const size_t min_datalen =
1426 MIN(sizeof(struct got_imsg_error),
1427 sizeof(struct got_imsg_commit_object));
1431 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1435 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1437 switch (imsg.hdr.type) {
1438 case GOT_IMSG_COMMIT:
1439 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1442 err = got_error(GOT_ERR_PRIVSEP_MSG);
1451 static const struct got_error *
1452 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1453 int idx0, int idxN, size_t len)
1456 struct got_imsg_tree_entries ientries;
1459 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1461 return got_error_from_errno("imsg_create TREE_ENTRY");
1463 ientries.nentries = idxN - idx0 + 1;
1464 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1465 return got_error_from_errno("imsg_add TREE_ENTRY");
1467 for (i = idx0; i <= idxN; i++) {
1468 struct got_parsed_tree_entry *pte = &entries[i];
1470 /* Keep in sync with struct got_imsg_tree_object definition! */
1471 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1472 return got_error_from_errno("imsg_add TREE_ENTRY");
1473 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1474 return got_error_from_errno("imsg_add TREE_ENTRY");
1475 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1476 return got_error_from_errno("imsg_add TREE_ENTRY");
1478 /* Remaining bytes are the entry's name. */
1479 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1480 return got_error_from_errno("imsg_add TREE_ENTRY");
1484 imsg_close(ibuf, wbuf);
1488 const struct got_error *
1489 got_privsep_send_tree(struct imsgbuf *ibuf,
1490 struct got_parsed_tree_entry *entries, int nentries)
1492 const struct got_error *err = NULL;
1493 struct got_imsg_tree_object itree;
1497 itree.nentries = nentries;
1498 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1500 return got_error_from_errno("imsg_compose TREE");
1502 entries_len = sizeof(struct got_imsg_tree_entries);
1504 for (j = 0; j < nentries; j++) {
1505 struct got_parsed_tree_entry *pte = &entries[j];
1506 size_t len = sizeof(*pte) + pte->namelen;
1509 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1510 err = send_tree_entries(ibuf, entries,
1511 i, j - 1, entries_len);
1515 entries_len = sizeof(struct got_imsg_tree_entries);
1522 err = send_tree_entries(ibuf, entries, i, j - 1, entries_len);
1527 return flush_imsg(ibuf);
1530 const struct got_error *
1531 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1533 const struct got_error *err = NULL;
1534 const size_t min_datalen =
1535 MIN(sizeof(struct got_imsg_error),
1536 sizeof(struct got_imsg_tree_object));
1537 struct got_imsg_tree_object *itree;
1543 err = read_imsg(ibuf);
1551 struct got_imsg_tree_entries *ientries;
1552 struct got_tree_entry *te = NULL;
1555 n = imsg_get(ibuf, &imsg);
1558 if (nentries < (*tree)->nentries) {
1559 err = read_imsg(ibuf);
1566 err = got_error(GOT_ERR_PRIVSEP_MSG);
1571 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1573 err = got_error(GOT_ERR_PRIVSEP_LEN);
1577 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1579 switch (imsg.hdr.type) {
1580 case GOT_IMSG_ERROR:
1581 err = recv_imsg_error(&imsg, datalen);
1584 /* This message should only appear once. */
1585 if (*tree != NULL) {
1586 err = got_error(GOT_ERR_PRIVSEP_MSG);
1589 if (datalen != sizeof(*itree)) {
1590 err = got_error(GOT_ERR_PRIVSEP_LEN);
1594 if (itree->nentries < 0) {
1595 err = got_error(GOT_ERR_PRIVSEP_LEN);
1598 *tree = malloc(sizeof(**tree));
1599 if (*tree == NULL) {
1600 err = got_error_from_errno("malloc");
1603 (*tree)->entries = calloc(itree->nentries,
1604 sizeof(struct got_tree_entry));
1605 if ((*tree)->entries == NULL) {
1606 err = got_error_from_errno("malloc");
1611 (*tree)->nentries = itree->nentries;
1612 (*tree)->refcnt = 0;
1614 case GOT_IMSG_TREE_ENTRIES:
1615 /* This message should be preceeded by GOT_IMSG_TREE. */
1616 if (*tree == NULL) {
1617 err = got_error(GOT_ERR_PRIVSEP_MSG);
1620 if (datalen <= sizeof(*ientries) ||
1621 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1622 err = got_error(GOT_ERR_PRIVSEP_LEN);
1626 ientries = imsg.data;
1627 if (ientries->nentries > INT_MAX) {
1628 err = got_error_msg(GOT_ERR_NO_SPACE,
1629 "too many tree entries");
1632 te_offset = sizeof(*ientries);
1633 for (i = 0; i < ientries->nentries; i++) {
1634 struct got_imsg_tree_entry ite;
1635 const char *te_name;
1636 uint8_t *buf = imsg.data + te_offset;
1638 if (te_offset >= datalen) {
1639 err = got_error(GOT_ERR_PRIVSEP_LEN);
1643 /* Might not be aligned, size is ~32 bytes. */
1644 memcpy(&ite, buf, sizeof(ite));
1646 if (ite.namelen >= sizeof(te->name)) {
1647 err = got_error(GOT_ERR_PRIVSEP_LEN);
1650 if (te_offset + sizeof(ite) + ite.namelen >
1652 err = got_error(GOT_ERR_PRIVSEP_LEN);
1655 if (nentries >= (*tree)->nentries) {
1656 err = got_error(GOT_ERR_PRIVSEP_LEN);
1659 te = &(*tree)->entries[nentries];
1660 te_name = buf + sizeof(ite);
1661 memcpy(te->name, te_name, ite.namelen);
1662 te->name[ite.namelen] = '\0';
1663 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1664 te->mode = ite.mode;
1668 te_offset += sizeof(ite) + ite.namelen;
1672 err = got_error(GOT_ERR_PRIVSEP_MSG);
1681 if (*tree && (*tree)->nentries != nentries) {
1683 err = got_error(GOT_ERR_PRIVSEP_LEN);
1684 got_object_tree_close(*tree);
1691 const struct got_error *
1692 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1693 const uint8_t *data)
1695 struct got_imsg_blob iblob;
1698 iblob.hdrlen = hdrlen;
1703 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1704 return got_error(GOT_ERR_NO_SPACE);
1706 buf = malloc(sizeof(iblob) + size);
1708 return got_error_from_errno("malloc");
1710 memcpy(buf, &iblob, sizeof(iblob));
1711 memcpy(buf + sizeof(iblob), data, size);
1712 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1713 sizeof(iblob) + size) == -1) {
1715 return got_error_from_errno("imsg_compose BLOB");
1719 /* Data has already been written to file descriptor. */
1720 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1721 sizeof(iblob)) == -1)
1722 return got_error_from_errno("imsg_compose BLOB");
1726 return flush_imsg(ibuf);
1729 const struct got_error *
1730 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1731 struct imsgbuf *ibuf)
1733 const struct got_error *err = NULL;
1735 struct got_imsg_blob *iblob;
1740 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1744 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1746 switch (imsg.hdr.type) {
1748 if (datalen < sizeof(*iblob)) {
1749 err = got_error(GOT_ERR_PRIVSEP_LEN);
1753 *size = iblob->size;
1754 *hdrlen = iblob->hdrlen;
1756 if (datalen == sizeof(*iblob)) {
1757 /* Data has been written to file descriptor. */
1761 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1762 err = got_error(GOT_ERR_PRIVSEP_LEN);
1766 *outbuf = malloc(*size);
1767 if (*outbuf == NULL) {
1768 err = got_error_from_errno("malloc");
1771 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1774 err = got_error(GOT_ERR_PRIVSEP_MSG);
1783 static const struct got_error *
1784 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1786 const struct got_error *err = NULL;
1787 size_t offset, remain;
1790 remain = tagmsg_len;
1791 while (remain > 0) {
1792 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1794 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1795 tag->tagmsg + offset, n) == -1) {
1796 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1800 err = flush_imsg(ibuf);
1811 const struct got_error *
1812 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1814 const struct got_error *err = NULL;
1815 struct got_imsg_tag_object *itag;
1818 size_t tag_len = strlen(tag->tag);
1819 size_t tagger_len = strlen(tag->tagger);
1820 size_t tagmsg_len = strlen(tag->tagmsg);
1822 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1824 buf = malloc(total);
1826 return got_error_from_errno("malloc");
1828 itag = (struct got_imsg_tag_object *)buf;
1829 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1830 itag->obj_type = tag->obj_type;
1831 itag->tag_len = tag_len;
1832 itag->tagger_len = tagger_len;
1833 itag->tagger_time = tag->tagger_time;
1834 itag->tagger_gmtoff = tag->tagger_gmtoff;
1835 itag->tagmsg_len = tagmsg_len;
1837 len = sizeof(*itag);
1838 memcpy(buf + len, tag->tag, tag_len);
1840 memcpy(buf + len, tag->tagger, tagger_len);
1843 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1844 err = got_error_from_errno("imsg_compose TAG");
1848 if (tagmsg_len == 0 ||
1849 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1850 err = flush_imsg(ibuf);
1854 err = send_tagmsg(ibuf, tag, tagmsg_len);
1860 const struct got_error *
1861 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1863 const struct got_error *err = NULL;
1865 struct got_imsg_tag_object *itag;
1866 size_t len, datalen;
1867 const size_t min_datalen =
1868 MIN(sizeof(struct got_imsg_error),
1869 sizeof(struct got_imsg_tag_object));
1873 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1877 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1880 switch (imsg.hdr.type) {
1882 if (datalen < sizeof(*itag)) {
1883 err = got_error(GOT_ERR_PRIVSEP_LEN);
1887 if (datalen != sizeof(*itag) + itag->tag_len +
1889 err = got_error(GOT_ERR_PRIVSEP_LEN);
1892 len += sizeof(*itag);
1894 *tag = calloc(1, sizeof(**tag));
1896 err = got_error_from_errno("calloc");
1900 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1902 if (itag->tag_len == 0) {
1903 (*tag)->tag = strdup("");
1904 if ((*tag)->tag == NULL) {
1905 err = got_error_from_errno("strdup");
1909 (*tag)->tag = malloc(itag->tag_len + 1);
1910 if ((*tag)->tag == NULL) {
1911 err = got_error_from_errno("malloc");
1914 memcpy((*tag)->tag, imsg.data + len,
1916 (*tag)->tag[itag->tag_len] = '\0';
1918 len += itag->tag_len;
1920 (*tag)->obj_type = itag->obj_type;
1921 (*tag)->tagger_time = itag->tagger_time;
1922 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1924 if (itag->tagger_len == 0) {
1925 (*tag)->tagger = strdup("");
1926 if ((*tag)->tagger == NULL) {
1927 err = got_error_from_errno("strdup");
1931 (*tag)->tagger = malloc(itag->tagger_len + 1);
1932 if ((*tag)->tagger == NULL) {
1933 err = got_error_from_errno("malloc");
1936 memcpy((*tag)->tagger, imsg.data + len,
1938 (*tag)->tagger[itag->tagger_len] = '\0';
1940 len += itag->tagger_len;
1942 if (itag->tagmsg_len == 0) {
1943 (*tag)->tagmsg = strdup("");
1944 if ((*tag)->tagmsg == NULL) {
1945 err = got_error_from_errno("strdup");
1949 size_t offset = 0, remain = itag->tagmsg_len;
1951 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1952 if ((*tag)->tagmsg == NULL) {
1953 err = got_error_from_errno("malloc");
1956 while (remain > 0) {
1957 struct imsg imsg_log;
1958 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1961 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1965 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1966 return got_error(GOT_ERR_PRIVSEP_MSG);
1968 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1970 imsg_free(&imsg_log);
1974 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1979 err = got_error(GOT_ERR_PRIVSEP_MSG);
1988 const struct got_error *
1989 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1990 struct got_packidx *packidx)
1992 const struct got_error *err = NULL;
1993 struct got_imsg_packidx ipackidx;
1994 struct got_imsg_pack ipack;
1997 ipackidx.len = packidx->len;
1998 ipackidx.packfile_size = pack->filesize;
1999 fd = dup(packidx->fd);
2001 return got_error_from_errno("dup");
2003 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
2004 sizeof(ipackidx)) == -1) {
2005 err = got_error_from_errno("imsg_compose PACKIDX");
2010 if (strlcpy(ipack.path_packfile, pack->path_packfile,
2011 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
2012 return got_error(GOT_ERR_NO_SPACE);
2013 ipack.filesize = pack->filesize;
2017 return got_error_from_errno("dup");
2019 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2021 err = got_error_from_errno("imsg_compose PACK");
2026 return flush_imsg(ibuf);
2029 const struct got_error *
2030 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2031 struct got_object_id *id)
2033 struct got_imsg_packed_object iobj;
2036 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2038 if (imsg_compose(ibuf, GOT_IMSG_PACKED_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_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2048 struct got_object_id *id)
2050 struct got_imsg_packed_object iobj;
2053 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2055 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2056 &iobj, sizeof(iobj)) == -1)
2057 return got_error_from_errno("imsg_compose "
2058 "PACKED_OBJECT_REQUEST");
2060 return flush_imsg(ibuf);
2063 const struct got_error *
2064 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2066 const struct got_error *err = NULL;
2068 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2070 err = got_error_from_errno("imsg_compose "
2071 "GITCONFIG_PARSE_REQUEST");
2076 return flush_imsg(ibuf);
2079 const struct got_error *
2080 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2082 if (imsg_compose(ibuf,
2083 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2085 return got_error_from_errno("imsg_compose "
2086 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2088 return flush_imsg(ibuf);
2091 const struct got_error *
2092 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2094 if (imsg_compose(ibuf,
2095 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2097 return got_error_from_errno("imsg_compose "
2098 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2100 return flush_imsg(ibuf);
2104 const struct got_error *
2105 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2107 if (imsg_compose(ibuf,
2108 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2109 return got_error_from_errno("imsg_compose "
2110 "GITCONFIG_AUTHOR_NAME_REQUEST");
2112 return flush_imsg(ibuf);
2115 const struct got_error *
2116 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2118 if (imsg_compose(ibuf,
2119 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2120 return got_error_from_errno("imsg_compose "
2121 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2123 return flush_imsg(ibuf);
2126 const struct got_error *
2127 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2129 if (imsg_compose(ibuf,
2130 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2131 return got_error_from_errno("imsg_compose "
2132 "GITCONFIG_REMOTE_REQUEST");
2134 return flush_imsg(ibuf);
2137 const struct got_error *
2138 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2140 if (imsg_compose(ibuf,
2141 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2142 return got_error_from_errno("imsg_compose "
2143 "GITCONFIG_OWNER_REQUEST");
2145 return flush_imsg(ibuf);
2148 const struct got_error *
2149 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2151 const struct got_error *err = NULL;
2154 const size_t min_datalen = 0;
2158 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2161 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2163 switch (imsg.hdr.type) {
2164 case GOT_IMSG_GITCONFIG_STR_VAL:
2167 /* datalen does not include terminating \0 */
2168 *str = malloc(datalen + 1);
2170 err = got_error_from_errno("malloc");
2173 memcpy(*str, imsg.data, datalen);
2174 (*str)[datalen] = '\0';
2177 err = got_error(GOT_ERR_PRIVSEP_MSG);
2185 const struct got_error *
2186 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2188 const struct got_error *err = NULL;
2191 const size_t min_datalen =
2192 MIN(sizeof(struct got_imsg_error), sizeof(int));
2196 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2199 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2201 switch (imsg.hdr.type) {
2202 case GOT_IMSG_GITCONFIG_INT_VAL:
2203 if (datalen != sizeof(*val)) {
2204 err = got_error(GOT_ERR_PRIVSEP_LEN);
2207 memcpy(val, imsg.data, sizeof(*val));
2210 err = got_error(GOT_ERR_PRIVSEP_MSG);
2219 free_remote_data(struct got_remote_repo *remote)
2224 free(remote->fetch_url);
2225 free(remote->send_url);
2226 for (i = 0; i < remote->nfetch_branches; i++)
2227 free(remote->fetch_branches[i]);
2228 free(remote->fetch_branches);
2229 for (i = 0; i < remote->nsend_branches; i++)
2230 free(remote->send_branches[i]);
2231 free(remote->send_branches);
2232 for (i = 0; i < remote->nfetch_refs; i++)
2233 free(remote->fetch_refs[i]);
2234 free(remote->fetch_refs);
2237 const struct got_error *
2238 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2239 int *nremotes, struct imsgbuf *ibuf)
2241 const struct got_error *err = NULL;
2244 struct got_imsg_remotes iremotes;
2245 struct got_imsg_remote iremote;
2249 iremotes.nremotes = 0;
2251 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2254 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2256 switch (imsg.hdr.type) {
2257 case GOT_IMSG_GITCONFIG_REMOTES:
2258 if (datalen != sizeof(iremotes)) {
2259 err = got_error(GOT_ERR_PRIVSEP_LEN);
2262 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2263 if (iremotes.nremotes == 0) {
2270 return got_error(GOT_ERR_PRIVSEP_MSG);
2275 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2276 if (*remotes == NULL)
2277 return got_error_from_errno("recallocarray");
2279 while (*nremotes < iremotes.nremotes) {
2280 struct got_remote_repo *remote;
2282 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2285 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2287 switch (imsg.hdr.type) {
2288 case GOT_IMSG_GITCONFIG_REMOTE:
2289 remote = &(*remotes)[*nremotes];
2290 memset(remote, 0, sizeof(*remote));
2291 if (datalen < sizeof(iremote)) {
2292 err = got_error(GOT_ERR_PRIVSEP_LEN);
2295 memcpy(&iremote, imsg.data, sizeof(iremote));
2296 if (iremote.name_len == 0 ||
2297 iremote.fetch_url_len == 0 ||
2298 iremote.send_url_len == 0 ||
2299 (sizeof(iremote) + iremote.name_len +
2300 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2301 err = got_error(GOT_ERR_PRIVSEP_LEN);
2304 remote->name = strndup(imsg.data + sizeof(iremote),
2306 if (remote->name == NULL) {
2307 err = got_error_from_errno("strndup");
2310 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2311 iremote.name_len, iremote.fetch_url_len);
2312 if (remote->fetch_url == NULL) {
2313 err = got_error_from_errno("strndup");
2314 free_remote_data(remote);
2317 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2318 iremote.name_len + iremote.fetch_url_len,
2319 iremote.send_url_len);
2320 if (remote->send_url == NULL) {
2321 err = got_error_from_errno("strndup");
2322 free_remote_data(remote);
2325 remote->mirror_references = iremote.mirror_references;
2326 remote->fetch_all_branches = iremote.fetch_all_branches;
2327 remote->nfetch_branches = 0;
2328 remote->fetch_branches = NULL;
2329 remote->nsend_branches = 0;
2330 remote->send_branches = NULL;
2331 remote->nfetch_refs = 0;
2332 remote->fetch_refs = NULL;
2336 err = got_error(GOT_ERR_PRIVSEP_MSG);
2347 for (i = 0; i < *nremotes; i++)
2348 free_remote_data(&(*remotes)[i]);
2356 const struct got_error *
2357 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2359 const struct got_error *err = NULL;
2361 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2363 err = got_error_from_errno("imsg_compose "
2364 "GOTCONFIG_PARSE_REQUEST");
2369 return flush_imsg(ibuf);
2372 const struct got_error *
2373 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2375 if (imsg_compose(ibuf,
2376 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2377 return got_error_from_errno("imsg_compose "
2378 "GOTCONFIG_AUTHOR_REQUEST");
2380 return flush_imsg(ibuf);
2383 const struct got_error *
2384 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2386 if (imsg_compose(ibuf,
2387 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2388 return got_error_from_errno("imsg_compose "
2389 "GOTCONFIG_REMOTE_REQUEST");
2391 return flush_imsg(ibuf);
2394 const struct got_error *
2395 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2397 const struct got_error *err = NULL;
2400 const size_t min_datalen = 0;
2404 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2407 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2409 switch (imsg.hdr.type) {
2410 case GOT_IMSG_ERROR:
2411 if (datalen < sizeof(struct got_imsg_error)) {
2412 err = got_error(GOT_ERR_PRIVSEP_LEN);
2415 err = recv_imsg_error(&imsg, datalen);
2417 case GOT_IMSG_GOTCONFIG_STR_VAL:
2420 /* datalen does not include terminating \0 */
2421 *str = malloc(datalen + 1);
2423 err = got_error_from_errno("malloc");
2426 memcpy(*str, imsg.data, datalen);
2427 (*str)[datalen] = '\0';
2430 err = got_error(GOT_ERR_PRIVSEP_MSG);
2438 const struct got_error *
2439 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2440 int *nremotes, struct imsgbuf *ibuf)
2442 const struct got_error *err = NULL;
2445 struct got_imsg_remotes iremotes;
2446 struct got_imsg_remote iremote;
2447 const size_t min_datalen =
2448 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2452 iremotes.nremotes = 0;
2454 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2457 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2459 switch (imsg.hdr.type) {
2460 case GOT_IMSG_ERROR:
2461 if (datalen < sizeof(struct got_imsg_error)) {
2462 err = got_error(GOT_ERR_PRIVSEP_LEN);
2465 err = recv_imsg_error(&imsg, datalen);
2467 case GOT_IMSG_GOTCONFIG_REMOTES:
2468 if (datalen != sizeof(iremotes)) {
2469 err = got_error(GOT_ERR_PRIVSEP_LEN);
2472 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2473 if (iremotes.nremotes == 0) {
2480 return got_error(GOT_ERR_PRIVSEP_MSG);
2485 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2486 if (*remotes == NULL)
2487 return got_error_from_errno("recallocarray");
2489 while (*nremotes < iremotes.nremotes) {
2490 struct got_remote_repo *remote;
2491 const size_t min_datalen =
2492 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2495 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2498 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2500 switch (imsg.hdr.type) {
2501 case GOT_IMSG_ERROR:
2502 if (datalen < sizeof(struct got_imsg_error)) {
2503 err = got_error(GOT_ERR_PRIVSEP_LEN);
2506 err = recv_imsg_error(&imsg, datalen);
2508 case GOT_IMSG_GOTCONFIG_REMOTE:
2509 remote = &(*remotes)[*nremotes];
2510 memset(remote, 0, sizeof(*remote));
2511 if (datalen < sizeof(iremote)) {
2512 err = got_error(GOT_ERR_PRIVSEP_LEN);
2515 memcpy(&iremote, imsg.data, sizeof(iremote));
2516 if (iremote.name_len == 0 ||
2517 (iremote.fetch_url_len == 0 &&
2518 iremote.send_url_len == 0) ||
2519 (sizeof(iremote) + iremote.name_len +
2520 iremote.fetch_url_len + iremote.send_url_len) >
2522 err = got_error(GOT_ERR_PRIVSEP_LEN);
2525 remote->name = strndup(imsg.data + sizeof(iremote),
2527 if (remote->name == NULL) {
2528 err = got_error_from_errno("strndup");
2531 remote->fetch_url = strndup(imsg.data +
2532 sizeof(iremote) + iremote.name_len,
2533 iremote.fetch_url_len);
2534 if (remote->fetch_url == NULL) {
2535 err = got_error_from_errno("strndup");
2536 free_remote_data(remote);
2539 remote->send_url = strndup(imsg.data +
2540 sizeof(iremote) + iremote.name_len +
2541 iremote.fetch_url_len, iremote.send_url_len);
2542 if (remote->send_url == NULL) {
2543 err = got_error_from_errno("strndup");
2544 free_remote_data(remote);
2547 remote->mirror_references = iremote.mirror_references;
2548 remote->fetch_all_branches = iremote.fetch_all_branches;
2549 if (iremote.nfetch_branches > 0) {
2550 remote->fetch_branches = recallocarray(NULL, 0,
2551 iremote.nfetch_branches, sizeof(char *));
2552 if (remote->fetch_branches == NULL) {
2553 err = got_error_from_errno("calloc");
2554 free_remote_data(remote);
2558 remote->nfetch_branches = 0;
2559 for (i = 0; i < iremote.nfetch_branches; i++) {
2561 err = got_privsep_recv_gotconfig_str(&branch,
2564 free_remote_data(remote);
2567 remote->fetch_branches[i] = branch;
2568 remote->nfetch_branches++;
2570 if (iremote.nsend_branches > 0) {
2571 remote->send_branches = recallocarray(NULL, 0,
2572 iremote.nsend_branches, sizeof(char *));
2573 if (remote->send_branches == NULL) {
2574 err = got_error_from_errno("calloc");
2575 free_remote_data(remote);
2579 remote->nsend_branches = 0;
2580 for (i = 0; i < iremote.nsend_branches; i++) {
2582 err = got_privsep_recv_gotconfig_str(&branch,
2585 free_remote_data(remote);
2588 remote->send_branches[i] = branch;
2589 remote->nsend_branches++;
2591 if (iremote.nfetch_refs > 0) {
2592 remote->fetch_refs = recallocarray(NULL, 0,
2593 iremote.nfetch_refs, sizeof(char *));
2594 if (remote->fetch_refs == NULL) {
2595 err = got_error_from_errno("calloc");
2596 free_remote_data(remote);
2600 remote->nfetch_refs = 0;
2601 for (i = 0; i < iremote.nfetch_refs; i++) {
2603 err = got_privsep_recv_gotconfig_str(&ref,
2606 free_remote_data(remote);
2609 remote->fetch_refs[i] = ref;
2610 remote->nfetch_refs++;
2615 err = got_error(GOT_ERR_PRIVSEP_MSG);
2626 for (i = 0; i < *nremotes; i++)
2627 free_remote_data(&(*remotes)[i]);
2635 const struct got_error *
2636 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2637 struct got_object_id *id, int idx, const char *path)
2640 size_t path_len = strlen(path) + 1;
2642 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2643 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2645 return got_error_from_errno(
2646 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2647 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2648 return got_error_from_errno("imsg_add "
2649 "COMMIT_TRAVERSAL_REQUEST");
2650 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2651 return got_error_from_errno("imsg_add "
2652 "COMMIT_TRAVERSAL_REQUEST");
2653 if (imsg_add(wbuf, path, path_len) == -1)
2654 return got_error_from_errno("imsg_add "
2655 "COMMIT_TRAVERSAL_REQUEST");
2658 imsg_close(ibuf, wbuf);
2660 return flush_imsg(ibuf);
2663 const struct got_error *
2664 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2665 struct got_object_id **changed_commit_id,
2666 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2668 const struct got_error *err = NULL;
2670 struct got_imsg_traversed_commits *icommits;
2674 *changed_commit = NULL;
2675 *changed_commit_id = NULL;
2678 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2682 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2683 switch (imsg.hdr.type) {
2684 case GOT_IMSG_TRAVERSED_COMMITS:
2685 icommits = imsg.data;
2686 if (datalen != sizeof(*icommits) +
2687 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2688 err = got_error(GOT_ERR_PRIVSEP_LEN);
2691 for (i = 0; i < icommits->ncommits; i++) {
2692 struct got_object_qid *qid;
2693 uint8_t *sha1 = (uint8_t *)imsg.data +
2694 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2695 err = got_object_qid_alloc_partial(&qid);
2698 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2699 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2701 /* The last commit may contain a change. */
2702 if (i == icommits->ncommits - 1) {
2703 *changed_commit_id =
2704 got_object_id_dup(&qid->id);
2705 if (*changed_commit_id == NULL) {
2706 err = got_error_from_errno(
2707 "got_object_id_dup");
2713 case GOT_IMSG_COMMIT:
2714 if (*changed_commit_id == NULL) {
2715 err = got_error(GOT_ERR_PRIVSEP_MSG);
2718 err = get_commit_from_imsg(changed_commit, &imsg,
2721 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2725 err = got_error(GOT_ERR_PRIVSEP_MSG);
2735 got_object_id_queue_free(commit_ids);
2739 const struct got_error *
2740 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2741 struct got_object_id *id)
2743 struct got_imsg_raw_delta_request dreq;
2746 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2748 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2749 &dreq, sizeof(dreq)) == -1)
2750 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2752 return flush_imsg(ibuf);
2755 const struct got_error *
2756 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2758 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2761 const struct got_error *
2762 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2763 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2764 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2766 struct got_imsg_raw_delta idelta;
2769 idelta.base_size = base_size;
2770 idelta.result_size = result_size;
2771 idelta.delta_size = delta_size;
2772 idelta.delta_compressed_size = delta_compressed_size;
2773 idelta.delta_offset = delta_offset;
2774 idelta.delta_out_offset = delta_out_offset;
2775 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2777 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2778 &idelta, sizeof(idelta));
2780 return got_error_from_errno("imsg_compose RAW_DELTA");
2782 return flush_imsg(ibuf);
2785 const struct got_error *
2786 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2787 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2788 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
2790 const struct got_error *err = NULL;
2792 struct got_imsg_raw_delta *delta;
2798 *delta_compressed_size = 0;
2800 *delta_out_offset = 0;
2803 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2807 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2809 switch (imsg.hdr.type) {
2810 case GOT_IMSG_RAW_DELTA:
2811 if (datalen != sizeof(*delta)) {
2812 err = got_error(GOT_ERR_PRIVSEP_LEN);
2816 *base_size = delta->base_size;
2817 *result_size = delta->result_size;
2818 *delta_size = delta->delta_size;
2819 *delta_compressed_size = delta->delta_compressed_size;
2820 *delta_offset = delta->delta_offset;
2821 *delta_out_offset = delta->delta_out_offset;
2822 *base_id = calloc(1, sizeof(**base_id));
2823 if (*base_id == NULL) {
2824 err = got_error_from_errno("malloc");
2827 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
2830 err = got_error(GOT_ERR_PRIVSEP_MSG);
2843 const struct got_error *
2844 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
2845 struct got_object_id **ids, size_t nids)
2847 const struct got_error *err = NULL;
2848 struct got_imsg_object_idlist idlist;
2852 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
2853 return got_error(GOT_ERR_NO_SPACE);
2855 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
2856 sizeof(idlist) + nids * sizeof(**ids));
2858 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
2863 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
2864 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2866 for (i = 0; i < nids; i++) {
2867 struct got_object_id *id = ids[i];
2868 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
2869 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2873 imsg_close(ibuf, wbuf);
2875 return flush_imsg(ibuf);
2878 const struct got_error *
2879 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
2881 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
2883 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
2885 return flush_imsg(ibuf);
2888 const struct got_error *
2889 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
2890 size_t *nids, struct imsgbuf *ibuf)
2892 const struct got_error *err = NULL;
2894 struct got_imsg_object_idlist *idlist;
2901 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2905 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2906 switch (imsg.hdr.type) {
2907 case GOT_IMSG_OBJ_ID_LIST:
2908 if (datalen < sizeof(*idlist)) {
2909 err = got_error(GOT_ERR_PRIVSEP_LEN);
2913 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
2914 err = got_error(GOT_ERR_PRIVSEP_LEN);
2917 *nids = idlist->nids;
2918 *ids = calloc(*nids, sizeof(**ids));
2920 err = got_error_from_errno("calloc");
2923 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
2924 *nids * sizeof(**ids));
2926 case GOT_IMSG_OBJ_ID_LIST_DONE:
2930 err = got_error(GOT_ERR_PRIVSEP_MSG);
2939 const struct got_error *
2940 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
2942 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
2944 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
2946 return flush_imsg(ibuf);
2949 const struct got_error *
2950 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
2951 struct got_imsg_reused_delta *deltas, size_t ndeltas)
2953 const struct got_error *err = NULL;
2955 struct got_imsg_reused_deltas ideltas;
2958 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
2959 return got_error(GOT_ERR_NO_SPACE);
2961 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
2962 sizeof(ideltas) + ndeltas * sizeof(*deltas));
2964 err = got_error_from_errno("imsg_create REUSED_DELTAS");
2968 ideltas.ndeltas = ndeltas;
2969 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
2970 return got_error_from_errno("imsg_add REUSED_DELTAS");
2972 for (i = 0; i < ndeltas; i++) {
2973 struct got_imsg_reused_delta *delta = &deltas[i];
2974 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
2975 return got_error_from_errno("imsg_add REUSED_DELTAS");
2979 imsg_close(ibuf, wbuf);
2981 return flush_imsg(ibuf);
2984 const struct got_error *
2985 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
2987 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
2989 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
2991 return flush_imsg(ibuf);
2994 const struct got_error *
2995 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
2996 size_t *ndeltas, struct imsgbuf *ibuf)
2998 const struct got_error *err = NULL;
3000 struct got_imsg_reused_deltas *ideltas;
3006 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3010 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3011 switch (imsg.hdr.type) {
3012 case GOT_IMSG_REUSED_DELTAS:
3013 if (datalen < sizeof(*ideltas)) {
3014 err = got_error(GOT_ERR_PRIVSEP_LEN);
3017 ideltas = imsg.data;
3018 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3019 err = got_error(GOT_ERR_PRIVSEP_LEN);
3022 *ndeltas = ideltas->ndeltas;
3023 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3024 *ndeltas * sizeof(*deltas));
3026 case GOT_IMSG_DELTA_REUSE_DONE:
3030 err = got_error(GOT_ERR_PRIVSEP_MSG);
3039 const struct got_error *
3040 got_privsep_unveil_exec_helpers(void)
3042 const char *helpers[] = {
3043 GOT_PATH_PROG_READ_PACK,
3044 GOT_PATH_PROG_READ_OBJECT,
3045 GOT_PATH_PROG_READ_COMMIT,
3046 GOT_PATH_PROG_READ_TREE,
3047 GOT_PATH_PROG_READ_BLOB,
3048 GOT_PATH_PROG_READ_TAG,
3049 GOT_PATH_PROG_READ_GITCONFIG,
3050 GOT_PATH_PROG_READ_GOTCONFIG,
3051 GOT_PATH_PROG_READ_PATCH,
3052 GOT_PATH_PROG_FETCH_PACK,
3053 GOT_PATH_PROG_INDEX_PACK,
3054 GOT_PATH_PROG_SEND_PACK,
3058 for (i = 0; i < nitems(helpers); i++) {
3059 if (unveil(helpers[i], "x") == 0)
3061 return got_error_from_errno2("unveil", helpers[i]);
3068 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3070 if (close(imsg_fds[0]) == -1) {
3071 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3075 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3076 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3080 closefrom(GOT_IMSG_FD_CHILD + 1);
3082 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3083 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,