Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/wait.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdint.h>
31 #include <poll.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <time.h>
38 #include "got_object.h"
39 #include "got_error.h"
40 #include "got_path.h"
41 #include "got_repository.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
51 #ifndef MIN
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #endif
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static const struct got_error *
60 poll_fd(int fd, int events, int timeout)
61 {
62 struct pollfd pfd[1];
63 struct timespec ts;
64 sigset_t sigset;
65 int n;
67 pfd[0].fd = fd;
68 pfd[0].events = events;
70 ts.tv_sec = timeout;
71 ts.tv_nsec = 0;
73 if (sigemptyset(&sigset) == -1)
74 return got_error_from_errno("sigemptyset");
75 if (sigaddset(&sigset, SIGWINCH) == -1)
76 return got_error_from_errno("sigaddset");
78 n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
79 if (n == -1)
80 return got_error_from_errno("ppoll");
81 if (n == 0)
82 return got_error(GOT_ERR_TIMEOUT);
83 if (pfd[0].revents & (POLLERR | POLLNVAL))
84 return got_error_from_errno("poll error");
85 if (pfd[0].revents & (events | POLLHUP))
86 return NULL;
88 return got_error(GOT_ERR_INTERRUPT);
89 }
91 static const struct got_error *
92 read_imsg(struct imsgbuf *ibuf)
93 {
94 const struct got_error *err;
95 size_t n;
97 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
98 if (err)
99 return err;
101 n = imsg_read(ibuf);
102 if (n == -1) {
103 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
104 return got_error(GOT_ERR_PRIVSEP_NO_FD);
105 return got_error(GOT_ERR_PRIVSEP_READ);
107 if (n == 0)
108 return got_error(GOT_ERR_PRIVSEP_PIPE);
110 return NULL;
113 const struct got_error *
114 got_privsep_wait_for_child(pid_t pid)
116 int child_status;
118 if (waitpid(pid, &child_status, 0) == -1)
119 return got_error_from_errno("waitpid");
121 if (!WIFEXITED(child_status))
122 return got_error(GOT_ERR_PRIVSEP_DIED);
124 if (WEXITSTATUS(child_status) != 0)
125 return got_error(GOT_ERR_PRIVSEP_EXIT);
127 return NULL;
130 static const struct got_error *
131 recv_imsg_error(struct imsg *imsg, size_t datalen)
133 struct got_imsg_error *ierr;
135 if (datalen != sizeof(*ierr))
136 return got_error(GOT_ERR_PRIVSEP_LEN);
138 ierr = imsg->data;
139 if (ierr->code == GOT_ERR_ERRNO) {
140 static struct got_error serr;
141 serr.code = GOT_ERR_ERRNO;
142 serr.msg = strerror(ierr->errno_code);
143 return &serr;
146 return got_error(ierr->code);
149 const struct got_error *
150 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
151 size_t min_datalen)
153 const struct got_error *err;
154 ssize_t n;
156 n = imsg_get(ibuf, imsg);
157 if (n == -1)
158 return got_error_from_errno("imsg_get");
160 while (n == 0) {
161 err = read_imsg(ibuf);
162 if (err)
163 return err;
164 n = imsg_get(ibuf, imsg);
165 if (n == -1)
166 return got_error_from_errno("imsg_get");
169 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
170 return got_error(GOT_ERR_PRIVSEP_LEN);
172 if (imsg->hdr.type == GOT_IMSG_ERROR) {
173 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
174 return recv_imsg_error(imsg, datalen);
177 return NULL;
180 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
181 void
182 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
184 const struct got_error *poll_err;
185 struct got_imsg_error ierr;
186 int ret;
188 ierr.code = err->code;
189 if (err->code == GOT_ERR_ERRNO)
190 ierr.errno_code = errno;
191 else
192 ierr.errno_code = 0;
193 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
194 if (ret == -1) {
195 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
196 getprogname(), err->code, err->msg, strerror(errno));
197 return;
200 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
201 if (poll_err) {
202 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
203 getprogname(), err->code, err->msg, poll_err->msg);
204 return;
207 ret = imsg_flush(ibuf);
208 if (ret == -1) {
209 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
210 getprogname(), err->code, err->msg, strerror(errno));
211 return;
215 static const struct got_error *
216 flush_imsg(struct imsgbuf *ibuf)
218 const struct got_error *err;
220 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
221 if (err)
222 return err;
224 if (imsg_flush(ibuf) == -1)
225 return got_error_from_errno("imsg_flush");
227 return NULL;
230 const struct got_error *
231 got_privsep_flush_imsg(struct imsgbuf *ibuf)
233 return flush_imsg(ibuf);
236 const struct got_error *
237 got_privsep_send_stop(int fd)
239 const struct got_error *err = NULL;
240 struct imsgbuf ibuf;
242 imsg_init(&ibuf, fd);
244 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
245 return got_error_from_errno("imsg_compose STOP");
247 err = flush_imsg(&ibuf);
248 imsg_clear(&ibuf);
249 return err;
252 const struct got_error *
253 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
254 struct got_object_id *id)
256 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
257 id, sizeof(*id)) == -1)
258 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
260 return flush_imsg(ibuf);
263 const struct got_error *
264 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
265 struct got_object_id *id)
267 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
268 id, sizeof(*id)) == -1)
269 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
271 return flush_imsg(ibuf);
274 const struct got_error *
275 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
277 const struct got_error *err = NULL;
279 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
280 == -1) {
281 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
282 close(outfd);
283 return err;
286 return flush_imsg(ibuf);
289 const struct got_error *
290 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
291 uint8_t *data)
293 const struct got_error *err = NULL;
294 struct got_imsg_raw_obj iobj;
295 size_t len = sizeof(iobj);
296 struct ibuf *wbuf;
298 iobj.hdrlen = hdrlen;
299 iobj.size = size;
301 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
302 len += (size_t)size + hdrlen;
304 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
305 if (wbuf == NULL) {
306 err = got_error_from_errno("imsg_create RAW_OBJECT");
307 return err;
310 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1) {
311 err = got_error_from_errno("imsg_add RAW_OBJECT");
312 ibuf_free(wbuf);
313 return err;
316 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
317 if (imsg_add(wbuf, data, size + hdrlen) == -1) {
318 err = got_error_from_errno("imsg_add RAW_OBJECT");
319 ibuf_free(wbuf);
320 return err;
324 wbuf->fd = -1;
325 imsg_close(ibuf, wbuf);
327 return flush_imsg(ibuf);
330 const struct got_error *
331 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
332 struct imsgbuf *ibuf)
334 const struct got_error *err = NULL;
335 struct imsg imsg;
336 struct got_imsg_raw_obj *iobj;
337 size_t datalen;
339 *outbuf = NULL;
341 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
342 if (err)
343 return err;
345 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
347 switch (imsg.hdr.type) {
348 case GOT_IMSG_RAW_OBJECT:
349 if (datalen < sizeof(*iobj)) {
350 err = got_error(GOT_ERR_PRIVSEP_LEN);
351 break;
353 iobj = imsg.data;
354 *size = iobj->size;
355 *hdrlen = iobj->hdrlen;
357 if (datalen == sizeof(*iobj)) {
358 /* Data has been written to file descriptor. */
359 break;
362 if (*size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
363 err = got_error(GOT_ERR_PRIVSEP_LEN);
364 break;
367 *outbuf = malloc(*size + *hdrlen);
368 if (*outbuf == NULL) {
369 err = got_error_from_errno("malloc");
370 break;
372 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
373 break;
374 default:
375 err = got_error(GOT_ERR_PRIVSEP_MSG);
376 break;
379 imsg_free(&imsg);
381 return err;
384 const struct got_error *
385 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
386 struct got_object_id *id, int pack_idx)
388 const struct got_error *err = NULL;
389 struct got_imsg_packed_object iobj;
390 void *data;
391 size_t len;
393 if (pack_idx != -1) { /* commit is packed */
394 iobj.idx = pack_idx;
395 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
396 data = &iobj;
397 len = sizeof(iobj);
398 } else {
399 data = id;
400 len = sizeof(*id);
403 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
404 == -1) {
405 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
406 close(fd);
407 return err;
410 return flush_imsg(ibuf);
413 const struct got_error *
414 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
415 struct got_object_id *id, int pack_idx)
417 const struct got_error *err = NULL;
418 struct ibuf *wbuf;
419 size_t len;
421 if (pack_idx != -1)
422 len = sizeof(struct got_imsg_packed_object);
423 else
424 len = sizeof(*id);
426 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
427 if (wbuf == NULL)
428 return got_error_from_errno("imsg_create TREE_REQUEST");
430 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
431 err = got_error_from_errno("imsg_add TREE_ENTRY");
432 ibuf_free(wbuf);
433 return err;
436 if (pack_idx != -1) { /* tree is packed */
437 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
438 err = got_error_from_errno("imsg_add TREE_ENTRY");
439 ibuf_free(wbuf);
440 return err;
444 wbuf->fd = fd;
445 imsg_close(ibuf, wbuf);
447 return flush_imsg(ibuf);
450 const struct got_error *
451 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
452 struct got_object_id *id, int pack_idx)
454 struct got_imsg_packed_object iobj;
455 void *data;
456 size_t len;
458 if (pack_idx != -1) { /* tag is packed */
459 iobj.idx = pack_idx;
460 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
461 data = &iobj;
462 len = sizeof(iobj);
463 } else {
464 data = id;
465 len = sizeof(*id);
468 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
469 == -1)
470 return got_error_from_errno("imsg_compose TAG_REQUEST");
472 return flush_imsg(ibuf);
475 const struct got_error *
476 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
477 struct got_object_id *id, int pack_idx)
479 const struct got_error *err = NULL;
480 struct got_imsg_packed_object iobj;
481 void *data;
482 size_t len;
484 if (pack_idx != -1) { /* blob is packed */
485 iobj.idx = pack_idx;
486 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
487 data = &iobj;
488 len = sizeof(iobj);
489 } else {
490 data = id;
491 len = sizeof(*id);
494 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
495 == -1) {
496 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
497 close(infd);
498 return err;
501 return flush_imsg(ibuf);
504 const struct got_error *
505 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
507 const struct got_error *err = NULL;
509 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
510 == -1) {
511 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
512 close(outfd);
513 return err;
516 return flush_imsg(ibuf);
519 static const struct got_error *
520 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
522 const struct got_error *err = NULL;
524 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
525 err = got_error_from_errno("imsg_compose TMPFD");
526 close(fd);
527 return err;
530 return flush_imsg(ibuf);
533 const struct got_error *
534 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
536 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
539 const struct got_error *
540 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
542 struct got_imsg_object iobj;
544 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
545 iobj.type = obj->type;
546 iobj.flags = obj->flags;
547 iobj.hdrlen = obj->hdrlen;
548 iobj.size = obj->size;
549 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
550 iobj.pack_offset = obj->pack_offset;
551 iobj.pack_idx = obj->pack_idx;
554 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
555 == -1)
556 return got_error_from_errno("imsg_compose OBJECT");
558 return flush_imsg(ibuf);
561 const struct got_error *
562 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
563 struct got_pathlist_head *have_refs, int fetch_all_branches,
564 struct got_pathlist_head *wanted_branches,
565 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
567 const struct got_error *err = NULL;
568 struct ibuf *wbuf;
569 size_t len;
570 struct got_pathlist_entry *pe;
571 struct got_imsg_fetch_request fetchreq;
573 memset(&fetchreq, 0, sizeof(fetchreq));
574 fetchreq.fetch_all_branches = fetch_all_branches;
575 fetchreq.list_refs_only = list_refs_only;
576 fetchreq.verbosity = verbosity;
577 TAILQ_FOREACH(pe, have_refs, entry)
578 fetchreq.n_have_refs++;
579 TAILQ_FOREACH(pe, wanted_branches, entry)
580 fetchreq.n_wanted_branches++;
581 TAILQ_FOREACH(pe, wanted_refs, entry)
582 fetchreq.n_wanted_refs++;
583 len = sizeof(struct got_imsg_fetch_request);
584 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
585 close(fd);
586 return got_error(GOT_ERR_NO_SPACE);
589 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
590 &fetchreq, sizeof(fetchreq)) == -1)
591 return got_error_from_errno(
592 "imsg_compose FETCH_SERVER_PROGRESS");
594 err = flush_imsg(ibuf);
595 if (err) {
596 close(fd);
597 return err;
599 fd = -1;
601 TAILQ_FOREACH(pe, have_refs, entry) {
602 const char *name = pe->path;
603 size_t name_len = pe->path_len;
604 struct got_object_id *id = pe->data;
606 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
607 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
608 if (wbuf == NULL)
609 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
611 /* Keep in sync with struct got_imsg_fetch_have_ref! */
612 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
613 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
614 ibuf_free(wbuf);
615 return err;
617 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
618 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
619 ibuf_free(wbuf);
620 return err;
622 if (imsg_add(wbuf, name, name_len) == -1) {
623 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
624 ibuf_free(wbuf);
625 return err;
628 wbuf->fd = -1;
629 imsg_close(ibuf, wbuf);
630 err = flush_imsg(ibuf);
631 if (err)
632 return err;
635 TAILQ_FOREACH(pe, wanted_branches, entry) {
636 const char *name = pe->path;
637 size_t name_len = pe->path_len;
639 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
640 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
641 len);
642 if (wbuf == NULL)
643 return got_error_from_errno(
644 "imsg_create FETCH_WANTED_BRANCH");
646 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
647 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
648 err = got_error_from_errno(
649 "imsg_add FETCH_WANTED_BRANCH");
650 ibuf_free(wbuf);
651 return err;
653 if (imsg_add(wbuf, name, name_len) == -1) {
654 err = got_error_from_errno(
655 "imsg_add FETCH_WANTED_BRANCH");
656 ibuf_free(wbuf);
657 return err;
660 wbuf->fd = -1;
661 imsg_close(ibuf, wbuf);
662 err = flush_imsg(ibuf);
663 if (err)
664 return err;
667 TAILQ_FOREACH(pe, wanted_refs, entry) {
668 const char *name = pe->path;
669 size_t name_len = pe->path_len;
671 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
672 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
673 len);
674 if (wbuf == NULL)
675 return got_error_from_errno(
676 "imsg_create FETCH_WANTED_REF");
678 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
679 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
680 err = got_error_from_errno(
681 "imsg_add FETCH_WANTED_REF");
682 ibuf_free(wbuf);
683 return err;
685 if (imsg_add(wbuf, name, name_len) == -1) {
686 err = got_error_from_errno(
687 "imsg_add FETCH_WANTED_REF");
688 ibuf_free(wbuf);
689 return err;
692 wbuf->fd = -1;
693 imsg_close(ibuf, wbuf);
694 err = flush_imsg(ibuf);
695 if (err)
696 return err;
700 return NULL;
704 const struct got_error *
705 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
707 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
710 const struct got_error *
711 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
712 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
713 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
715 const struct got_error *err = NULL;
716 struct imsg imsg;
717 size_t datalen;
718 struct got_imsg_fetch_symrefs *isymrefs = NULL;
719 size_t n, remain;
720 off_t off;
721 int i;
723 *done = 0;
724 *id = NULL;
725 *refname = NULL;
726 *server_progress = NULL;
727 *packfile_size = 0;
728 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
730 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
731 if (err)
732 return err;
734 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
735 switch (imsg.hdr.type) {
736 case GOT_IMSG_ERROR:
737 if (datalen < sizeof(struct got_imsg_error)) {
738 err = got_error(GOT_ERR_PRIVSEP_LEN);
739 break;
741 err = recv_imsg_error(&imsg, datalen);
742 break;
743 case GOT_IMSG_FETCH_SYMREFS:
744 if (datalen < sizeof(*isymrefs)) {
745 err = got_error(GOT_ERR_PRIVSEP_LEN);
746 break;
748 if (isymrefs != NULL) {
749 err = got_error(GOT_ERR_PRIVSEP_MSG);
750 break;
752 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
753 off = sizeof(*isymrefs);
754 remain = datalen - off;
755 for (n = 0; n < isymrefs->nsymrefs; n++) {
756 struct got_imsg_fetch_symref *s;
757 char *name, *target;
758 if (remain < sizeof(struct got_imsg_fetch_symref)) {
759 err = got_error(GOT_ERR_PRIVSEP_LEN);
760 goto done;
762 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
763 off += sizeof(*s);
764 remain -= sizeof(*s);
765 if (remain < s->name_len) {
766 err = got_error(GOT_ERR_PRIVSEP_LEN);
767 goto done;
769 name = strndup(imsg.data + off, s->name_len);
770 if (name == NULL) {
771 err = got_error_from_errno("strndup");
772 goto done;
774 off += s->name_len;
775 remain -= s->name_len;
776 if (remain < s->target_len) {
777 err = got_error(GOT_ERR_PRIVSEP_LEN);
778 free(name);
779 goto done;
781 target = strndup(imsg.data + off, s->target_len);
782 if (target == NULL) {
783 err = got_error_from_errno("strndup");
784 free(name);
785 goto done;
787 off += s->target_len;
788 remain -= s->target_len;
789 err = got_pathlist_append(symrefs, name, target);
790 if (err) {
791 free(name);
792 free(target);
793 goto done;
796 break;
797 case GOT_IMSG_FETCH_REF:
798 if (datalen <= SHA1_DIGEST_LENGTH) {
799 err = got_error(GOT_ERR_PRIVSEP_MSG);
800 break;
802 *id = malloc(sizeof(**id));
803 if (*id == NULL) {
804 err = got_error_from_errno("malloc");
805 break;
807 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
808 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
809 datalen - SHA1_DIGEST_LENGTH);
810 if (*refname == NULL) {
811 err = got_error_from_errno("strndup");
812 break;
814 break;
815 case GOT_IMSG_FETCH_SERVER_PROGRESS:
816 if (datalen == 0) {
817 err = got_error(GOT_ERR_PRIVSEP_LEN);
818 break;
820 *server_progress = strndup(imsg.data, datalen);
821 if (*server_progress == NULL) {
822 err = got_error_from_errno("strndup");
823 break;
825 for (i = 0; i < datalen; i++) {
826 if (!isprint((unsigned char)(*server_progress)[i]) &&
827 !isspace((unsigned char)(*server_progress)[i])) {
828 err = got_error(GOT_ERR_PRIVSEP_MSG);
829 free(*server_progress);
830 *server_progress = NULL;
831 goto done;
834 break;
835 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
836 if (datalen < sizeof(*packfile_size)) {
837 err = got_error(GOT_ERR_PRIVSEP_MSG);
838 break;
840 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
841 break;
842 case GOT_IMSG_FETCH_DONE:
843 if (datalen != SHA1_DIGEST_LENGTH) {
844 err = got_error(GOT_ERR_PRIVSEP_MSG);
845 break;
847 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
848 *done = 1;
849 break;
850 default:
851 err = got_error(GOT_ERR_PRIVSEP_MSG);
852 break;
854 done:
855 if (err) {
856 free(*id);
857 *id = NULL;
858 free(*refname);
859 *refname = NULL;
861 imsg_free(&imsg);
862 return err;
865 const struct got_error *
866 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
867 int fd)
869 const struct got_error *err = NULL;
871 /* Keep in sync with struct got_imsg_index_pack_request */
872 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
873 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
874 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
875 close(fd);
876 return err;
878 return flush_imsg(ibuf);
881 const struct got_error *
882 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
884 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
887 const struct got_error *
888 got_privsep_recv_index_progress(int *done, int *nobj_total,
889 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
890 struct imsgbuf *ibuf)
892 const struct got_error *err = NULL;
893 struct imsg imsg;
894 struct got_imsg_index_pack_progress *iprogress;
895 size_t datalen;
897 *done = 0;
898 *nobj_total = 0;
899 *nobj_indexed = 0;
900 *nobj_resolved = 0;
902 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
903 if (err)
904 return err;
906 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
907 switch (imsg.hdr.type) {
908 case GOT_IMSG_ERROR:
909 if (datalen < sizeof(struct got_imsg_error)) {
910 err = got_error(GOT_ERR_PRIVSEP_LEN);
911 break;
913 err = recv_imsg_error(&imsg, datalen);
914 break;
915 case GOT_IMSG_IDXPACK_PROGRESS:
916 if (datalen < sizeof(*iprogress)) {
917 err = got_error(GOT_ERR_PRIVSEP_LEN);
918 break;
920 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
921 *nobj_total = iprogress->nobj_total;
922 *nobj_indexed = iprogress->nobj_indexed;
923 *nobj_loose = iprogress->nobj_loose;
924 *nobj_resolved = iprogress->nobj_resolved;
925 break;
926 case GOT_IMSG_IDXPACK_DONE:
927 if (datalen != 0) {
928 err = got_error(GOT_ERR_PRIVSEP_LEN);
929 break;
931 *done = 1;
932 break;
933 default:
934 err = got_error(GOT_ERR_PRIVSEP_MSG);
935 break;
938 imsg_free(&imsg);
939 return err;
942 const struct got_error *
943 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
944 struct imsgbuf *ibuf)
946 const struct got_error *err = NULL;
947 struct got_imsg_object *iobj;
948 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
950 if (datalen != sizeof(*iobj))
951 return got_error(GOT_ERR_PRIVSEP_LEN);
952 iobj = imsg->data;
954 *obj = calloc(1, sizeof(**obj));
955 if (*obj == NULL)
956 return got_error_from_errno("calloc");
958 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
959 (*obj)->type = iobj->type;
960 (*obj)->flags = iobj->flags;
961 (*obj)->hdrlen = iobj->hdrlen;
962 (*obj)->size = iobj->size;
963 /* path_packfile is handled by caller */
964 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
965 (*obj)->pack_offset = iobj->pack_offset;
966 (*obj)->pack_idx = iobj->pack_idx;
969 return err;
972 const struct got_error *
973 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
975 const struct got_error *err = NULL;
976 struct imsg imsg;
977 const size_t min_datalen =
978 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
980 *obj = NULL;
982 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
983 if (err)
984 return err;
986 switch (imsg.hdr.type) {
987 case GOT_IMSG_OBJECT:
988 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
989 break;
990 default:
991 err = got_error(GOT_ERR_PRIVSEP_MSG);
992 break;
995 imsg_free(&imsg);
997 return err;
1000 static const struct got_error *
1001 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1002 size_t logmsg_len)
1004 const struct got_error *err = NULL;
1005 size_t offset, remain;
1007 offset = 0;
1008 remain = logmsg_len;
1009 while (remain > 0) {
1010 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1012 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1013 commit->logmsg + offset, n) == -1) {
1014 err = got_error_from_errno("imsg_compose "
1015 "COMMIT_LOGMSG");
1016 break;
1019 err = flush_imsg(ibuf);
1020 if (err)
1021 break;
1023 offset += n;
1024 remain -= n;
1027 return err;
1030 const struct got_error *
1031 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1033 const struct got_error *err = NULL;
1034 struct got_imsg_commit_object *icommit;
1035 uint8_t *buf;
1036 size_t len, total;
1037 struct got_object_qid *qid;
1038 size_t author_len = strlen(commit->author);
1039 size_t committer_len = strlen(commit->committer);
1040 size_t logmsg_len = strlen(commit->logmsg);
1042 total = sizeof(*icommit) + author_len + committer_len +
1043 commit->nparents * SHA1_DIGEST_LENGTH;
1045 buf = malloc(total);
1046 if (buf == NULL)
1047 return got_error_from_errno("malloc");
1049 icommit = (struct got_imsg_commit_object *)buf;
1050 memcpy(icommit->tree_id, commit->tree_id->sha1,
1051 sizeof(icommit->tree_id));
1052 icommit->author_len = author_len;
1053 icommit->author_time = commit->author_time;
1054 icommit->author_gmtoff = commit->author_gmtoff;
1055 icommit->committer_len = committer_len;
1056 icommit->committer_time = commit->committer_time;
1057 icommit->committer_gmtoff = commit->committer_gmtoff;
1058 icommit->logmsg_len = logmsg_len;
1059 icommit->nparents = commit->nparents;
1061 len = sizeof(*icommit);
1062 memcpy(buf + len, commit->author, author_len);
1063 len += author_len;
1064 memcpy(buf + len, commit->committer, committer_len);
1065 len += committer_len;
1066 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1067 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1068 len += SHA1_DIGEST_LENGTH;
1071 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1072 err = got_error_from_errno("imsg_compose COMMIT");
1073 goto done;
1076 if (logmsg_len == 0 ||
1077 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1078 err = flush_imsg(ibuf);
1079 if (err)
1080 goto done;
1082 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1083 done:
1084 free(buf);
1085 return err;
1088 static const struct got_error *
1089 get_commit_from_imsg(struct got_commit_object **commit,
1090 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1092 const struct got_error *err = NULL;
1093 struct got_imsg_commit_object *icommit;
1094 size_t len = 0;
1095 int i;
1097 if (datalen < sizeof(*icommit))
1098 return got_error(GOT_ERR_PRIVSEP_LEN);
1100 icommit = imsg->data;
1101 if (datalen != sizeof(*icommit) + icommit->author_len +
1102 icommit->committer_len +
1103 icommit->nparents * SHA1_DIGEST_LENGTH)
1104 return got_error(GOT_ERR_PRIVSEP_LEN);
1106 if (icommit->nparents < 0)
1107 return got_error(GOT_ERR_PRIVSEP_LEN);
1109 len += sizeof(*icommit);
1111 *commit = got_object_commit_alloc_partial();
1112 if (*commit == NULL)
1113 return got_error_from_errno(
1114 "got_object_commit_alloc_partial");
1116 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1117 SHA1_DIGEST_LENGTH);
1118 (*commit)->author_time = icommit->author_time;
1119 (*commit)->author_gmtoff = icommit->author_gmtoff;
1120 (*commit)->committer_time = icommit->committer_time;
1121 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1123 if (icommit->author_len == 0) {
1124 (*commit)->author = strdup("");
1125 if ((*commit)->author == NULL) {
1126 err = got_error_from_errno("strdup");
1127 goto done;
1129 } else {
1130 (*commit)->author = malloc(icommit->author_len + 1);
1131 if ((*commit)->author == NULL) {
1132 err = got_error_from_errno("malloc");
1133 goto done;
1135 memcpy((*commit)->author, imsg->data + len,
1136 icommit->author_len);
1137 (*commit)->author[icommit->author_len] = '\0';
1139 len += icommit->author_len;
1141 if (icommit->committer_len == 0) {
1142 (*commit)->committer = strdup("");
1143 if ((*commit)->committer == NULL) {
1144 err = got_error_from_errno("strdup");
1145 goto done;
1147 } else {
1148 (*commit)->committer =
1149 malloc(icommit->committer_len + 1);
1150 if ((*commit)->committer == NULL) {
1151 err = got_error_from_errno("malloc");
1152 goto done;
1154 memcpy((*commit)->committer, imsg->data + len,
1155 icommit->committer_len);
1156 (*commit)->committer[icommit->committer_len] = '\0';
1158 len += icommit->committer_len;
1160 if (icommit->logmsg_len == 0) {
1161 (*commit)->logmsg = strdup("");
1162 if ((*commit)->logmsg == NULL) {
1163 err = got_error_from_errno("strdup");
1164 goto done;
1166 } else {
1167 size_t offset = 0, remain = icommit->logmsg_len;
1169 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1170 if ((*commit)->logmsg == NULL) {
1171 err = got_error_from_errno("malloc");
1172 goto done;
1174 while (remain > 0) {
1175 struct imsg imsg_log;
1176 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1177 remain);
1179 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1180 if (err)
1181 goto done;
1183 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1184 err = got_error(GOT_ERR_PRIVSEP_MSG);
1185 goto done;
1188 memcpy((*commit)->logmsg + offset,
1189 imsg_log.data, n);
1190 imsg_free(&imsg_log);
1191 offset += n;
1192 remain -= n;
1194 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1197 for (i = 0; i < icommit->nparents; i++) {
1198 struct got_object_qid *qid;
1200 err = got_object_qid_alloc_partial(&qid);
1201 if (err)
1202 break;
1203 memcpy(qid->id, imsg->data + len +
1204 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1205 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1206 (*commit)->nparents++;
1208 done:
1209 if (err) {
1210 got_object_commit_close(*commit);
1211 *commit = NULL;
1213 return err;
1216 const struct got_error *
1217 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1219 const struct got_error *err = NULL;
1220 struct imsg imsg;
1221 size_t datalen;
1222 const size_t min_datalen =
1223 MIN(sizeof(struct got_imsg_error),
1224 sizeof(struct got_imsg_commit_object));
1226 *commit = NULL;
1228 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1229 if (err)
1230 return err;
1232 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1234 switch (imsg.hdr.type) {
1235 case GOT_IMSG_COMMIT:
1236 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1237 break;
1238 default:
1239 err = got_error(GOT_ERR_PRIVSEP_MSG);
1240 break;
1243 imsg_free(&imsg);
1245 return err;
1248 const struct got_error *
1249 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1250 int nentries)
1252 const struct got_error *err = NULL;
1253 struct got_imsg_tree_object itree;
1254 struct got_pathlist_entry *pe;
1255 size_t totlen;
1256 int nimsg; /* number of imsg queued in ibuf */
1258 itree.nentries = nentries;
1259 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1260 == -1)
1261 return got_error_from_errno("imsg_compose TREE");
1263 totlen = sizeof(itree);
1264 nimsg = 1;
1265 TAILQ_FOREACH(pe, entries, entry) {
1266 const char *name = pe->path;
1267 struct got_parsed_tree_entry *pte = pe->data;
1268 struct ibuf *wbuf;
1269 size_t namelen = strlen(name);
1270 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1272 if (len > MAX_IMSGSIZE)
1273 return got_error(GOT_ERR_NO_SPACE);
1275 nimsg++;
1276 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1277 err = flush_imsg(ibuf);
1278 if (err)
1279 return err;
1280 nimsg = 0;
1283 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1284 if (wbuf == NULL)
1285 return got_error_from_errno("imsg_create TREE_ENTRY");
1287 /* Keep in sync with struct got_imsg_tree_object definition! */
1288 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1289 err = got_error_from_errno("imsg_add TREE_ENTRY");
1290 ibuf_free(wbuf);
1291 return err;
1293 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1294 err = got_error_from_errno("imsg_add TREE_ENTRY");
1295 ibuf_free(wbuf);
1296 return err;
1299 if (imsg_add(wbuf, name, namelen) == -1) {
1300 err = got_error_from_errno("imsg_add TREE_ENTRY");
1301 ibuf_free(wbuf);
1302 return err;
1305 wbuf->fd = -1;
1306 imsg_close(ibuf, wbuf);
1308 totlen += len;
1311 return flush_imsg(ibuf);
1314 const struct got_error *
1315 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1317 const struct got_error *err = NULL;
1318 const size_t min_datalen =
1319 MIN(sizeof(struct got_imsg_error),
1320 sizeof(struct got_imsg_tree_object));
1321 struct got_imsg_tree_object *itree;
1322 int nentries = 0;
1324 *tree = NULL;
1325 get_more:
1326 err = read_imsg(ibuf);
1327 if (err)
1328 goto done;
1330 for (;;) {
1331 struct imsg imsg;
1332 size_t n;
1333 size_t datalen;
1334 struct got_imsg_tree_entry *ite;
1335 struct got_tree_entry *te = NULL;
1337 n = imsg_get(ibuf, &imsg);
1338 if (n == 0) {
1339 if (*tree && (*tree)->nentries != nentries)
1340 goto get_more;
1341 break;
1344 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1345 imsg_free(&imsg);
1346 err = got_error(GOT_ERR_PRIVSEP_LEN);
1347 break;
1350 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1352 switch (imsg.hdr.type) {
1353 case GOT_IMSG_ERROR:
1354 err = recv_imsg_error(&imsg, datalen);
1355 break;
1356 case GOT_IMSG_TREE:
1357 /* This message should only appear once. */
1358 if (*tree != NULL) {
1359 err = got_error(GOT_ERR_PRIVSEP_MSG);
1360 break;
1362 if (datalen != sizeof(*itree)) {
1363 err = got_error(GOT_ERR_PRIVSEP_LEN);
1364 break;
1366 itree = imsg.data;
1367 *tree = malloc(sizeof(**tree));
1368 if (*tree == NULL) {
1369 err = got_error_from_errno("malloc");
1370 break;
1372 (*tree)->entries = calloc(itree->nentries,
1373 sizeof(struct got_tree_entry));
1374 if ((*tree)->entries == NULL) {
1375 err = got_error_from_errno("malloc");
1376 break;
1378 (*tree)->nentries = itree->nentries;
1379 (*tree)->refcnt = 0;
1380 break;
1381 case GOT_IMSG_TREE_ENTRY:
1382 /* This message should be preceeded by GOT_IMSG_TREE. */
1383 if (*tree == NULL) {
1384 err = got_error(GOT_ERR_PRIVSEP_MSG);
1385 break;
1387 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1388 err = got_error(GOT_ERR_PRIVSEP_LEN);
1389 break;
1392 /* Remaining data contains the entry's name. */
1393 datalen -= sizeof(*ite);
1394 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1395 err = got_error(GOT_ERR_PRIVSEP_LEN);
1396 break;
1398 ite = imsg.data;
1400 if (datalen + 1 > sizeof(te->name)) {
1401 err = got_error(GOT_ERR_NO_SPACE);
1402 break;
1404 te = &(*tree)->entries[nentries];
1405 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1406 te->name[datalen] = '\0';
1408 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1409 te->mode = ite->mode;
1410 te->idx = nentries;
1411 nentries++;
1412 break;
1413 default:
1414 err = got_error(GOT_ERR_PRIVSEP_MSG);
1415 break;
1418 imsg_free(&imsg);
1419 if (err)
1420 break;
1422 done:
1423 if (*tree && (*tree)->nentries != nentries) {
1424 if (err == NULL)
1425 err = got_error(GOT_ERR_PRIVSEP_LEN);
1426 got_object_tree_close(*tree);
1427 *tree = NULL;
1430 return err;
1433 const struct got_error *
1434 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1435 const uint8_t *data)
1437 struct got_imsg_blob iblob;
1439 iblob.size = size;
1440 iblob.hdrlen = hdrlen;
1442 if (data) {
1443 uint8_t *buf;
1445 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1446 return got_error(GOT_ERR_NO_SPACE);
1448 buf = malloc(sizeof(iblob) + size);
1449 if (buf == NULL)
1450 return got_error_from_errno("malloc");
1452 memcpy(buf, &iblob, sizeof(iblob));
1453 memcpy(buf + sizeof(iblob), data, size);
1454 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1455 sizeof(iblob) + size) == -1) {
1456 free(buf);
1457 return got_error_from_errno("imsg_compose BLOB");
1459 free(buf);
1460 } else {
1461 /* Data has already been written to file descriptor. */
1462 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1463 sizeof(iblob)) == -1)
1464 return got_error_from_errno("imsg_compose BLOB");
1468 return flush_imsg(ibuf);
1471 const struct got_error *
1472 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1473 struct imsgbuf *ibuf)
1475 const struct got_error *err = NULL;
1476 struct imsg imsg;
1477 struct got_imsg_blob *iblob;
1478 size_t datalen;
1480 *outbuf = NULL;
1482 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1483 if (err)
1484 return err;
1486 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1488 switch (imsg.hdr.type) {
1489 case GOT_IMSG_BLOB:
1490 if (datalen < sizeof(*iblob)) {
1491 err = got_error(GOT_ERR_PRIVSEP_LEN);
1492 break;
1494 iblob = imsg.data;
1495 *size = iblob->size;
1496 *hdrlen = iblob->hdrlen;
1498 if (datalen == sizeof(*iblob)) {
1499 /* Data has been written to file descriptor. */
1500 break;
1503 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1504 err = got_error(GOT_ERR_PRIVSEP_LEN);
1505 break;
1508 *outbuf = malloc(*size);
1509 if (*outbuf == NULL) {
1510 err = got_error_from_errno("malloc");
1511 break;
1513 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1514 break;
1515 default:
1516 err = got_error(GOT_ERR_PRIVSEP_MSG);
1517 break;
1520 imsg_free(&imsg);
1522 return err;
1525 static const struct got_error *
1526 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1528 const struct got_error *err = NULL;
1529 size_t offset, remain;
1531 offset = 0;
1532 remain = tagmsg_len;
1533 while (remain > 0) {
1534 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1536 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1537 tag->tagmsg + offset, n) == -1) {
1538 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1539 break;
1542 err = flush_imsg(ibuf);
1543 if (err)
1544 break;
1546 offset += n;
1547 remain -= n;
1550 return err;
1553 const struct got_error *
1554 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1556 const struct got_error *err = NULL;
1557 struct got_imsg_tag_object *itag;
1558 uint8_t *buf;
1559 size_t len, total;
1560 size_t tag_len = strlen(tag->tag);
1561 size_t tagger_len = strlen(tag->tagger);
1562 size_t tagmsg_len = strlen(tag->tagmsg);
1564 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1566 buf = malloc(total);
1567 if (buf == NULL)
1568 return got_error_from_errno("malloc");
1570 itag = (struct got_imsg_tag_object *)buf;
1571 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1572 itag->obj_type = tag->obj_type;
1573 itag->tag_len = tag_len;
1574 itag->tagger_len = tagger_len;
1575 itag->tagger_time = tag->tagger_time;
1576 itag->tagger_gmtoff = tag->tagger_gmtoff;
1577 itag->tagmsg_len = tagmsg_len;
1579 len = sizeof(*itag);
1580 memcpy(buf + len, tag->tag, tag_len);
1581 len += tag_len;
1582 memcpy(buf + len, tag->tagger, tagger_len);
1583 len += tagger_len;
1585 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1586 err = got_error_from_errno("imsg_compose TAG");
1587 goto done;
1590 if (tagmsg_len == 0 ||
1591 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1592 err = flush_imsg(ibuf);
1593 if (err)
1594 goto done;
1596 err = send_tagmsg(ibuf, tag, tagmsg_len);
1597 done:
1598 free(buf);
1599 return err;
1602 const struct got_error *
1603 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1605 const struct got_error *err = NULL;
1606 struct imsg imsg;
1607 struct got_imsg_tag_object *itag;
1608 size_t len, datalen;
1609 const size_t min_datalen =
1610 MIN(sizeof(struct got_imsg_error),
1611 sizeof(struct got_imsg_tag_object));
1613 *tag = NULL;
1615 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1616 if (err)
1617 return err;
1619 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1620 len = 0;
1622 switch (imsg.hdr.type) {
1623 case GOT_IMSG_TAG:
1624 if (datalen < sizeof(*itag)) {
1625 err = got_error(GOT_ERR_PRIVSEP_LEN);
1626 break;
1628 itag = imsg.data;
1629 if (datalen != sizeof(*itag) + itag->tag_len +
1630 itag->tagger_len) {
1631 err = got_error(GOT_ERR_PRIVSEP_LEN);
1632 break;
1634 len += sizeof(*itag);
1636 *tag = calloc(1, sizeof(**tag));
1637 if (*tag == NULL) {
1638 err = got_error_from_errno("calloc");
1639 break;
1642 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1644 if (itag->tag_len == 0) {
1645 (*tag)->tag = strdup("");
1646 if ((*tag)->tag == NULL) {
1647 err = got_error_from_errno("strdup");
1648 break;
1650 } else {
1651 (*tag)->tag = malloc(itag->tag_len + 1);
1652 if ((*tag)->tag == NULL) {
1653 err = got_error_from_errno("malloc");
1654 break;
1656 memcpy((*tag)->tag, imsg.data + len,
1657 itag->tag_len);
1658 (*tag)->tag[itag->tag_len] = '\0';
1660 len += itag->tag_len;
1662 (*tag)->obj_type = itag->obj_type;
1663 (*tag)->tagger_time = itag->tagger_time;
1664 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1666 if (itag->tagger_len == 0) {
1667 (*tag)->tagger = strdup("");
1668 if ((*tag)->tagger == NULL) {
1669 err = got_error_from_errno("strdup");
1670 break;
1672 } else {
1673 (*tag)->tagger = malloc(itag->tagger_len + 1);
1674 if ((*tag)->tagger == NULL) {
1675 err = got_error_from_errno("malloc");
1676 break;
1678 memcpy((*tag)->tagger, imsg.data + len,
1679 itag->tagger_len);
1680 (*tag)->tagger[itag->tagger_len] = '\0';
1682 len += itag->tagger_len;
1684 if (itag->tagmsg_len == 0) {
1685 (*tag)->tagmsg = strdup("");
1686 if ((*tag)->tagmsg == NULL) {
1687 err = got_error_from_errno("strdup");
1688 break;
1690 } else {
1691 size_t offset = 0, remain = itag->tagmsg_len;
1693 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1694 if ((*tag)->tagmsg == NULL) {
1695 err = got_error_from_errno("malloc");
1696 break;
1698 while (remain > 0) {
1699 struct imsg imsg_log;
1700 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1701 remain);
1703 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1704 if (err)
1705 return err;
1707 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1708 return got_error(GOT_ERR_PRIVSEP_MSG);
1710 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1711 n);
1712 imsg_free(&imsg_log);
1713 offset += n;
1714 remain -= n;
1716 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1719 break;
1720 default:
1721 err = got_error(GOT_ERR_PRIVSEP_MSG);
1722 break;
1725 imsg_free(&imsg);
1727 return err;
1730 const struct got_error *
1731 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1732 struct got_packidx *packidx)
1734 const struct got_error *err = NULL;
1735 struct got_imsg_packidx ipackidx;
1736 struct got_imsg_pack ipack;
1737 int fd;
1739 ipackidx.len = packidx->len;
1740 ipackidx.packfile_size = pack->filesize;
1741 fd = dup(packidx->fd);
1742 if (fd == -1)
1743 return got_error_from_errno("dup");
1745 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1746 sizeof(ipackidx)) == -1) {
1747 err = got_error_from_errno("imsg_compose PACKIDX");
1748 close(fd);
1749 return err;
1752 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1753 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1754 return got_error(GOT_ERR_NO_SPACE);
1755 ipack.filesize = pack->filesize;
1757 fd = dup(pack->fd);
1758 if (fd == -1)
1759 return got_error_from_errno("dup");
1761 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1762 == -1) {
1763 err = got_error_from_errno("imsg_compose PACK");
1764 close(fd);
1765 return err;
1768 return flush_imsg(ibuf);
1771 const struct got_error *
1772 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1773 struct got_object_id *id)
1775 struct got_imsg_packed_object iobj;
1777 iobj.idx = idx;
1778 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1780 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1781 &iobj, sizeof(iobj)) == -1)
1782 return got_error_from_errno("imsg_compose "
1783 "PACKED_OBJECT_REQUEST");
1785 return flush_imsg(ibuf);
1788 const struct got_error *
1789 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1790 struct got_object_id *id)
1792 struct got_imsg_packed_object iobj;
1794 iobj.idx = idx;
1795 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1797 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
1798 &iobj, sizeof(iobj)) == -1)
1799 return got_error_from_errno("imsg_compose "
1800 "PACKED_OBJECT_REQUEST");
1802 return flush_imsg(ibuf);
1805 const struct got_error *
1806 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1808 const struct got_error *err = NULL;
1810 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1811 NULL, 0) == -1) {
1812 err = got_error_from_errno("imsg_compose "
1813 "GITCONFIG_PARSE_REQUEST");
1814 close(fd);
1815 return err;
1818 return flush_imsg(ibuf);
1821 const struct got_error *
1822 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1824 if (imsg_compose(ibuf,
1825 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1826 NULL, 0) == -1)
1827 return got_error_from_errno("imsg_compose "
1828 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1830 return flush_imsg(ibuf);
1833 const struct got_error *
1834 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
1836 if (imsg_compose(ibuf,
1837 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
1838 NULL, 0) == -1)
1839 return got_error_from_errno("imsg_compose "
1840 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
1842 return flush_imsg(ibuf);
1846 const struct got_error *
1847 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1849 if (imsg_compose(ibuf,
1850 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1851 return got_error_from_errno("imsg_compose "
1852 "GITCONFIG_AUTHOR_NAME_REQUEST");
1854 return flush_imsg(ibuf);
1857 const struct got_error *
1858 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1860 if (imsg_compose(ibuf,
1861 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1862 return got_error_from_errno("imsg_compose "
1863 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1865 return flush_imsg(ibuf);
1868 const struct got_error *
1869 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1871 if (imsg_compose(ibuf,
1872 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1873 return got_error_from_errno("imsg_compose "
1874 "GITCONFIG_REMOTE_REQUEST");
1876 return flush_imsg(ibuf);
1879 const struct got_error *
1880 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1882 if (imsg_compose(ibuf,
1883 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1884 return got_error_from_errno("imsg_compose "
1885 "GITCONFIG_OWNER_REQUEST");
1887 return flush_imsg(ibuf);
1890 const struct got_error *
1891 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1893 const struct got_error *err = NULL;
1894 struct imsg imsg;
1895 size_t datalen;
1896 const size_t min_datalen = 0;
1898 *str = NULL;
1900 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1901 if (err)
1902 return err;
1903 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1905 switch (imsg.hdr.type) {
1906 case GOT_IMSG_GITCONFIG_STR_VAL:
1907 if (datalen == 0)
1908 break;
1909 /* datalen does not include terminating \0 */
1910 *str = malloc(datalen + 1);
1911 if (*str == NULL) {
1912 err = got_error_from_errno("malloc");
1913 break;
1915 memcpy(*str, imsg.data, datalen);
1916 (*str)[datalen] = '\0';
1917 break;
1918 default:
1919 err = got_error(GOT_ERR_PRIVSEP_MSG);
1920 break;
1923 imsg_free(&imsg);
1924 return err;
1927 const struct got_error *
1928 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1930 const struct got_error *err = NULL;
1931 struct imsg imsg;
1932 size_t datalen;
1933 const size_t min_datalen =
1934 MIN(sizeof(struct got_imsg_error), sizeof(int));
1936 *val = 0;
1938 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1939 if (err)
1940 return err;
1941 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1943 switch (imsg.hdr.type) {
1944 case GOT_IMSG_GITCONFIG_INT_VAL:
1945 if (datalen != sizeof(*val)) {
1946 err = got_error(GOT_ERR_PRIVSEP_LEN);
1947 break;
1949 memcpy(val, imsg.data, sizeof(*val));
1950 break;
1951 default:
1952 err = got_error(GOT_ERR_PRIVSEP_MSG);
1953 break;
1956 imsg_free(&imsg);
1957 return err;
1960 static void
1961 free_remote_data(struct got_remote_repo *remote)
1963 int i;
1965 free(remote->name);
1966 free(remote->url);
1967 for (i = 0; i < remote->nbranches; i++)
1968 free(remote->branches[i]);
1969 free(remote->branches);
1970 for (i = 0; i < remote->nrefs; i++)
1971 free(remote->refs[i]);
1972 free(remote->refs);
1975 const struct got_error *
1976 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1977 int *nremotes, struct imsgbuf *ibuf)
1979 const struct got_error *err = NULL;
1980 struct imsg imsg;
1981 size_t datalen;
1982 struct got_imsg_remotes iremotes;
1983 struct got_imsg_remote iremote;
1985 *remotes = NULL;
1986 *nremotes = 0;
1987 iremotes.nremotes = 0;
1989 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1990 if (err)
1991 return err;
1992 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1994 switch (imsg.hdr.type) {
1995 case GOT_IMSG_GITCONFIG_REMOTES:
1996 if (datalen != sizeof(iremotes)) {
1997 err = got_error(GOT_ERR_PRIVSEP_LEN);
1998 break;
2000 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2001 if (iremotes.nremotes == 0) {
2002 imsg_free(&imsg);
2003 return NULL;
2005 break;
2006 default:
2007 imsg_free(&imsg);
2008 return got_error(GOT_ERR_PRIVSEP_MSG);
2011 imsg_free(&imsg);
2013 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2014 if (*remotes == NULL)
2015 return got_error_from_errno("recallocarray");
2017 while (*nremotes < iremotes.nremotes) {
2018 struct got_remote_repo *remote;
2020 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2021 if (err)
2022 break;
2023 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2025 switch (imsg.hdr.type) {
2026 case GOT_IMSG_GITCONFIG_REMOTE:
2027 remote = &(*remotes)[*nremotes];
2028 memset(remote, 0, sizeof(*remote));
2029 if (datalen < sizeof(iremote)) {
2030 err = got_error(GOT_ERR_PRIVSEP_LEN);
2031 break;
2033 memcpy(&iremote, imsg.data, sizeof(iremote));
2034 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2035 (sizeof(iremote) + iremote.name_len +
2036 iremote.url_len) > datalen) {
2037 err = got_error(GOT_ERR_PRIVSEP_LEN);
2038 break;
2040 remote->name = strndup(imsg.data + sizeof(iremote),
2041 iremote.name_len);
2042 if (remote->name == NULL) {
2043 err = got_error_from_errno("strndup");
2044 break;
2046 remote->url = strndup(imsg.data + sizeof(iremote) +
2047 iremote.name_len, iremote.url_len);
2048 if (remote->url == NULL) {
2049 err = got_error_from_errno("strndup");
2050 free_remote_data(remote);
2051 break;
2053 remote->mirror_references = iremote.mirror_references;
2054 remote->fetch_all_branches = iremote.fetch_all_branches;
2055 remote->nbranches = 0;
2056 remote->branches = NULL;
2057 remote->nrefs = 0;
2058 remote->refs = NULL;
2059 (*nremotes)++;
2060 break;
2061 default:
2062 err = got_error(GOT_ERR_PRIVSEP_MSG);
2063 break;
2066 imsg_free(&imsg);
2067 if (err)
2068 break;
2071 if (err) {
2072 int i;
2073 for (i = 0; i < *nremotes; i++)
2074 free_remote_data(&(*remotes)[i]);
2075 free(*remotes);
2076 *remotes = NULL;
2077 *nremotes = 0;
2079 return err;
2082 const struct got_error *
2083 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2085 const struct got_error *err = NULL;
2087 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2088 NULL, 0) == -1) {
2089 err = got_error_from_errno("imsg_compose "
2090 "GOTCONFIG_PARSE_REQUEST");
2091 close(fd);
2092 return err;
2095 return flush_imsg(ibuf);
2098 const struct got_error *
2099 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2101 if (imsg_compose(ibuf,
2102 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2103 return got_error_from_errno("imsg_compose "
2104 "GOTCONFIG_AUTHOR_REQUEST");
2106 return flush_imsg(ibuf);
2109 const struct got_error *
2110 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2112 if (imsg_compose(ibuf,
2113 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2114 return got_error_from_errno("imsg_compose "
2115 "GOTCONFIG_REMOTE_REQUEST");
2117 return flush_imsg(ibuf);
2120 const struct got_error *
2121 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2123 const struct got_error *err = NULL;
2124 struct imsg imsg;
2125 size_t datalen;
2126 const size_t min_datalen = 0;
2128 *str = NULL;
2130 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2131 if (err)
2132 return err;
2133 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2135 switch (imsg.hdr.type) {
2136 case GOT_IMSG_ERROR:
2137 if (datalen < sizeof(struct got_imsg_error)) {
2138 err = got_error(GOT_ERR_PRIVSEP_LEN);
2139 break;
2141 err = recv_imsg_error(&imsg, datalen);
2142 break;
2143 case GOT_IMSG_GOTCONFIG_STR_VAL:
2144 if (datalen == 0)
2145 break;
2146 /* datalen does not include terminating \0 */
2147 *str = malloc(datalen + 1);
2148 if (*str == NULL) {
2149 err = got_error_from_errno("malloc");
2150 break;
2152 memcpy(*str, imsg.data, datalen);
2153 (*str)[datalen] = '\0';
2154 break;
2155 default:
2156 err = got_error(GOT_ERR_PRIVSEP_MSG);
2157 break;
2160 imsg_free(&imsg);
2161 return err;
2165 const struct got_error *
2166 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2167 int *nremotes, struct imsgbuf *ibuf)
2169 const struct got_error *err = NULL;
2170 struct imsg imsg;
2171 size_t datalen;
2172 struct got_imsg_remotes iremotes;
2173 struct got_imsg_remote iremote;
2174 const size_t min_datalen =
2175 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2177 *remotes = NULL;
2178 *nremotes = 0;
2179 iremotes.nremotes = 0;
2181 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2182 if (err)
2183 return err;
2184 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2186 switch (imsg.hdr.type) {
2187 case GOT_IMSG_ERROR:
2188 if (datalen < sizeof(struct got_imsg_error)) {
2189 err = got_error(GOT_ERR_PRIVSEP_LEN);
2190 break;
2192 err = recv_imsg_error(&imsg, datalen);
2193 break;
2194 case GOT_IMSG_GOTCONFIG_REMOTES:
2195 if (datalen != sizeof(iremotes)) {
2196 err = got_error(GOT_ERR_PRIVSEP_LEN);
2197 break;
2199 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2200 if (iremotes.nremotes == 0) {
2201 imsg_free(&imsg);
2202 return NULL;
2204 break;
2205 default:
2206 imsg_free(&imsg);
2207 return got_error(GOT_ERR_PRIVSEP_MSG);
2210 imsg_free(&imsg);
2212 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2213 if (*remotes == NULL)
2214 return got_error_from_errno("recallocarray");
2216 while (*nremotes < iremotes.nremotes) {
2217 struct got_remote_repo *remote;
2218 const size_t min_datalen =
2219 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2220 int i;
2222 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2223 if (err)
2224 break;
2225 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2227 switch (imsg.hdr.type) {
2228 case GOT_IMSG_ERROR:
2229 if (datalen < sizeof(struct got_imsg_error)) {
2230 err = got_error(GOT_ERR_PRIVSEP_LEN);
2231 break;
2233 err = recv_imsg_error(&imsg, datalen);
2234 break;
2235 case GOT_IMSG_GOTCONFIG_REMOTE:
2236 remote = &(*remotes)[*nremotes];
2237 memset(remote, 0, sizeof(*remote));
2238 if (datalen < sizeof(iremote)) {
2239 err = got_error(GOT_ERR_PRIVSEP_LEN);
2240 break;
2242 memcpy(&iremote, imsg.data, sizeof(iremote));
2243 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2244 (sizeof(iremote) + iremote.name_len +
2245 iremote.url_len) > datalen) {
2246 err = got_error(GOT_ERR_PRIVSEP_LEN);
2247 break;
2249 remote->name = strndup(imsg.data + sizeof(iremote),
2250 iremote.name_len);
2251 if (remote->name == NULL) {
2252 err = got_error_from_errno("strndup");
2253 break;
2255 remote->url = strndup(imsg.data + sizeof(iremote) +
2256 iremote.name_len, iremote.url_len);
2257 if (remote->url == NULL) {
2258 err = got_error_from_errno("strndup");
2259 free_remote_data(remote);
2260 break;
2262 remote->mirror_references = iremote.mirror_references;
2263 remote->fetch_all_branches = iremote.fetch_all_branches;
2264 if (iremote.nbranches > 0) {
2265 remote->branches = recallocarray(NULL, 0,
2266 iremote.nbranches, sizeof(char *));
2267 if (remote->branches == NULL) {
2268 err = got_error_from_errno("calloc");
2269 free_remote_data(remote);
2270 break;
2273 remote->nbranches = 0;
2274 for (i = 0; i < iremote.nbranches; i++) {
2275 char *branch;
2276 err = got_privsep_recv_gotconfig_str(&branch,
2277 ibuf);
2278 if (err) {
2279 free_remote_data(remote);
2280 goto done;
2282 remote->branches[i] = branch;
2283 remote->nbranches++;
2285 if (iremote.nrefs > 0) {
2286 remote->refs = recallocarray(NULL, 0,
2287 iremote.nrefs, sizeof(char *));
2288 if (remote->refs == NULL) {
2289 err = got_error_from_errno("calloc");
2290 free_remote_data(remote);
2291 break;
2294 remote->nrefs = 0;
2295 for (i = 0; i < iremote.nrefs; i++) {
2296 char *ref;
2297 err = got_privsep_recv_gotconfig_str(&ref,
2298 ibuf);
2299 if (err) {
2300 free_remote_data(remote);
2301 goto done;
2303 remote->refs[i] = ref;
2304 remote->nrefs++;
2306 (*nremotes)++;
2307 break;
2308 default:
2309 err = got_error(GOT_ERR_PRIVSEP_MSG);
2310 break;
2313 imsg_free(&imsg);
2314 if (err)
2315 break;
2317 done:
2318 if (err) {
2319 int i;
2320 for (i = 0; i < *nremotes; i++)
2321 free_remote_data(&(*remotes)[i]);
2322 free(*remotes);
2323 *remotes = NULL;
2324 *nremotes = 0;
2326 return err;
2329 const struct got_error *
2330 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2331 struct got_object_id *id, int idx, const char *path)
2333 const struct got_error *err = NULL;
2334 struct ibuf *wbuf;
2335 size_t path_len = strlen(path) + 1;
2337 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2338 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2339 if (wbuf == NULL)
2340 return got_error_from_errno(
2341 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2342 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2343 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2344 ibuf_free(wbuf);
2345 return err;
2347 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2348 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2349 ibuf_free(wbuf);
2350 return err;
2352 if (imsg_add(wbuf, path, path_len) == -1) {
2353 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2354 ibuf_free(wbuf);
2355 return err;
2358 wbuf->fd = -1;
2359 imsg_close(ibuf, wbuf);
2361 return flush_imsg(ibuf);
2364 const struct got_error *
2365 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2366 struct got_object_id **changed_commit_id,
2367 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2369 const struct got_error *err = NULL;
2370 struct imsg imsg;
2371 struct got_imsg_traversed_commits *icommits;
2372 size_t datalen;
2373 int i, done = 0;
2375 *changed_commit = NULL;
2376 *changed_commit_id = NULL;
2378 while (!done) {
2379 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2380 if (err)
2381 return err;
2383 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2384 switch (imsg.hdr.type) {
2385 case GOT_IMSG_TRAVERSED_COMMITS:
2386 icommits = imsg.data;
2387 if (datalen != sizeof(*icommits) +
2388 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2389 err = got_error(GOT_ERR_PRIVSEP_LEN);
2390 break;
2392 for (i = 0; i < icommits->ncommits; i++) {
2393 struct got_object_qid *qid;
2394 uint8_t *sha1 = (uint8_t *)imsg.data +
2395 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2396 err = got_object_qid_alloc_partial(&qid);
2397 if (err)
2398 break;
2399 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2400 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2402 /* The last commit may contain a change. */
2403 if (i == icommits->ncommits - 1) {
2404 *changed_commit_id =
2405 got_object_id_dup(qid->id);
2406 if (*changed_commit_id == NULL) {
2407 err = got_error_from_errno(
2408 "got_object_id_dup");
2409 break;
2413 break;
2414 case GOT_IMSG_COMMIT:
2415 if (*changed_commit_id == NULL) {
2416 err = got_error(GOT_ERR_PRIVSEP_MSG);
2417 break;
2419 err = get_commit_from_imsg(changed_commit, &imsg,
2420 datalen, ibuf);
2421 break;
2422 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2423 done = 1;
2424 break;
2425 default:
2426 err = got_error(GOT_ERR_PRIVSEP_MSG);
2427 break;
2430 imsg_free(&imsg);
2431 if (err)
2432 break;
2435 if (err)
2436 got_object_id_queue_free(commit_ids);
2437 return err;
2440 const struct got_error *
2441 got_privsep_unveil_exec_helpers(void)
2443 const char *helpers[] = {
2444 GOT_PATH_PROG_READ_PACK,
2445 GOT_PATH_PROG_READ_OBJECT,
2446 GOT_PATH_PROG_READ_COMMIT,
2447 GOT_PATH_PROG_READ_TREE,
2448 GOT_PATH_PROG_READ_BLOB,
2449 GOT_PATH_PROG_READ_TAG,
2450 GOT_PATH_PROG_READ_GITCONFIG,
2451 GOT_PATH_PROG_READ_GOTCONFIG,
2452 GOT_PATH_PROG_FETCH_PACK,
2453 GOT_PATH_PROG_INDEX_PACK,
2455 size_t i;
2457 for (i = 0; i < nitems(helpers); i++) {
2458 if (unveil(helpers[i], "x") == 0)
2459 continue;
2460 return got_error_from_errno2("unveil", helpers[i]);
2463 return NULL;
2466 void
2467 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2469 if (close(imsg_fds[0]) == -1) {
2470 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2471 _exit(1);
2474 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2475 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2476 _exit(1);
2478 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2479 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2480 _exit(1);
2483 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2484 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2485 strerror(errno));
2486 _exit(1);