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_REQUEST");
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_REQUEST");
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 static const struct got_error *
866 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
867 int delete, struct imsgbuf *ibuf)
869 const struct got_error *err = NULL;
870 size_t len;
871 struct ibuf *wbuf;
873 len = sizeof(struct got_imsg_send_ref) + name_len;
874 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
875 if (wbuf == NULL)
876 return got_error_from_errno("imsg_create SEND_REF");
878 /* Keep in sync with struct got_imsg_send_ref! */
879 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
880 err = got_error_from_errno("imsg_add SEND_REF");
881 ibuf_free(wbuf);
882 return err;
884 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1) {
885 err = got_error_from_errno("imsg_add SEND_REF");
886 ibuf_free(wbuf);
887 return err;
889 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
890 err = got_error_from_errno("imsg_add SEND_REF");
891 ibuf_free(wbuf);
892 return err;
894 if (imsg_add(wbuf, name, name_len) == -1) {
895 err = got_error_from_errno("imsg_add SEND_REF");
896 ibuf_free(wbuf);
897 return err;
900 wbuf->fd = -1;
901 imsg_close(ibuf, wbuf);
902 return flush_imsg(ibuf);
905 const struct got_error *
906 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
907 struct got_pathlist_head *have_refs,
908 struct got_pathlist_head *delete_refs,
909 int verbosity)
911 const struct got_error *err = NULL;
912 struct got_pathlist_entry *pe;
913 struct got_imsg_send_request sendreq;
914 struct got_object_id zero_id;
916 memset(&zero_id, 0, sizeof(zero_id));
917 memset(&sendreq, 0, sizeof(sendreq));
918 sendreq.verbosity = verbosity;
919 TAILQ_FOREACH(pe, have_refs, entry)
920 sendreq.nrefs++;
921 TAILQ_FOREACH(pe, delete_refs, entry)
922 sendreq.nrefs++;
923 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
924 &sendreq, sizeof(sendreq)) == -1) {
925 err = got_error_from_errno(
926 "imsg_compose FETCH_SERVER_PROGRESS");
927 goto done;
930 err = flush_imsg(ibuf);
931 if (err)
932 goto done;
933 fd = -1;
935 TAILQ_FOREACH(pe, have_refs, entry) {
936 const char *name = pe->path;
937 size_t name_len = pe->path_len;
938 struct got_object_id *id = pe->data;
939 err = send_send_ref(name, name_len, id, 0, ibuf);
940 if (err)
941 goto done;
944 TAILQ_FOREACH(pe, delete_refs, entry) {
945 const char *name = pe->path;
946 size_t name_len = pe->path_len;
947 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
948 if (err)
949 goto done;
951 done:
952 if (fd != -1 && close(fd) == -1 && err == NULL)
953 err = got_error_from_errno("close");
954 return err;
958 const struct got_error *
959 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
960 struct imsgbuf *ibuf)
962 const struct got_error *err = NULL;
963 struct imsg imsg;
964 size_t datalen;
965 int done = 0;
966 struct got_imsg_send_remote_ref iremote_ref;
967 struct got_object_id *id = NULL;
968 char *refname = NULL;
969 struct got_pathlist_entry *new;
971 while (!done) {
972 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
973 if (err)
974 return err;
975 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
976 switch (imsg.hdr.type) {
977 case GOT_IMSG_ERROR:
978 if (datalen < sizeof(struct got_imsg_error)) {
979 err = got_error(GOT_ERR_PRIVSEP_LEN);
980 goto done;
982 err = recv_imsg_error(&imsg, datalen);
983 goto done;
984 case GOT_IMSG_SEND_REMOTE_REF:
985 if (datalen < sizeof(iremote_ref)) {
986 err = got_error(GOT_ERR_PRIVSEP_MSG);
987 goto done;
989 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
990 if (datalen != sizeof(iremote_ref) +
991 iremote_ref.name_len) {
992 err = got_error(GOT_ERR_PRIVSEP_MSG);
993 goto done;
995 id = malloc(sizeof(*id));
996 if (id == NULL) {
997 err = got_error_from_errno("malloc");
998 goto done;
1000 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
1001 refname = strndup(imsg.data + sizeof(iremote_ref),
1002 datalen - sizeof(iremote_ref));
1003 if (refname == NULL) {
1004 err = got_error_from_errno("strndup");
1005 goto done;
1007 err = got_pathlist_insert(&new, remote_refs,
1008 refname, id);
1009 if (err)
1010 goto done;
1011 if (new == NULL) { /* duplicate which wasn't inserted */
1012 free(id);
1013 free(refname);
1015 id = NULL;
1016 refname = NULL;
1017 break;
1018 case GOT_IMSG_SEND_PACK_REQUEST:
1019 if (datalen != 0) {
1020 err = got_error(GOT_ERR_PRIVSEP_MSG);
1021 goto done;
1023 /* got-send-pack is now waiting for a pack file. */
1024 done = 1;
1025 break;
1026 default:
1027 err = got_error(GOT_ERR_PRIVSEP_MSG);
1028 break;
1031 done:
1032 free(id);
1033 free(refname);
1034 imsg_free(&imsg);
1035 return err;
1038 const struct got_error *
1039 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
1041 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
1044 const struct got_error *
1045 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1046 int *success, char **refname, struct imsgbuf *ibuf)
1048 const struct got_error *err = NULL;
1049 struct imsg imsg;
1050 size_t datalen;
1051 struct got_imsg_send_ref_status iref_status;
1053 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1054 *done = 0;
1055 *success = 0;
1056 *refname = NULL;
1058 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1059 if (err)
1060 return err;
1062 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1063 switch (imsg.hdr.type) {
1064 case GOT_IMSG_ERROR:
1065 if (datalen < sizeof(struct got_imsg_error)) {
1066 err = got_error(GOT_ERR_PRIVSEP_LEN);
1067 break;
1069 err = recv_imsg_error(&imsg, datalen);
1070 break;
1071 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1072 if (datalen < sizeof(*bytes_sent)) {
1073 err = got_error(GOT_ERR_PRIVSEP_MSG);
1074 break;
1076 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1077 break;
1078 case GOT_IMSG_SEND_REF_STATUS:
1079 if (datalen < sizeof(iref_status)) {
1080 err = got_error(GOT_ERR_PRIVSEP_MSG);
1081 break;
1083 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1084 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1085 err = got_error(GOT_ERR_PRIVSEP_MSG);
1086 break;
1088 *success = iref_status.success;
1089 *refname = strndup(imsg.data + sizeof(iref_status),
1090 iref_status.name_len);
1091 break;
1092 case GOT_IMSG_SEND_DONE:
1093 if (datalen != 0) {
1094 err = got_error(GOT_ERR_PRIVSEP_MSG);
1095 break;
1097 *done = 1;
1098 break;
1099 default:
1100 err = got_error(GOT_ERR_PRIVSEP_MSG);
1101 break;
1104 imsg_free(&imsg);
1105 return err;
1108 const struct got_error *
1109 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1110 int fd)
1112 const struct got_error *err = NULL;
1114 /* Keep in sync with struct got_imsg_index_pack_request */
1115 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1116 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1117 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1118 close(fd);
1119 return err;
1121 return flush_imsg(ibuf);
1124 const struct got_error *
1125 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1127 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1130 const struct got_error *
1131 got_privsep_recv_index_progress(int *done, int *nobj_total,
1132 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1133 struct imsgbuf *ibuf)
1135 const struct got_error *err = NULL;
1136 struct imsg imsg;
1137 struct got_imsg_index_pack_progress *iprogress;
1138 size_t datalen;
1140 *done = 0;
1141 *nobj_total = 0;
1142 *nobj_indexed = 0;
1143 *nobj_resolved = 0;
1145 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1146 if (err)
1147 return err;
1149 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1150 switch (imsg.hdr.type) {
1151 case GOT_IMSG_ERROR:
1152 if (datalen < sizeof(struct got_imsg_error)) {
1153 err = got_error(GOT_ERR_PRIVSEP_LEN);
1154 break;
1156 err = recv_imsg_error(&imsg, datalen);
1157 break;
1158 case GOT_IMSG_IDXPACK_PROGRESS:
1159 if (datalen < sizeof(*iprogress)) {
1160 err = got_error(GOT_ERR_PRIVSEP_LEN);
1161 break;
1163 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1164 *nobj_total = iprogress->nobj_total;
1165 *nobj_indexed = iprogress->nobj_indexed;
1166 *nobj_loose = iprogress->nobj_loose;
1167 *nobj_resolved = iprogress->nobj_resolved;
1168 break;
1169 case GOT_IMSG_IDXPACK_DONE:
1170 if (datalen != 0) {
1171 err = got_error(GOT_ERR_PRIVSEP_LEN);
1172 break;
1174 *done = 1;
1175 break;
1176 default:
1177 err = got_error(GOT_ERR_PRIVSEP_MSG);
1178 break;
1181 imsg_free(&imsg);
1182 return err;
1185 const struct got_error *
1186 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1187 struct imsgbuf *ibuf)
1189 struct got_imsg_object *iobj;
1190 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1192 if (datalen != sizeof(*iobj))
1193 return got_error(GOT_ERR_PRIVSEP_LEN);
1194 iobj = imsg->data;
1196 *obj = calloc(1, sizeof(**obj));
1197 if (*obj == NULL)
1198 return got_error_from_errno("calloc");
1200 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1201 (*obj)->type = iobj->type;
1202 (*obj)->flags = iobj->flags;
1203 (*obj)->hdrlen = iobj->hdrlen;
1204 (*obj)->size = iobj->size;
1205 /* path_packfile is handled by caller */
1206 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1207 (*obj)->pack_offset = iobj->pack_offset;
1208 (*obj)->pack_idx = iobj->pack_idx;
1210 STAILQ_INIT(&(*obj)->deltas.entries);
1211 return NULL;
1214 const struct got_error *
1215 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1217 const struct got_error *err = NULL;
1218 struct imsg imsg;
1219 const size_t min_datalen =
1220 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1222 *obj = NULL;
1224 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1225 if (err)
1226 return err;
1228 switch (imsg.hdr.type) {
1229 case GOT_IMSG_OBJECT:
1230 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1231 break;
1232 default:
1233 err = got_error(GOT_ERR_PRIVSEP_MSG);
1234 break;
1237 imsg_free(&imsg);
1239 return err;
1242 static const struct got_error *
1243 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1244 size_t logmsg_len)
1246 const struct got_error *err = NULL;
1247 size_t offset, remain;
1249 offset = 0;
1250 remain = logmsg_len;
1251 while (remain > 0) {
1252 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1254 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1255 commit->logmsg + offset, n) == -1) {
1256 err = got_error_from_errno("imsg_compose "
1257 "COMMIT_LOGMSG");
1258 break;
1261 err = flush_imsg(ibuf);
1262 if (err)
1263 break;
1265 offset += n;
1266 remain -= n;
1269 return err;
1272 const struct got_error *
1273 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1275 const struct got_error *err = NULL;
1276 struct got_imsg_commit_object *icommit;
1277 uint8_t *buf;
1278 size_t len, total;
1279 struct got_object_qid *qid;
1280 size_t author_len = strlen(commit->author);
1281 size_t committer_len = strlen(commit->committer);
1282 size_t logmsg_len = strlen(commit->logmsg);
1284 total = sizeof(*icommit) + author_len + committer_len +
1285 commit->nparents * SHA1_DIGEST_LENGTH;
1287 buf = malloc(total);
1288 if (buf == NULL)
1289 return got_error_from_errno("malloc");
1291 icommit = (struct got_imsg_commit_object *)buf;
1292 memcpy(icommit->tree_id, commit->tree_id->sha1,
1293 sizeof(icommit->tree_id));
1294 icommit->author_len = author_len;
1295 icommit->author_time = commit->author_time;
1296 icommit->author_gmtoff = commit->author_gmtoff;
1297 icommit->committer_len = committer_len;
1298 icommit->committer_time = commit->committer_time;
1299 icommit->committer_gmtoff = commit->committer_gmtoff;
1300 icommit->logmsg_len = logmsg_len;
1301 icommit->nparents = commit->nparents;
1303 len = sizeof(*icommit);
1304 memcpy(buf + len, commit->author, author_len);
1305 len += author_len;
1306 memcpy(buf + len, commit->committer, committer_len);
1307 len += committer_len;
1308 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1309 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1310 len += SHA1_DIGEST_LENGTH;
1313 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1314 err = got_error_from_errno("imsg_compose COMMIT");
1315 goto done;
1318 if (logmsg_len == 0 ||
1319 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1320 err = flush_imsg(ibuf);
1321 if (err)
1322 goto done;
1324 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1325 done:
1326 free(buf);
1327 return err;
1330 static const struct got_error *
1331 get_commit_from_imsg(struct got_commit_object **commit,
1332 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1334 const struct got_error *err = NULL;
1335 struct got_imsg_commit_object *icommit;
1336 size_t len = 0;
1337 int i;
1339 if (datalen < sizeof(*icommit))
1340 return got_error(GOT_ERR_PRIVSEP_LEN);
1342 icommit = imsg->data;
1343 if (datalen != sizeof(*icommit) + icommit->author_len +
1344 icommit->committer_len +
1345 icommit->nparents * SHA1_DIGEST_LENGTH)
1346 return got_error(GOT_ERR_PRIVSEP_LEN);
1348 if (icommit->nparents < 0)
1349 return got_error(GOT_ERR_PRIVSEP_LEN);
1351 len += sizeof(*icommit);
1353 *commit = got_object_commit_alloc_partial();
1354 if (*commit == NULL)
1355 return got_error_from_errno(
1356 "got_object_commit_alloc_partial");
1358 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1359 SHA1_DIGEST_LENGTH);
1360 (*commit)->author_time = icommit->author_time;
1361 (*commit)->author_gmtoff = icommit->author_gmtoff;
1362 (*commit)->committer_time = icommit->committer_time;
1363 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1365 if (icommit->author_len == 0) {
1366 (*commit)->author = strdup("");
1367 if ((*commit)->author == NULL) {
1368 err = got_error_from_errno("strdup");
1369 goto done;
1371 } else {
1372 (*commit)->author = malloc(icommit->author_len + 1);
1373 if ((*commit)->author == NULL) {
1374 err = got_error_from_errno("malloc");
1375 goto done;
1377 memcpy((*commit)->author, imsg->data + len,
1378 icommit->author_len);
1379 (*commit)->author[icommit->author_len] = '\0';
1381 len += icommit->author_len;
1383 if (icommit->committer_len == 0) {
1384 (*commit)->committer = strdup("");
1385 if ((*commit)->committer == NULL) {
1386 err = got_error_from_errno("strdup");
1387 goto done;
1389 } else {
1390 (*commit)->committer =
1391 malloc(icommit->committer_len + 1);
1392 if ((*commit)->committer == NULL) {
1393 err = got_error_from_errno("malloc");
1394 goto done;
1396 memcpy((*commit)->committer, imsg->data + len,
1397 icommit->committer_len);
1398 (*commit)->committer[icommit->committer_len] = '\0';
1400 len += icommit->committer_len;
1402 if (icommit->logmsg_len == 0) {
1403 (*commit)->logmsg = strdup("");
1404 if ((*commit)->logmsg == NULL) {
1405 err = got_error_from_errno("strdup");
1406 goto done;
1408 } else {
1409 size_t offset = 0, remain = icommit->logmsg_len;
1411 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1412 if ((*commit)->logmsg == NULL) {
1413 err = got_error_from_errno("malloc");
1414 goto done;
1416 while (remain > 0) {
1417 struct imsg imsg_log;
1418 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1419 remain);
1421 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1422 if (err)
1423 goto done;
1425 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1426 err = got_error(GOT_ERR_PRIVSEP_MSG);
1427 goto done;
1430 memcpy((*commit)->logmsg + offset,
1431 imsg_log.data, n);
1432 imsg_free(&imsg_log);
1433 offset += n;
1434 remain -= n;
1436 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1439 for (i = 0; i < icommit->nparents; i++) {
1440 struct got_object_qid *qid;
1442 err = got_object_qid_alloc_partial(&qid);
1443 if (err)
1444 break;
1445 memcpy(qid->id, imsg->data + len +
1446 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1447 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1448 (*commit)->nparents++;
1450 done:
1451 if (err) {
1452 got_object_commit_close(*commit);
1453 *commit = NULL;
1455 return err;
1458 const struct got_error *
1459 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1461 const struct got_error *err = NULL;
1462 struct imsg imsg;
1463 size_t datalen;
1464 const size_t min_datalen =
1465 MIN(sizeof(struct got_imsg_error),
1466 sizeof(struct got_imsg_commit_object));
1468 *commit = NULL;
1470 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1471 if (err)
1472 return err;
1474 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1476 switch (imsg.hdr.type) {
1477 case GOT_IMSG_COMMIT:
1478 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1479 break;
1480 default:
1481 err = got_error(GOT_ERR_PRIVSEP_MSG);
1482 break;
1485 imsg_free(&imsg);
1487 return err;
1490 const struct got_error *
1491 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1492 int nentries)
1494 const struct got_error *err = NULL;
1495 struct got_imsg_tree_object itree;
1496 struct got_pathlist_entry *pe;
1497 size_t totlen;
1498 int nimsg; /* number of imsg queued in ibuf */
1500 itree.nentries = nentries;
1501 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1502 == -1)
1503 return got_error_from_errno("imsg_compose TREE");
1505 totlen = sizeof(itree);
1506 nimsg = 1;
1507 TAILQ_FOREACH(pe, entries, entry) {
1508 const char *name = pe->path;
1509 struct got_parsed_tree_entry *pte = pe->data;
1510 struct ibuf *wbuf;
1511 size_t namelen = strlen(name);
1512 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1514 if (len > MAX_IMSGSIZE)
1515 return got_error(GOT_ERR_NO_SPACE);
1517 nimsg++;
1518 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1519 err = flush_imsg(ibuf);
1520 if (err)
1521 return err;
1522 nimsg = 0;
1525 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1526 if (wbuf == NULL)
1527 return got_error_from_errno("imsg_create TREE_ENTRY");
1529 /* Keep in sync with struct got_imsg_tree_object definition! */
1530 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1531 err = got_error_from_errno("imsg_add TREE_ENTRY");
1532 ibuf_free(wbuf);
1533 return err;
1535 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1536 err = got_error_from_errno("imsg_add TREE_ENTRY");
1537 ibuf_free(wbuf);
1538 return err;
1541 if (imsg_add(wbuf, name, namelen) == -1) {
1542 err = got_error_from_errno("imsg_add TREE_ENTRY");
1543 ibuf_free(wbuf);
1544 return err;
1547 wbuf->fd = -1;
1548 imsg_close(ibuf, wbuf);
1550 totlen += len;
1553 return flush_imsg(ibuf);
1556 const struct got_error *
1557 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1559 const struct got_error *err = NULL;
1560 const size_t min_datalen =
1561 MIN(sizeof(struct got_imsg_error),
1562 sizeof(struct got_imsg_tree_object));
1563 struct got_imsg_tree_object *itree;
1564 int nentries = 0;
1566 *tree = NULL;
1567 get_more:
1568 err = read_imsg(ibuf);
1569 if (err)
1570 goto done;
1572 for (;;) {
1573 struct imsg imsg;
1574 size_t n;
1575 size_t datalen;
1576 struct got_imsg_tree_entry *ite;
1577 struct got_tree_entry *te = NULL;
1579 n = imsg_get(ibuf, &imsg);
1580 if (n == 0) {
1581 if (*tree && (*tree)->nentries != nentries)
1582 goto get_more;
1583 break;
1586 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1587 imsg_free(&imsg);
1588 err = got_error(GOT_ERR_PRIVSEP_LEN);
1589 break;
1592 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1594 switch (imsg.hdr.type) {
1595 case GOT_IMSG_ERROR:
1596 err = recv_imsg_error(&imsg, datalen);
1597 break;
1598 case GOT_IMSG_TREE:
1599 /* This message should only appear once. */
1600 if (*tree != NULL) {
1601 err = got_error(GOT_ERR_PRIVSEP_MSG);
1602 break;
1604 if (datalen != sizeof(*itree)) {
1605 err = got_error(GOT_ERR_PRIVSEP_LEN);
1606 break;
1608 itree = imsg.data;
1609 *tree = malloc(sizeof(**tree));
1610 if (*tree == NULL) {
1611 err = got_error_from_errno("malloc");
1612 break;
1614 (*tree)->entries = calloc(itree->nentries,
1615 sizeof(struct got_tree_entry));
1616 if ((*tree)->entries == NULL) {
1617 err = got_error_from_errno("malloc");
1618 break;
1620 (*tree)->nentries = itree->nentries;
1621 (*tree)->refcnt = 0;
1622 break;
1623 case GOT_IMSG_TREE_ENTRY:
1624 /* This message should be preceeded by GOT_IMSG_TREE. */
1625 if (*tree == NULL) {
1626 err = got_error(GOT_ERR_PRIVSEP_MSG);
1627 break;
1629 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1630 err = got_error(GOT_ERR_PRIVSEP_LEN);
1631 break;
1634 /* Remaining data contains the entry's name. */
1635 datalen -= sizeof(*ite);
1636 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1637 err = got_error(GOT_ERR_PRIVSEP_LEN);
1638 break;
1640 ite = imsg.data;
1642 if (datalen + 1 > sizeof(te->name)) {
1643 err = got_error(GOT_ERR_NO_SPACE);
1644 break;
1646 te = &(*tree)->entries[nentries];
1647 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1648 te->name[datalen] = '\0';
1650 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1651 te->mode = ite->mode;
1652 te->idx = nentries;
1653 nentries++;
1654 break;
1655 default:
1656 err = got_error(GOT_ERR_PRIVSEP_MSG);
1657 break;
1660 imsg_free(&imsg);
1661 if (err)
1662 break;
1664 done:
1665 if (*tree && (*tree)->nentries != nentries) {
1666 if (err == NULL)
1667 err = got_error(GOT_ERR_PRIVSEP_LEN);
1668 got_object_tree_close(*tree);
1669 *tree = NULL;
1672 return err;
1675 const struct got_error *
1676 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1677 const uint8_t *data)
1679 struct got_imsg_blob iblob;
1681 iblob.size = size;
1682 iblob.hdrlen = hdrlen;
1684 if (data) {
1685 uint8_t *buf;
1687 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1688 return got_error(GOT_ERR_NO_SPACE);
1690 buf = malloc(sizeof(iblob) + size);
1691 if (buf == NULL)
1692 return got_error_from_errno("malloc");
1694 memcpy(buf, &iblob, sizeof(iblob));
1695 memcpy(buf + sizeof(iblob), data, size);
1696 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1697 sizeof(iblob) + size) == -1) {
1698 free(buf);
1699 return got_error_from_errno("imsg_compose BLOB");
1701 free(buf);
1702 } else {
1703 /* Data has already been written to file descriptor. */
1704 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1705 sizeof(iblob)) == -1)
1706 return got_error_from_errno("imsg_compose BLOB");
1710 return flush_imsg(ibuf);
1713 const struct got_error *
1714 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1715 struct imsgbuf *ibuf)
1717 const struct got_error *err = NULL;
1718 struct imsg imsg;
1719 struct got_imsg_blob *iblob;
1720 size_t datalen;
1722 *outbuf = NULL;
1724 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1725 if (err)
1726 return err;
1728 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1730 switch (imsg.hdr.type) {
1731 case GOT_IMSG_BLOB:
1732 if (datalen < sizeof(*iblob)) {
1733 err = got_error(GOT_ERR_PRIVSEP_LEN);
1734 break;
1736 iblob = imsg.data;
1737 *size = iblob->size;
1738 *hdrlen = iblob->hdrlen;
1740 if (datalen == sizeof(*iblob)) {
1741 /* Data has been written to file descriptor. */
1742 break;
1745 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1746 err = got_error(GOT_ERR_PRIVSEP_LEN);
1747 break;
1750 *outbuf = malloc(*size);
1751 if (*outbuf == NULL) {
1752 err = got_error_from_errno("malloc");
1753 break;
1755 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1756 break;
1757 default:
1758 err = got_error(GOT_ERR_PRIVSEP_MSG);
1759 break;
1762 imsg_free(&imsg);
1764 return err;
1767 static const struct got_error *
1768 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1770 const struct got_error *err = NULL;
1771 size_t offset, remain;
1773 offset = 0;
1774 remain = tagmsg_len;
1775 while (remain > 0) {
1776 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1778 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1779 tag->tagmsg + offset, n) == -1) {
1780 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1781 break;
1784 err = flush_imsg(ibuf);
1785 if (err)
1786 break;
1788 offset += n;
1789 remain -= n;
1792 return err;
1795 const struct got_error *
1796 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1798 const struct got_error *err = NULL;
1799 struct got_imsg_tag_object *itag;
1800 uint8_t *buf;
1801 size_t len, total;
1802 size_t tag_len = strlen(tag->tag);
1803 size_t tagger_len = strlen(tag->tagger);
1804 size_t tagmsg_len = strlen(tag->tagmsg);
1806 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1808 buf = malloc(total);
1809 if (buf == NULL)
1810 return got_error_from_errno("malloc");
1812 itag = (struct got_imsg_tag_object *)buf;
1813 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1814 itag->obj_type = tag->obj_type;
1815 itag->tag_len = tag_len;
1816 itag->tagger_len = tagger_len;
1817 itag->tagger_time = tag->tagger_time;
1818 itag->tagger_gmtoff = tag->tagger_gmtoff;
1819 itag->tagmsg_len = tagmsg_len;
1821 len = sizeof(*itag);
1822 memcpy(buf + len, tag->tag, tag_len);
1823 len += tag_len;
1824 memcpy(buf + len, tag->tagger, tagger_len);
1825 len += tagger_len;
1827 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1828 err = got_error_from_errno("imsg_compose TAG");
1829 goto done;
1832 if (tagmsg_len == 0 ||
1833 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1834 err = flush_imsg(ibuf);
1835 if (err)
1836 goto done;
1838 err = send_tagmsg(ibuf, tag, tagmsg_len);
1839 done:
1840 free(buf);
1841 return err;
1844 const struct got_error *
1845 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1847 const struct got_error *err = NULL;
1848 struct imsg imsg;
1849 struct got_imsg_tag_object *itag;
1850 size_t len, datalen;
1851 const size_t min_datalen =
1852 MIN(sizeof(struct got_imsg_error),
1853 sizeof(struct got_imsg_tag_object));
1855 *tag = NULL;
1857 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1858 if (err)
1859 return err;
1861 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1862 len = 0;
1864 switch (imsg.hdr.type) {
1865 case GOT_IMSG_TAG:
1866 if (datalen < sizeof(*itag)) {
1867 err = got_error(GOT_ERR_PRIVSEP_LEN);
1868 break;
1870 itag = imsg.data;
1871 if (datalen != sizeof(*itag) + itag->tag_len +
1872 itag->tagger_len) {
1873 err = got_error(GOT_ERR_PRIVSEP_LEN);
1874 break;
1876 len += sizeof(*itag);
1878 *tag = calloc(1, sizeof(**tag));
1879 if (*tag == NULL) {
1880 err = got_error_from_errno("calloc");
1881 break;
1884 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1886 if (itag->tag_len == 0) {
1887 (*tag)->tag = strdup("");
1888 if ((*tag)->tag == NULL) {
1889 err = got_error_from_errno("strdup");
1890 break;
1892 } else {
1893 (*tag)->tag = malloc(itag->tag_len + 1);
1894 if ((*tag)->tag == NULL) {
1895 err = got_error_from_errno("malloc");
1896 break;
1898 memcpy((*tag)->tag, imsg.data + len,
1899 itag->tag_len);
1900 (*tag)->tag[itag->tag_len] = '\0';
1902 len += itag->tag_len;
1904 (*tag)->obj_type = itag->obj_type;
1905 (*tag)->tagger_time = itag->tagger_time;
1906 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1908 if (itag->tagger_len == 0) {
1909 (*tag)->tagger = strdup("");
1910 if ((*tag)->tagger == NULL) {
1911 err = got_error_from_errno("strdup");
1912 break;
1914 } else {
1915 (*tag)->tagger = malloc(itag->tagger_len + 1);
1916 if ((*tag)->tagger == NULL) {
1917 err = got_error_from_errno("malloc");
1918 break;
1920 memcpy((*tag)->tagger, imsg.data + len,
1921 itag->tagger_len);
1922 (*tag)->tagger[itag->tagger_len] = '\0';
1924 len += itag->tagger_len;
1926 if (itag->tagmsg_len == 0) {
1927 (*tag)->tagmsg = strdup("");
1928 if ((*tag)->tagmsg == NULL) {
1929 err = got_error_from_errno("strdup");
1930 break;
1932 } else {
1933 size_t offset = 0, remain = itag->tagmsg_len;
1935 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1936 if ((*tag)->tagmsg == NULL) {
1937 err = got_error_from_errno("malloc");
1938 break;
1940 while (remain > 0) {
1941 struct imsg imsg_log;
1942 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1943 remain);
1945 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1946 if (err)
1947 return err;
1949 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1950 return got_error(GOT_ERR_PRIVSEP_MSG);
1952 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1953 n);
1954 imsg_free(&imsg_log);
1955 offset += n;
1956 remain -= n;
1958 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1961 break;
1962 default:
1963 err = got_error(GOT_ERR_PRIVSEP_MSG);
1964 break;
1967 imsg_free(&imsg);
1969 return err;
1972 const struct got_error *
1973 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1974 struct got_packidx *packidx)
1976 const struct got_error *err = NULL;
1977 struct got_imsg_packidx ipackidx;
1978 struct got_imsg_pack ipack;
1979 int fd;
1981 ipackidx.len = packidx->len;
1982 ipackidx.packfile_size = pack->filesize;
1983 fd = dup(packidx->fd);
1984 if (fd == -1)
1985 return got_error_from_errno("dup");
1987 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1988 sizeof(ipackidx)) == -1) {
1989 err = got_error_from_errno("imsg_compose PACKIDX");
1990 close(fd);
1991 return err;
1994 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1995 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1996 return got_error(GOT_ERR_NO_SPACE);
1997 ipack.filesize = pack->filesize;
1999 fd = dup(pack->fd);
2000 if (fd == -1)
2001 return got_error_from_errno("dup");
2003 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2004 == -1) {
2005 err = got_error_from_errno("imsg_compose PACK");
2006 close(fd);
2007 return err;
2010 return flush_imsg(ibuf);
2013 const struct got_error *
2014 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2015 struct got_object_id *id)
2017 struct got_imsg_packed_object iobj;
2019 iobj.idx = idx;
2020 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2022 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2023 &iobj, sizeof(iobj)) == -1)
2024 return got_error_from_errno("imsg_compose "
2025 "PACKED_OBJECT_REQUEST");
2027 return flush_imsg(ibuf);
2030 const struct got_error *
2031 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2032 struct got_object_id *id)
2034 struct got_imsg_packed_object iobj;
2036 iobj.idx = idx;
2037 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2039 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2040 &iobj, sizeof(iobj)) == -1)
2041 return got_error_from_errno("imsg_compose "
2042 "PACKED_OBJECT_REQUEST");
2044 return flush_imsg(ibuf);
2047 const struct got_error *
2048 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2050 const struct got_error *err = NULL;
2052 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2053 NULL, 0) == -1) {
2054 err = got_error_from_errno("imsg_compose "
2055 "GITCONFIG_PARSE_REQUEST");
2056 close(fd);
2057 return err;
2060 return flush_imsg(ibuf);
2063 const struct got_error *
2064 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2066 if (imsg_compose(ibuf,
2067 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2068 NULL, 0) == -1)
2069 return got_error_from_errno("imsg_compose "
2070 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2072 return flush_imsg(ibuf);
2075 const struct got_error *
2076 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2078 if (imsg_compose(ibuf,
2079 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2080 NULL, 0) == -1)
2081 return got_error_from_errno("imsg_compose "
2082 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2084 return flush_imsg(ibuf);
2088 const struct got_error *
2089 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2091 if (imsg_compose(ibuf,
2092 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2093 return got_error_from_errno("imsg_compose "
2094 "GITCONFIG_AUTHOR_NAME_REQUEST");
2096 return flush_imsg(ibuf);
2099 const struct got_error *
2100 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2102 if (imsg_compose(ibuf,
2103 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2104 return got_error_from_errno("imsg_compose "
2105 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2107 return flush_imsg(ibuf);
2110 const struct got_error *
2111 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2113 if (imsg_compose(ibuf,
2114 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2115 return got_error_from_errno("imsg_compose "
2116 "GITCONFIG_REMOTE_REQUEST");
2118 return flush_imsg(ibuf);
2121 const struct got_error *
2122 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2124 if (imsg_compose(ibuf,
2125 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2126 return got_error_from_errno("imsg_compose "
2127 "GITCONFIG_OWNER_REQUEST");
2129 return flush_imsg(ibuf);
2132 const struct got_error *
2133 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2135 const struct got_error *err = NULL;
2136 struct imsg imsg;
2137 size_t datalen;
2138 const size_t min_datalen = 0;
2140 *str = NULL;
2142 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2143 if (err)
2144 return err;
2145 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2147 switch (imsg.hdr.type) {
2148 case GOT_IMSG_GITCONFIG_STR_VAL:
2149 if (datalen == 0)
2150 break;
2151 /* datalen does not include terminating \0 */
2152 *str = malloc(datalen + 1);
2153 if (*str == NULL) {
2154 err = got_error_from_errno("malloc");
2155 break;
2157 memcpy(*str, imsg.data, datalen);
2158 (*str)[datalen] = '\0';
2159 break;
2160 default:
2161 err = got_error(GOT_ERR_PRIVSEP_MSG);
2162 break;
2165 imsg_free(&imsg);
2166 return err;
2169 const struct got_error *
2170 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2172 const struct got_error *err = NULL;
2173 struct imsg imsg;
2174 size_t datalen;
2175 const size_t min_datalen =
2176 MIN(sizeof(struct got_imsg_error), sizeof(int));
2178 *val = 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_GITCONFIG_INT_VAL:
2187 if (datalen != sizeof(*val)) {
2188 err = got_error(GOT_ERR_PRIVSEP_LEN);
2189 break;
2191 memcpy(val, imsg.data, sizeof(*val));
2192 break;
2193 default:
2194 err = got_error(GOT_ERR_PRIVSEP_MSG);
2195 break;
2198 imsg_free(&imsg);
2199 return err;
2202 static void
2203 free_remote_data(struct got_remote_repo *remote)
2205 int i;
2207 free(remote->name);
2208 free(remote->fetch_url);
2209 free(remote->send_url);
2210 for (i = 0; i < remote->nfetch_branches; i++)
2211 free(remote->fetch_branches[i]);
2212 free(remote->fetch_branches);
2213 for (i = 0; i < remote->nsend_branches; i++)
2214 free(remote->send_branches[i]);
2215 free(remote->send_branches);
2216 for (i = 0; i < remote->nfetch_refs; i++)
2217 free(remote->fetch_refs[i]);
2218 free(remote->fetch_refs);
2221 const struct got_error *
2222 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2223 int *nremotes, struct imsgbuf *ibuf)
2225 const struct got_error *err = NULL;
2226 struct imsg imsg;
2227 size_t datalen;
2228 struct got_imsg_remotes iremotes;
2229 struct got_imsg_remote iremote;
2231 *remotes = NULL;
2232 *nremotes = 0;
2233 iremotes.nremotes = 0;
2235 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2236 if (err)
2237 return err;
2238 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2240 switch (imsg.hdr.type) {
2241 case GOT_IMSG_GITCONFIG_REMOTES:
2242 if (datalen != sizeof(iremotes)) {
2243 err = got_error(GOT_ERR_PRIVSEP_LEN);
2244 break;
2246 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2247 if (iremotes.nremotes == 0) {
2248 imsg_free(&imsg);
2249 return NULL;
2251 break;
2252 default:
2253 imsg_free(&imsg);
2254 return got_error(GOT_ERR_PRIVSEP_MSG);
2257 imsg_free(&imsg);
2259 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2260 if (*remotes == NULL)
2261 return got_error_from_errno("recallocarray");
2263 while (*nremotes < iremotes.nremotes) {
2264 struct got_remote_repo *remote;
2266 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2267 if (err)
2268 break;
2269 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2271 switch (imsg.hdr.type) {
2272 case GOT_IMSG_GITCONFIG_REMOTE:
2273 remote = &(*remotes)[*nremotes];
2274 memset(remote, 0, sizeof(*remote));
2275 if (datalen < sizeof(iremote)) {
2276 err = got_error(GOT_ERR_PRIVSEP_LEN);
2277 break;
2279 memcpy(&iremote, imsg.data, sizeof(iremote));
2280 if (iremote.name_len == 0 ||
2281 iremote.fetch_url_len == 0 ||
2282 iremote.send_url_len == 0 ||
2283 (sizeof(iremote) + iremote.name_len +
2284 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2285 err = got_error(GOT_ERR_PRIVSEP_LEN);
2286 break;
2288 remote->name = strndup(imsg.data + sizeof(iremote),
2289 iremote.name_len);
2290 if (remote->name == NULL) {
2291 err = got_error_from_errno("strndup");
2292 break;
2294 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2295 iremote.name_len, iremote.fetch_url_len);
2296 if (remote->fetch_url == NULL) {
2297 err = got_error_from_errno("strndup");
2298 free_remote_data(remote);
2299 break;
2301 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2302 iremote.name_len + iremote.fetch_url_len,
2303 iremote.send_url_len);
2304 if (remote->send_url == NULL) {
2305 err = got_error_from_errno("strndup");
2306 free_remote_data(remote);
2307 break;
2309 remote->mirror_references = iremote.mirror_references;
2310 remote->fetch_all_branches = iremote.fetch_all_branches;
2311 remote->nfetch_branches = 0;
2312 remote->fetch_branches = NULL;
2313 remote->nsend_branches = 0;
2314 remote->send_branches = NULL;
2315 remote->nfetch_refs = 0;
2316 remote->fetch_refs = NULL;
2317 (*nremotes)++;
2318 break;
2319 default:
2320 err = got_error(GOT_ERR_PRIVSEP_MSG);
2321 break;
2324 imsg_free(&imsg);
2325 if (err)
2326 break;
2329 if (err) {
2330 int i;
2331 for (i = 0; i < *nremotes; i++)
2332 free_remote_data(&(*remotes)[i]);
2333 free(*remotes);
2334 *remotes = NULL;
2335 *nremotes = 0;
2337 return err;
2340 const struct got_error *
2341 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2343 const struct got_error *err = NULL;
2345 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2346 NULL, 0) == -1) {
2347 err = got_error_from_errno("imsg_compose "
2348 "GOTCONFIG_PARSE_REQUEST");
2349 close(fd);
2350 return err;
2353 return flush_imsg(ibuf);
2356 const struct got_error *
2357 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2359 if (imsg_compose(ibuf,
2360 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2361 return got_error_from_errno("imsg_compose "
2362 "GOTCONFIG_AUTHOR_REQUEST");
2364 return flush_imsg(ibuf);
2367 const struct got_error *
2368 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2370 if (imsg_compose(ibuf,
2371 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2372 return got_error_from_errno("imsg_compose "
2373 "GOTCONFIG_REMOTE_REQUEST");
2375 return flush_imsg(ibuf);
2378 const struct got_error *
2379 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2381 const struct got_error *err = NULL;
2382 struct imsg imsg;
2383 size_t datalen;
2384 const size_t min_datalen = 0;
2386 *str = NULL;
2388 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2389 if (err)
2390 return err;
2391 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2393 switch (imsg.hdr.type) {
2394 case GOT_IMSG_ERROR:
2395 if (datalen < sizeof(struct got_imsg_error)) {
2396 err = got_error(GOT_ERR_PRIVSEP_LEN);
2397 break;
2399 err = recv_imsg_error(&imsg, datalen);
2400 break;
2401 case GOT_IMSG_GOTCONFIG_STR_VAL:
2402 if (datalen == 0)
2403 break;
2404 /* datalen does not include terminating \0 */
2405 *str = malloc(datalen + 1);
2406 if (*str == NULL) {
2407 err = got_error_from_errno("malloc");
2408 break;
2410 memcpy(*str, imsg.data, datalen);
2411 (*str)[datalen] = '\0';
2412 break;
2413 default:
2414 err = got_error(GOT_ERR_PRIVSEP_MSG);
2415 break;
2418 imsg_free(&imsg);
2419 return err;
2422 const struct got_error *
2423 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2424 int *nremotes, struct imsgbuf *ibuf)
2426 const struct got_error *err = NULL;
2427 struct imsg imsg;
2428 size_t datalen;
2429 struct got_imsg_remotes iremotes;
2430 struct got_imsg_remote iremote;
2431 const size_t min_datalen =
2432 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2434 *remotes = NULL;
2435 *nremotes = 0;
2436 iremotes.nremotes = 0;
2438 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2439 if (err)
2440 return err;
2441 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2443 switch (imsg.hdr.type) {
2444 case GOT_IMSG_ERROR:
2445 if (datalen < sizeof(struct got_imsg_error)) {
2446 err = got_error(GOT_ERR_PRIVSEP_LEN);
2447 break;
2449 err = recv_imsg_error(&imsg, datalen);
2450 break;
2451 case GOT_IMSG_GOTCONFIG_REMOTES:
2452 if (datalen != sizeof(iremotes)) {
2453 err = got_error(GOT_ERR_PRIVSEP_LEN);
2454 break;
2456 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2457 if (iremotes.nremotes == 0) {
2458 imsg_free(&imsg);
2459 return NULL;
2461 break;
2462 default:
2463 imsg_free(&imsg);
2464 return got_error(GOT_ERR_PRIVSEP_MSG);
2467 imsg_free(&imsg);
2469 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2470 if (*remotes == NULL)
2471 return got_error_from_errno("recallocarray");
2473 while (*nremotes < iremotes.nremotes) {
2474 struct got_remote_repo *remote;
2475 const size_t min_datalen =
2476 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2477 int i;
2479 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2480 if (err)
2481 break;
2482 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2484 switch (imsg.hdr.type) {
2485 case GOT_IMSG_ERROR:
2486 if (datalen < sizeof(struct got_imsg_error)) {
2487 err = got_error(GOT_ERR_PRIVSEP_LEN);
2488 break;
2490 err = recv_imsg_error(&imsg, datalen);
2491 break;
2492 case GOT_IMSG_GOTCONFIG_REMOTE:
2493 remote = &(*remotes)[*nremotes];
2494 memset(remote, 0, sizeof(*remote));
2495 if (datalen < sizeof(iremote)) {
2496 err = got_error(GOT_ERR_PRIVSEP_LEN);
2497 break;
2499 memcpy(&iremote, imsg.data, sizeof(iremote));
2500 if (iremote.name_len == 0 ||
2501 (iremote.fetch_url_len == 0 &&
2502 iremote.send_url_len == 0) ||
2503 (sizeof(iremote) + iremote.name_len +
2504 iremote.fetch_url_len + iremote.send_url_len) >
2505 datalen) {
2506 err = got_error(GOT_ERR_PRIVSEP_LEN);
2507 break;
2509 remote->name = strndup(imsg.data + sizeof(iremote),
2510 iremote.name_len);
2511 if (remote->name == NULL) {
2512 err = got_error_from_errno("strndup");
2513 break;
2515 remote->fetch_url = strndup(imsg.data +
2516 sizeof(iremote) + iremote.name_len,
2517 iremote.fetch_url_len);
2518 if (remote->fetch_url == NULL) {
2519 err = got_error_from_errno("strndup");
2520 free_remote_data(remote);
2521 break;
2523 remote->send_url = strndup(imsg.data +
2524 sizeof(iremote) + iremote.name_len +
2525 iremote.fetch_url_len, iremote.send_url_len);
2526 if (remote->send_url == NULL) {
2527 err = got_error_from_errno("strndup");
2528 free_remote_data(remote);
2529 break;
2531 remote->mirror_references = iremote.mirror_references;
2532 remote->fetch_all_branches = iremote.fetch_all_branches;
2533 if (iremote.nfetch_branches > 0) {
2534 remote->fetch_branches = recallocarray(NULL, 0,
2535 iremote.nfetch_branches, sizeof(char *));
2536 if (remote->fetch_branches == NULL) {
2537 err = got_error_from_errno("calloc");
2538 free_remote_data(remote);
2539 break;
2542 remote->nfetch_branches = 0;
2543 for (i = 0; i < iremote.nfetch_branches; i++) {
2544 char *branch;
2545 err = got_privsep_recv_gotconfig_str(&branch,
2546 ibuf);
2547 if (err) {
2548 free_remote_data(remote);
2549 goto done;
2551 remote->fetch_branches[i] = branch;
2552 remote->nfetch_branches++;
2554 if (iremote.nsend_branches > 0) {
2555 remote->send_branches = recallocarray(NULL, 0,
2556 iremote.nsend_branches, sizeof(char *));
2557 if (remote->send_branches == NULL) {
2558 err = got_error_from_errno("calloc");
2559 free_remote_data(remote);
2560 break;
2563 remote->nsend_branches = 0;
2564 for (i = 0; i < iremote.nsend_branches; i++) {
2565 char *branch;
2566 err = got_privsep_recv_gotconfig_str(&branch,
2567 ibuf);
2568 if (err) {
2569 free_remote_data(remote);
2570 goto done;
2572 remote->send_branches[i] = branch;
2573 remote->nsend_branches++;
2575 if (iremote.nfetch_refs > 0) {
2576 remote->fetch_refs = recallocarray(NULL, 0,
2577 iremote.nfetch_refs, sizeof(char *));
2578 if (remote->fetch_refs == NULL) {
2579 err = got_error_from_errno("calloc");
2580 free_remote_data(remote);
2581 break;
2584 remote->nfetch_refs = 0;
2585 for (i = 0; i < iremote.nfetch_refs; i++) {
2586 char *ref;
2587 err = got_privsep_recv_gotconfig_str(&ref,
2588 ibuf);
2589 if (err) {
2590 free_remote_data(remote);
2591 goto done;
2593 remote->fetch_refs[i] = ref;
2594 remote->nfetch_refs++;
2596 (*nremotes)++;
2597 break;
2598 default:
2599 err = got_error(GOT_ERR_PRIVSEP_MSG);
2600 break;
2603 imsg_free(&imsg);
2604 if (err)
2605 break;
2607 done:
2608 if (err) {
2609 int i;
2610 for (i = 0; i < *nremotes; i++)
2611 free_remote_data(&(*remotes)[i]);
2612 free(*remotes);
2613 *remotes = NULL;
2614 *nremotes = 0;
2616 return err;
2619 const struct got_error *
2620 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2621 struct got_object_id *id, int idx, const char *path)
2623 const struct got_error *err = NULL;
2624 struct ibuf *wbuf;
2625 size_t path_len = strlen(path) + 1;
2627 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2628 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2629 if (wbuf == NULL)
2630 return got_error_from_errno(
2631 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2632 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2633 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2634 ibuf_free(wbuf);
2635 return err;
2637 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2638 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2639 ibuf_free(wbuf);
2640 return err;
2642 if (imsg_add(wbuf, path, path_len) == -1) {
2643 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2644 ibuf_free(wbuf);
2645 return err;
2648 wbuf->fd = -1;
2649 imsg_close(ibuf, wbuf);
2651 return flush_imsg(ibuf);
2654 const struct got_error *
2655 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2656 struct got_object_id **changed_commit_id,
2657 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2659 const struct got_error *err = NULL;
2660 struct imsg imsg;
2661 struct got_imsg_traversed_commits *icommits;
2662 size_t datalen;
2663 int i, done = 0;
2665 *changed_commit = NULL;
2666 *changed_commit_id = NULL;
2668 while (!done) {
2669 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2670 if (err)
2671 return err;
2673 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2674 switch (imsg.hdr.type) {
2675 case GOT_IMSG_TRAVERSED_COMMITS:
2676 icommits = imsg.data;
2677 if (datalen != sizeof(*icommits) +
2678 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2679 err = got_error(GOT_ERR_PRIVSEP_LEN);
2680 break;
2682 for (i = 0; i < icommits->ncommits; i++) {
2683 struct got_object_qid *qid;
2684 uint8_t *sha1 = (uint8_t *)imsg.data +
2685 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2686 err = got_object_qid_alloc_partial(&qid);
2687 if (err)
2688 break;
2689 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2690 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2692 /* The last commit may contain a change. */
2693 if (i == icommits->ncommits - 1) {
2694 *changed_commit_id =
2695 got_object_id_dup(qid->id);
2696 if (*changed_commit_id == NULL) {
2697 err = got_error_from_errno(
2698 "got_object_id_dup");
2699 break;
2703 break;
2704 case GOT_IMSG_COMMIT:
2705 if (*changed_commit_id == NULL) {
2706 err = got_error(GOT_ERR_PRIVSEP_MSG);
2707 break;
2709 err = get_commit_from_imsg(changed_commit, &imsg,
2710 datalen, ibuf);
2711 break;
2712 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2713 done = 1;
2714 break;
2715 default:
2716 err = got_error(GOT_ERR_PRIVSEP_MSG);
2717 break;
2720 imsg_free(&imsg);
2721 if (err)
2722 break;
2725 if (err)
2726 got_object_id_queue_free(commit_ids);
2727 return err;
2730 const struct got_error *
2731 got_privsep_unveil_exec_helpers(void)
2733 const char *helpers[] = {
2734 GOT_PATH_PROG_READ_PACK,
2735 GOT_PATH_PROG_READ_OBJECT,
2736 GOT_PATH_PROG_READ_COMMIT,
2737 GOT_PATH_PROG_READ_TREE,
2738 GOT_PATH_PROG_READ_BLOB,
2739 GOT_PATH_PROG_READ_TAG,
2740 GOT_PATH_PROG_READ_GITCONFIG,
2741 GOT_PATH_PROG_READ_GOTCONFIG,
2742 GOT_PATH_PROG_FETCH_PACK,
2743 GOT_PATH_PROG_INDEX_PACK,
2744 GOT_PATH_PROG_SEND_PACK,
2746 size_t i;
2748 for (i = 0; i < nitems(helpers); i++) {
2749 if (unveil(helpers[i], "x") == 0)
2750 continue;
2751 return got_error_from_errno2("unveil", helpers[i]);
2754 return NULL;
2757 void
2758 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2760 if (close(imsg_fds[0]) == -1) {
2761 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2762 _exit(1);
2765 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2766 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2767 _exit(1);
2770 closefrom(GOT_IMSG_FD_CHILD + 1);
2772 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2773 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2774 strerror(errno));
2775 _exit(1);