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 fd = dup(packidx->fd);
1741 if (fd == -1)
1742 return got_error_from_errno("dup");
1744 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1745 sizeof(ipackidx)) == -1) {
1746 err = got_error_from_errno("imsg_compose PACKIDX");
1747 close(fd);
1748 return err;
1751 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1752 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1753 return got_error(GOT_ERR_NO_SPACE);
1754 ipack.filesize = pack->filesize;
1756 fd = dup(pack->fd);
1757 if (fd == -1)
1758 return got_error_from_errno("dup");
1760 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1761 == -1) {
1762 err = got_error_from_errno("imsg_compose PACK");
1763 close(fd);
1764 return err;
1767 return flush_imsg(ibuf);
1770 const struct got_error *
1771 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1772 struct got_object_id *id)
1774 struct got_imsg_packed_object iobj;
1776 iobj.idx = idx;
1777 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1779 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1780 &iobj, sizeof(iobj)) == -1)
1781 return got_error_from_errno("imsg_compose "
1782 "PACKED_OBJECT_REQUEST");
1784 return flush_imsg(ibuf);
1787 const struct got_error *
1788 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
1789 struct got_object_id *id)
1791 struct got_imsg_packed_object iobj;
1793 iobj.idx = idx;
1794 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1796 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
1797 &iobj, sizeof(iobj)) == -1)
1798 return got_error_from_errno("imsg_compose "
1799 "PACKED_OBJECT_REQUEST");
1801 return flush_imsg(ibuf);
1804 const struct got_error *
1805 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1807 const struct got_error *err = NULL;
1809 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1810 NULL, 0) == -1) {
1811 err = got_error_from_errno("imsg_compose "
1812 "GITCONFIG_PARSE_REQUEST");
1813 close(fd);
1814 return err;
1817 return flush_imsg(ibuf);
1820 const struct got_error *
1821 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1823 if (imsg_compose(ibuf,
1824 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1825 NULL, 0) == -1)
1826 return got_error_from_errno("imsg_compose "
1827 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1829 return flush_imsg(ibuf);
1832 const struct got_error *
1833 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
1835 if (imsg_compose(ibuf,
1836 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
1837 NULL, 0) == -1)
1838 return got_error_from_errno("imsg_compose "
1839 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
1841 return flush_imsg(ibuf);
1845 const struct got_error *
1846 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1848 if (imsg_compose(ibuf,
1849 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1850 return got_error_from_errno("imsg_compose "
1851 "GITCONFIG_AUTHOR_NAME_REQUEST");
1853 return flush_imsg(ibuf);
1856 const struct got_error *
1857 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1859 if (imsg_compose(ibuf,
1860 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1861 return got_error_from_errno("imsg_compose "
1862 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1864 return flush_imsg(ibuf);
1867 const struct got_error *
1868 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1870 if (imsg_compose(ibuf,
1871 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1872 return got_error_from_errno("imsg_compose "
1873 "GITCONFIG_REMOTE_REQUEST");
1875 return flush_imsg(ibuf);
1878 const struct got_error *
1879 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1881 if (imsg_compose(ibuf,
1882 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1883 return got_error_from_errno("imsg_compose "
1884 "GITCONFIG_OWNER_REQUEST");
1886 return flush_imsg(ibuf);
1889 const struct got_error *
1890 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1892 const struct got_error *err = NULL;
1893 struct imsg imsg;
1894 size_t datalen;
1895 const size_t min_datalen = 0;
1897 *str = NULL;
1899 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1900 if (err)
1901 return err;
1902 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1904 switch (imsg.hdr.type) {
1905 case GOT_IMSG_GITCONFIG_STR_VAL:
1906 if (datalen == 0)
1907 break;
1908 /* datalen does not include terminating \0 */
1909 *str = malloc(datalen + 1);
1910 if (*str == NULL) {
1911 err = got_error_from_errno("malloc");
1912 break;
1914 memcpy(*str, imsg.data, datalen);
1915 (*str)[datalen] = '\0';
1916 break;
1917 default:
1918 err = got_error(GOT_ERR_PRIVSEP_MSG);
1919 break;
1922 imsg_free(&imsg);
1923 return err;
1926 const struct got_error *
1927 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1929 const struct got_error *err = NULL;
1930 struct imsg imsg;
1931 size_t datalen;
1932 const size_t min_datalen =
1933 MIN(sizeof(struct got_imsg_error), sizeof(int));
1935 *val = 0;
1937 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1938 if (err)
1939 return err;
1940 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1942 switch (imsg.hdr.type) {
1943 case GOT_IMSG_GITCONFIG_INT_VAL:
1944 if (datalen != sizeof(*val)) {
1945 err = got_error(GOT_ERR_PRIVSEP_LEN);
1946 break;
1948 memcpy(val, imsg.data, sizeof(*val));
1949 break;
1950 default:
1951 err = got_error(GOT_ERR_PRIVSEP_MSG);
1952 break;
1955 imsg_free(&imsg);
1956 return err;
1959 static void
1960 free_remote_data(struct got_remote_repo *remote)
1962 int i;
1964 free(remote->name);
1965 free(remote->url);
1966 for (i = 0; i < remote->nbranches; i++)
1967 free(remote->branches[i]);
1968 free(remote->branches);
1969 for (i = 0; i < remote->nrefs; i++)
1970 free(remote->refs[i]);
1971 free(remote->refs);
1974 const struct got_error *
1975 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1976 int *nremotes, struct imsgbuf *ibuf)
1978 const struct got_error *err = NULL;
1979 struct imsg imsg;
1980 size_t datalen;
1981 struct got_imsg_remotes iremotes;
1982 struct got_imsg_remote iremote;
1984 *remotes = NULL;
1985 *nremotes = 0;
1986 iremotes.nremotes = 0;
1988 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1989 if (err)
1990 return err;
1991 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1993 switch (imsg.hdr.type) {
1994 case GOT_IMSG_GITCONFIG_REMOTES:
1995 if (datalen != sizeof(iremotes)) {
1996 err = got_error(GOT_ERR_PRIVSEP_LEN);
1997 break;
1999 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2000 if (iremotes.nremotes == 0) {
2001 imsg_free(&imsg);
2002 return NULL;
2004 break;
2005 default:
2006 imsg_free(&imsg);
2007 return got_error(GOT_ERR_PRIVSEP_MSG);
2010 imsg_free(&imsg);
2012 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2013 if (*remotes == NULL)
2014 return got_error_from_errno("recallocarray");
2016 while (*nremotes < iremotes.nremotes) {
2017 struct got_remote_repo *remote;
2019 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2020 if (err)
2021 break;
2022 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2024 switch (imsg.hdr.type) {
2025 case GOT_IMSG_GITCONFIG_REMOTE:
2026 remote = &(*remotes)[*nremotes];
2027 memset(remote, 0, sizeof(*remote));
2028 if (datalen < sizeof(iremote)) {
2029 err = got_error(GOT_ERR_PRIVSEP_LEN);
2030 break;
2032 memcpy(&iremote, imsg.data, sizeof(iremote));
2033 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2034 (sizeof(iremote) + iremote.name_len +
2035 iremote.url_len) > datalen) {
2036 err = got_error(GOT_ERR_PRIVSEP_LEN);
2037 break;
2039 remote->name = strndup(imsg.data + sizeof(iremote),
2040 iremote.name_len);
2041 if (remote->name == NULL) {
2042 err = got_error_from_errno("strndup");
2043 break;
2045 remote->url = strndup(imsg.data + sizeof(iremote) +
2046 iremote.name_len, iremote.url_len);
2047 if (remote->url == NULL) {
2048 err = got_error_from_errno("strndup");
2049 free_remote_data(remote);
2050 break;
2052 remote->mirror_references = iremote.mirror_references;
2053 remote->fetch_all_branches = iremote.fetch_all_branches;
2054 remote->nbranches = 0;
2055 remote->branches = NULL;
2056 remote->nrefs = 0;
2057 remote->refs = NULL;
2058 (*nremotes)++;
2059 break;
2060 default:
2061 err = got_error(GOT_ERR_PRIVSEP_MSG);
2062 break;
2065 imsg_free(&imsg);
2066 if (err)
2067 break;
2070 if (err) {
2071 int i;
2072 for (i = 0; i < *nremotes; i++)
2073 free_remote_data(&(*remotes)[i]);
2074 free(*remotes);
2075 *remotes = NULL;
2076 *nremotes = 0;
2078 return err;
2081 const struct got_error *
2082 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2084 const struct got_error *err = NULL;
2086 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2087 NULL, 0) == -1) {
2088 err = got_error_from_errno("imsg_compose "
2089 "GOTCONFIG_PARSE_REQUEST");
2090 close(fd);
2091 return err;
2094 return flush_imsg(ibuf);
2097 const struct got_error *
2098 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2100 if (imsg_compose(ibuf,
2101 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2102 return got_error_from_errno("imsg_compose "
2103 "GOTCONFIG_AUTHOR_REQUEST");
2105 return flush_imsg(ibuf);
2108 const struct got_error *
2109 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2111 if (imsg_compose(ibuf,
2112 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2113 return got_error_from_errno("imsg_compose "
2114 "GOTCONFIG_REMOTE_REQUEST");
2116 return flush_imsg(ibuf);
2119 const struct got_error *
2120 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2122 const struct got_error *err = NULL;
2123 struct imsg imsg;
2124 size_t datalen;
2125 const size_t min_datalen = 0;
2127 *str = NULL;
2129 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2130 if (err)
2131 return err;
2132 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2134 switch (imsg.hdr.type) {
2135 case GOT_IMSG_ERROR:
2136 if (datalen < sizeof(struct got_imsg_error)) {
2137 err = got_error(GOT_ERR_PRIVSEP_LEN);
2138 break;
2140 err = recv_imsg_error(&imsg, datalen);
2141 break;
2142 case GOT_IMSG_GOTCONFIG_STR_VAL:
2143 if (datalen == 0)
2144 break;
2145 /* datalen does not include terminating \0 */
2146 *str = malloc(datalen + 1);
2147 if (*str == NULL) {
2148 err = got_error_from_errno("malloc");
2149 break;
2151 memcpy(*str, imsg.data, datalen);
2152 (*str)[datalen] = '\0';
2153 break;
2154 default:
2155 err = got_error(GOT_ERR_PRIVSEP_MSG);
2156 break;
2159 imsg_free(&imsg);
2160 return err;
2164 const struct got_error *
2165 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2166 int *nremotes, struct imsgbuf *ibuf)
2168 const struct got_error *err = NULL;
2169 struct imsg imsg;
2170 size_t datalen;
2171 struct got_imsg_remotes iremotes;
2172 struct got_imsg_remote iremote;
2173 const size_t min_datalen =
2174 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2176 *remotes = NULL;
2177 *nremotes = 0;
2178 iremotes.nremotes = 0;
2180 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2181 if (err)
2182 return err;
2183 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2185 switch (imsg.hdr.type) {
2186 case GOT_IMSG_ERROR:
2187 if (datalen < sizeof(struct got_imsg_error)) {
2188 err = got_error(GOT_ERR_PRIVSEP_LEN);
2189 break;
2191 err = recv_imsg_error(&imsg, datalen);
2192 break;
2193 case GOT_IMSG_GOTCONFIG_REMOTES:
2194 if (datalen != sizeof(iremotes)) {
2195 err = got_error(GOT_ERR_PRIVSEP_LEN);
2196 break;
2198 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2199 if (iremotes.nremotes == 0) {
2200 imsg_free(&imsg);
2201 return NULL;
2203 break;
2204 default:
2205 imsg_free(&imsg);
2206 return got_error(GOT_ERR_PRIVSEP_MSG);
2209 imsg_free(&imsg);
2211 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2212 if (*remotes == NULL)
2213 return got_error_from_errno("recallocarray");
2215 while (*nremotes < iremotes.nremotes) {
2216 struct got_remote_repo *remote;
2217 const size_t min_datalen =
2218 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2219 int i;
2221 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2222 if (err)
2223 break;
2224 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2226 switch (imsg.hdr.type) {
2227 case GOT_IMSG_ERROR:
2228 if (datalen < sizeof(struct got_imsg_error)) {
2229 err = got_error(GOT_ERR_PRIVSEP_LEN);
2230 break;
2232 err = recv_imsg_error(&imsg, datalen);
2233 break;
2234 case GOT_IMSG_GOTCONFIG_REMOTE:
2235 remote = &(*remotes)[*nremotes];
2236 memset(remote, 0, sizeof(*remote));
2237 if (datalen < sizeof(iremote)) {
2238 err = got_error(GOT_ERR_PRIVSEP_LEN);
2239 break;
2241 memcpy(&iremote, imsg.data, sizeof(iremote));
2242 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2243 (sizeof(iremote) + iremote.name_len +
2244 iremote.url_len) > datalen) {
2245 err = got_error(GOT_ERR_PRIVSEP_LEN);
2246 break;
2248 remote->name = strndup(imsg.data + sizeof(iremote),
2249 iremote.name_len);
2250 if (remote->name == NULL) {
2251 err = got_error_from_errno("strndup");
2252 break;
2254 remote->url = strndup(imsg.data + sizeof(iremote) +
2255 iremote.name_len, iremote.url_len);
2256 if (remote->url == NULL) {
2257 err = got_error_from_errno("strndup");
2258 free_remote_data(remote);
2259 break;
2261 remote->mirror_references = iremote.mirror_references;
2262 remote->fetch_all_branches = iremote.fetch_all_branches;
2263 if (iremote.nbranches > 0) {
2264 remote->branches = recallocarray(NULL, 0,
2265 iremote.nbranches, sizeof(char *));
2266 if (remote->branches == NULL) {
2267 err = got_error_from_errno("calloc");
2268 free_remote_data(remote);
2269 break;
2272 remote->nbranches = 0;
2273 for (i = 0; i < iremote.nbranches; i++) {
2274 char *branch;
2275 err = got_privsep_recv_gotconfig_str(&branch,
2276 ibuf);
2277 if (err) {
2278 free_remote_data(remote);
2279 goto done;
2281 remote->branches[i] = branch;
2282 remote->nbranches++;
2284 if (iremote.nrefs > 0) {
2285 remote->refs = recallocarray(NULL, 0,
2286 iremote.nrefs, sizeof(char *));
2287 if (remote->refs == NULL) {
2288 err = got_error_from_errno("calloc");
2289 free_remote_data(remote);
2290 break;
2293 remote->nrefs = 0;
2294 for (i = 0; i < iremote.nrefs; i++) {
2295 char *ref;
2296 err = got_privsep_recv_gotconfig_str(&ref,
2297 ibuf);
2298 if (err) {
2299 free_remote_data(remote);
2300 goto done;
2302 remote->refs[i] = ref;
2303 remote->nrefs++;
2305 (*nremotes)++;
2306 break;
2307 default:
2308 err = got_error(GOT_ERR_PRIVSEP_MSG);
2309 break;
2312 imsg_free(&imsg);
2313 if (err)
2314 break;
2316 done:
2317 if (err) {
2318 int i;
2319 for (i = 0; i < *nremotes; i++)
2320 free_remote_data(&(*remotes)[i]);
2321 free(*remotes);
2322 *remotes = NULL;
2323 *nremotes = 0;
2325 return err;
2328 const struct got_error *
2329 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2330 struct got_object_id *id, int idx, const char *path)
2332 const struct got_error *err = NULL;
2333 struct ibuf *wbuf;
2334 size_t path_len = strlen(path) + 1;
2336 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2337 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2338 if (wbuf == NULL)
2339 return got_error_from_errno(
2340 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2341 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2342 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2343 ibuf_free(wbuf);
2344 return err;
2346 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2347 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2348 ibuf_free(wbuf);
2349 return err;
2351 if (imsg_add(wbuf, path, path_len) == -1) {
2352 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2353 ibuf_free(wbuf);
2354 return err;
2357 wbuf->fd = -1;
2358 imsg_close(ibuf, wbuf);
2360 return flush_imsg(ibuf);
2363 const struct got_error *
2364 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2365 struct got_object_id **changed_commit_id,
2366 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2368 const struct got_error *err = NULL;
2369 struct imsg imsg;
2370 struct got_imsg_traversed_commits *icommits;
2371 size_t datalen;
2372 int i, done = 0;
2374 *changed_commit = NULL;
2375 *changed_commit_id = NULL;
2377 while (!done) {
2378 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2379 if (err)
2380 return err;
2382 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2383 switch (imsg.hdr.type) {
2384 case GOT_IMSG_TRAVERSED_COMMITS:
2385 icommits = imsg.data;
2386 if (datalen != sizeof(*icommits) +
2387 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2388 err = got_error(GOT_ERR_PRIVSEP_LEN);
2389 break;
2391 for (i = 0; i < icommits->ncommits; i++) {
2392 struct got_object_qid *qid;
2393 uint8_t *sha1 = (uint8_t *)imsg.data +
2394 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2395 err = got_object_qid_alloc_partial(&qid);
2396 if (err)
2397 break;
2398 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2399 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2401 /* The last commit may contain a change. */
2402 if (i == icommits->ncommits - 1) {
2403 *changed_commit_id =
2404 got_object_id_dup(qid->id);
2405 if (*changed_commit_id == NULL) {
2406 err = got_error_from_errno(
2407 "got_object_id_dup");
2408 break;
2412 break;
2413 case GOT_IMSG_COMMIT:
2414 if (*changed_commit_id == NULL) {
2415 err = got_error(GOT_ERR_PRIVSEP_MSG);
2416 break;
2418 err = get_commit_from_imsg(changed_commit, &imsg,
2419 datalen, ibuf);
2420 break;
2421 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2422 done = 1;
2423 break;
2424 default:
2425 err = got_error(GOT_ERR_PRIVSEP_MSG);
2426 break;
2429 imsg_free(&imsg);
2430 if (err)
2431 break;
2434 if (err)
2435 got_object_id_queue_free(commit_ids);
2436 return err;
2439 const struct got_error *
2440 got_privsep_unveil_exec_helpers(void)
2442 const char *helpers[] = {
2443 GOT_PATH_PROG_READ_PACK,
2444 GOT_PATH_PROG_READ_OBJECT,
2445 GOT_PATH_PROG_READ_COMMIT,
2446 GOT_PATH_PROG_READ_TREE,
2447 GOT_PATH_PROG_READ_BLOB,
2448 GOT_PATH_PROG_READ_TAG,
2449 GOT_PATH_PROG_READ_GITCONFIG,
2450 GOT_PATH_PROG_READ_GOTCONFIG,
2451 GOT_PATH_PROG_FETCH_PACK,
2452 GOT_PATH_PROG_INDEX_PACK,
2454 size_t i;
2456 for (i = 0; i < nitems(helpers); i++) {
2457 if (unveil(helpers[i], "x") == 0)
2458 continue;
2459 return got_error_from_errno2("unveil", helpers[i]);
2462 return NULL;
2465 void
2466 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2468 if (close(imsg_fds[0]) == -1) {
2469 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2470 _exit(1);
2473 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2474 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2475 _exit(1);
2477 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2478 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2479 _exit(1);
2482 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2483 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2484 strerror(errno));
2485 _exit(1);