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/syslimits.h>
22 #include <sys/wait.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <stdint.h>
30 #include <poll.h>
31 #include <imsg.h>
32 #include <sha1.h>
33 #include <zlib.h>
34 #include <time.h>
36 #include "got_object.h"
37 #include "got_error.h"
38 #include "got_path.h"
39 #include "got_repository.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_parse.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_pack.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 static const struct got_error *
58 poll_fd(int fd, int events, int timeout)
59 {
60 struct pollfd pfd[1];
61 int n;
63 pfd[0].fd = fd;
64 pfd[0].events = events;
66 n = poll(pfd, 1, timeout);
67 if (n == -1)
68 return got_error_from_errno("poll");
69 if (n == 0)
70 return got_error(GOT_ERR_TIMEOUT);
71 if (pfd[0].revents & (POLLERR | POLLNVAL))
72 return got_error_from_errno("poll error");
73 if (pfd[0].revents & (events | POLLHUP))
74 return NULL;
76 return got_error(GOT_ERR_INTERRUPT);
77 }
79 static const struct got_error *
80 read_imsg(struct imsgbuf *ibuf)
81 {
82 const struct got_error *err;
83 size_t n;
85 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
86 if (err)
87 return err;
89 n = imsg_read(ibuf);
90 if (n == -1) {
91 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
92 return got_error(GOT_ERR_PRIVSEP_NO_FD);
93 return got_error(GOT_ERR_PRIVSEP_READ);
94 }
95 if (n == 0)
96 return got_error(GOT_ERR_PRIVSEP_PIPE);
98 return NULL;
99 }
101 const struct got_error *
102 got_privsep_wait_for_child(pid_t pid)
104 int child_status;
106 if (waitpid(pid, &child_status, 0) == -1)
107 return got_error_from_errno("waitpid");
109 if (!WIFEXITED(child_status))
110 return got_error(GOT_ERR_PRIVSEP_DIED);
112 if (WEXITSTATUS(child_status) != 0)
113 return got_error(GOT_ERR_PRIVSEP_EXIT);
115 return NULL;
118 static const struct got_error *
119 recv_imsg_error(struct imsg *imsg, size_t datalen)
121 struct got_imsg_error *ierr;
123 if (datalen != sizeof(*ierr))
124 return got_error(GOT_ERR_PRIVSEP_LEN);
126 ierr = imsg->data;
127 if (ierr->code == GOT_ERR_ERRNO) {
128 static struct got_error serr;
129 serr.code = GOT_ERR_ERRNO;
130 serr.msg = strerror(ierr->errno_code);
131 return &serr;
134 return got_error(ierr->code);
137 const struct got_error *
138 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
139 size_t min_datalen)
141 const struct got_error *err;
142 ssize_t n;
144 n = imsg_get(ibuf, imsg);
145 if (n == -1)
146 return got_error_from_errno("imsg_get");
148 while (n == 0) {
149 err = read_imsg(ibuf);
150 if (err)
151 return err;
152 n = imsg_get(ibuf, imsg);
153 if (n == -1)
154 return got_error_from_errno("imsg_get");
157 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
158 return got_error(GOT_ERR_PRIVSEP_LEN);
160 if (imsg->hdr.type == GOT_IMSG_ERROR) {
161 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 return recv_imsg_error(imsg, datalen);
165 return NULL;
168 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
169 void
170 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
172 const struct got_error *poll_err;
173 struct got_imsg_error ierr;
174 int ret;
176 ierr.code = err->code;
177 if (err->code == GOT_ERR_ERRNO)
178 ierr.errno_code = errno;
179 else
180 ierr.errno_code = 0;
181 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
182 if (ret == -1) {
183 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
184 getprogname(), err->code, err->msg, strerror(errno));
185 return;
188 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
189 if (poll_err) {
190 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
191 getprogname(), err->code, err->msg, poll_err->msg);
192 return;
195 ret = imsg_flush(ibuf);
196 if (ret == -1) {
197 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
198 getprogname(), err->code, err->msg, strerror(errno));
199 return;
203 static const struct got_error *
204 flush_imsg(struct imsgbuf *ibuf)
206 const struct got_error *err;
208 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
209 if (err)
210 return err;
212 if (imsg_flush(ibuf) == -1)
213 return got_error_from_errno("imsg_flush");
215 return NULL;
218 const struct got_error *
219 got_privsep_flush_imsg(struct imsgbuf *ibuf)
221 return flush_imsg(ibuf);
224 const struct got_error *
225 got_privsep_send_stop(int fd)
227 const struct got_error *err = NULL;
228 struct imsgbuf ibuf;
230 imsg_init(&ibuf, fd);
232 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
233 return got_error_from_errno("imsg_compose STOP");
235 err = flush_imsg(&ibuf);
236 imsg_clear(&ibuf);
237 return err;
240 const struct got_error *
241 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
243 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
244 == -1)
245 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
247 return flush_imsg(ibuf);
250 const struct got_error *
251 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
252 struct got_object_id *id, int pack_idx)
254 const struct got_error *err = NULL;
255 struct got_imsg_packed_object iobj, *iobjp;
256 size_t len;
258 if (id) { /* commit is packed */
259 iobj.idx = pack_idx;
260 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
261 iobjp = &iobj;
262 len = sizeof(iobj);
263 } else {
264 iobjp = NULL;
265 len = 0;
268 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
269 == -1) {
270 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
271 close(fd);
272 return err;
275 return flush_imsg(ibuf);
278 const struct got_error *
279 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
280 struct got_object_id *id, int pack_idx)
282 const struct got_error *err = NULL;
283 struct ibuf *wbuf;
284 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
286 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
287 if (wbuf == NULL)
288 return got_error_from_errno("imsg_create TREE_REQUEST");
290 if (id) { /* tree is packed */
291 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
292 err = got_error_from_errno("imsg_add TREE_ENTRY");
293 ibuf_free(wbuf);
294 return err;
297 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
298 err = got_error_from_errno("imsg_add TREE_ENTRY");
299 ibuf_free(wbuf);
300 return err;
304 wbuf->fd = fd;
305 imsg_close(ibuf, wbuf);
307 return flush_imsg(ibuf);
310 const struct got_error *
311 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
312 struct got_object_id *id, int pack_idx)
314 struct got_imsg_packed_object iobj, *iobjp;
315 size_t len;
317 if (id) { /* tag is packed */
318 iobj.idx = pack_idx;
319 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
320 iobjp = &iobj;
321 len = sizeof(iobj);
322 } else {
323 iobjp = NULL;
324 len = 0;
327 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
328 == -1)
329 return got_error_from_errno("imsg_compose TAG_REQUEST");
331 return flush_imsg(ibuf);
334 const struct got_error *
335 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
336 struct got_object_id *id, int pack_idx)
338 const struct got_error *err = NULL;
339 struct got_imsg_packed_object iobj, *iobjp;
340 size_t len;
342 if (id) { /* blob is packed */
343 iobj.idx = pack_idx;
344 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
345 iobjp = &iobj;
346 len = sizeof(iobj);
347 } else {
348 iobjp = NULL;
349 len = 0;
352 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
353 == -1) {
354 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
355 close(infd);
356 return err;
359 return flush_imsg(ibuf);
362 const struct got_error *
363 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
365 const struct got_error *err = NULL;
367 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
368 == -1) {
369 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
370 close(outfd);
371 return err;
374 return flush_imsg(ibuf);
377 static const struct got_error *
378 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
380 const struct got_error *err = NULL;
382 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
383 err = got_error_from_errno("imsg_compose TMPFD");
384 close(fd);
385 return err;
388 return flush_imsg(ibuf);
391 const struct got_error *
392 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
394 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
397 const struct got_error *
398 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
400 struct got_imsg_object iobj;
402 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
403 iobj.type = obj->type;
404 iobj.flags = obj->flags;
405 iobj.hdrlen = obj->hdrlen;
406 iobj.size = obj->size;
407 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
408 iobj.pack_offset = obj->pack_offset;
409 iobj.pack_idx = obj->pack_idx;
412 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
413 == -1)
414 return got_error_from_errno("imsg_compose OBJECT");
416 return flush_imsg(ibuf);
419 const struct got_error *
420 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
421 struct got_pathlist_head *have_refs, int fetch_all_branches,
422 struct got_pathlist_head *wanted_branches,
423 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
425 const struct got_error *err = NULL;
426 struct ibuf *wbuf;
427 size_t len;
428 struct got_pathlist_entry *pe;
429 struct got_imsg_fetch_request fetchreq;
431 memset(&fetchreq, 0, sizeof(fetchreq));
432 fetchreq.fetch_all_branches = fetch_all_branches;
433 fetchreq.list_refs_only = list_refs_only;
434 fetchreq.verbosity = verbosity;
435 TAILQ_FOREACH(pe, have_refs, entry)
436 fetchreq.n_have_refs++;
437 TAILQ_FOREACH(pe, wanted_branches, entry)
438 fetchreq.n_wanted_branches++;
439 TAILQ_FOREACH(pe, wanted_refs, entry)
440 fetchreq.n_wanted_refs++;
441 len = sizeof(struct got_imsg_fetch_request);
442 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
443 close(fd);
444 return got_error(GOT_ERR_NO_SPACE);
447 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
448 &fetchreq, sizeof(fetchreq)) == -1)
449 return got_error_from_errno(
450 "imsg_compose FETCH_SERVER_PROGRESS");
452 err = flush_imsg(ibuf);
453 if (err) {
454 close(fd);
455 return err;
457 fd = -1;
459 TAILQ_FOREACH(pe, have_refs, entry) {
460 const char *name = pe->path;
461 size_t name_len = pe->path_len;
462 struct got_object_id *id = pe->data;
464 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
465 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
466 if (wbuf == NULL)
467 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
469 /* Keep in sync with struct got_imsg_fetch_have_ref! */
470 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
471 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
472 ibuf_free(wbuf);
473 return err;
475 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
476 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
477 ibuf_free(wbuf);
478 return err;
480 if (imsg_add(wbuf, name, name_len) == -1) {
481 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
482 ibuf_free(wbuf);
483 return err;
486 wbuf->fd = -1;
487 imsg_close(ibuf, wbuf);
488 err = flush_imsg(ibuf);
489 if (err)
490 return err;
493 TAILQ_FOREACH(pe, wanted_branches, entry) {
494 const char *name = pe->path;
495 size_t name_len = pe->path_len;
497 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
498 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
499 len);
500 if (wbuf == NULL)
501 return got_error_from_errno(
502 "imsg_create FETCH_WANTED_BRANCH");
504 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
505 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
506 err = got_error_from_errno(
507 "imsg_add FETCH_WANTED_BRANCH");
508 ibuf_free(wbuf);
509 return err;
511 if (imsg_add(wbuf, name, name_len) == -1) {
512 err = got_error_from_errno(
513 "imsg_add FETCH_WANTED_BRANCH");
514 ibuf_free(wbuf);
515 return err;
518 wbuf->fd = -1;
519 imsg_close(ibuf, wbuf);
520 err = flush_imsg(ibuf);
521 if (err)
522 return err;
525 TAILQ_FOREACH(pe, wanted_refs, entry) {
526 const char *name = pe->path;
527 size_t name_len = pe->path_len;
529 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
530 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
531 len);
532 if (wbuf == NULL)
533 return got_error_from_errno(
534 "imsg_create FETCH_WANTED_REF");
536 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
537 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
538 err = got_error_from_errno(
539 "imsg_add FETCH_WANTED_REF");
540 ibuf_free(wbuf);
541 return err;
543 if (imsg_add(wbuf, name, name_len) == -1) {
544 err = got_error_from_errno(
545 "imsg_add FETCH_WANTED_REF");
546 ibuf_free(wbuf);
547 return err;
550 wbuf->fd = -1;
551 imsg_close(ibuf, wbuf);
552 err = flush_imsg(ibuf);
553 if (err)
554 return err;
558 return NULL;
562 const struct got_error *
563 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
565 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
568 const struct got_error *
569 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
570 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
571 off_t *packfile_size, struct imsgbuf *ibuf)
573 const struct got_error *err = NULL;
574 struct imsg imsg;
575 size_t datalen;
576 struct got_imsg_fetch_symrefs *isymrefs = NULL;
577 size_t n, remain;
578 off_t off;
579 int i;
581 *done = 0;
582 *id = NULL;
583 *refname = NULL;
584 *server_progress = NULL;
585 *packfile_size = 0;
587 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
588 if (err)
589 return err;
591 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
592 switch (imsg.hdr.type) {
593 case GOT_IMSG_ERROR:
594 if (datalen < sizeof(struct got_imsg_error)) {
595 err = got_error(GOT_ERR_PRIVSEP_LEN);
596 break;
598 err = recv_imsg_error(&imsg, datalen);
599 break;
600 case GOT_IMSG_FETCH_SYMREFS:
601 if (datalen < sizeof(*isymrefs)) {
602 err = got_error(GOT_ERR_PRIVSEP_LEN);
603 break;
605 if (isymrefs != NULL) {
606 err = got_error(GOT_ERR_PRIVSEP_MSG);
607 break;
609 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
610 off = sizeof(*isymrefs);
611 remain = datalen - off;
612 for (n = 0; n < isymrefs->nsymrefs; n++) {
613 struct got_imsg_fetch_symref *s;
614 char *name, *target;
615 if (remain < sizeof(struct got_imsg_fetch_symref)) {
616 err = got_error(GOT_ERR_PRIVSEP_LEN);
617 goto done;
619 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
620 off += sizeof(*s);
621 remain -= sizeof(*s);
622 if (remain < s->name_len) {
623 err = got_error(GOT_ERR_PRIVSEP_LEN);
624 goto done;
626 name = strndup(imsg.data + off, s->name_len);
627 if (name == NULL) {
628 err = got_error_from_errno("strndup");
629 goto done;
631 off += s->name_len;
632 remain -= s->name_len;
633 if (remain < s->target_len) {
634 err = got_error(GOT_ERR_PRIVSEP_LEN);
635 free(name);
636 goto done;
638 target = strndup(imsg.data + off, s->target_len);
639 if (target == NULL) {
640 err = got_error_from_errno("strndup");
641 free(name);
642 goto done;
644 off += s->target_len;
645 remain -= s->target_len;
646 err = got_pathlist_append(symrefs, name, target);
647 if (err) {
648 free(name);
649 free(target);
650 goto done;
653 break;
654 case GOT_IMSG_FETCH_REF:
655 if (datalen <= SHA1_DIGEST_LENGTH) {
656 err = got_error(GOT_ERR_PRIVSEP_MSG);
657 break;
659 *id = malloc(sizeof(**id));
660 if (*id == NULL) {
661 err = got_error_from_errno("malloc");
662 break;
664 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
665 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
666 datalen - SHA1_DIGEST_LENGTH);
667 if (*refname == NULL) {
668 err = got_error_from_errno("strndup");
669 break;
671 break;
672 case GOT_IMSG_FETCH_SERVER_PROGRESS:
673 if (datalen == 0) {
674 err = got_error(GOT_ERR_PRIVSEP_LEN);
675 break;
677 *server_progress = strndup(imsg.data, datalen);
678 if (*server_progress == NULL) {
679 err = got_error_from_errno("strndup");
680 break;
682 for (i = 0; i < datalen; i++) {
683 if (!isprint((unsigned char)(*server_progress)[i]) &&
684 !isspace((unsigned char)(*server_progress)[i])) {
685 err = got_error(GOT_ERR_PRIVSEP_MSG);
686 free(*server_progress);
687 *server_progress = NULL;
688 goto done;
691 break;
692 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
693 if (datalen < sizeof(*packfile_size)) {
694 err = got_error(GOT_ERR_PRIVSEP_MSG);
695 break;
697 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
698 break;
699 case GOT_IMSG_FETCH_DONE:
700 *id = malloc(sizeof(**id));
701 if (*id == NULL) {
702 err = got_error_from_errno("malloc");
703 break;
705 if (datalen != SHA1_DIGEST_LENGTH) {
706 err = got_error(GOT_ERR_PRIVSEP_MSG);
707 break;
709 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
710 *done = 1;
711 break;
712 default:
713 err = got_error(GOT_ERR_PRIVSEP_MSG);
714 break;
716 done:
717 if (err) {
718 free(*id);
719 *id = NULL;
720 free(*refname);
721 *refname = NULL;
723 imsg_free(&imsg);
724 return err;
727 const struct got_error *
728 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
729 int fd)
731 const struct got_error *err = NULL;
733 /* Keep in sync with struct got_imsg_index_pack_request */
734 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
735 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
736 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
737 close(fd);
738 return err;
740 return flush_imsg(ibuf);
743 const struct got_error *
744 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
746 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
749 const struct got_error *
750 got_privsep_recv_index_progress(int *done, int *nobj_total,
751 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
752 struct imsgbuf *ibuf)
754 const struct got_error *err = NULL;
755 struct imsg imsg;
756 struct got_imsg_index_pack_progress *iprogress;
757 size_t datalen;
759 *done = 0;
760 *nobj_total = 0;
761 *nobj_indexed = 0;
762 *nobj_resolved = 0;
764 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
765 if (err)
766 return err;
768 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
769 switch (imsg.hdr.type) {
770 case GOT_IMSG_ERROR:
771 if (datalen < sizeof(struct got_imsg_error)) {
772 err = got_error(GOT_ERR_PRIVSEP_LEN);
773 break;
775 err = recv_imsg_error(&imsg, datalen);
776 break;
777 case GOT_IMSG_IDXPACK_PROGRESS:
778 if (datalen < sizeof(*iprogress)) {
779 err = got_error(GOT_ERR_PRIVSEP_LEN);
780 break;
782 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
783 *nobj_total = iprogress->nobj_total;
784 *nobj_indexed = iprogress->nobj_indexed;
785 *nobj_loose = iprogress->nobj_loose;
786 *nobj_resolved = iprogress->nobj_resolved;
787 break;
788 case GOT_IMSG_IDXPACK_DONE:
789 if (datalen != 0) {
790 err = got_error(GOT_ERR_PRIVSEP_LEN);
791 break;
793 *done = 1;
794 break;
795 default:
796 err = got_error(GOT_ERR_PRIVSEP_MSG);
797 break;
800 imsg_free(&imsg);
801 return err;
804 const struct got_error *
805 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
806 struct imsgbuf *ibuf)
808 const struct got_error *err = NULL;
809 struct got_imsg_object *iobj;
810 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
812 if (datalen != sizeof(*iobj))
813 return got_error(GOT_ERR_PRIVSEP_LEN);
814 iobj = imsg->data;
816 *obj = calloc(1, sizeof(**obj));
817 if (*obj == NULL)
818 return got_error_from_errno("calloc");
820 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
821 (*obj)->type = iobj->type;
822 (*obj)->flags = iobj->flags;
823 (*obj)->hdrlen = iobj->hdrlen;
824 (*obj)->size = iobj->size;
825 /* path_packfile is handled by caller */
826 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
827 (*obj)->pack_offset = iobj->pack_offset;
828 (*obj)->pack_idx = iobj->pack_idx;
831 return err;
834 const struct got_error *
835 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
837 const struct got_error *err = NULL;
838 struct imsg imsg;
839 const size_t min_datalen =
840 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
842 *obj = NULL;
844 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
845 if (err)
846 return err;
848 switch (imsg.hdr.type) {
849 case GOT_IMSG_OBJECT:
850 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
851 break;
852 default:
853 err = got_error(GOT_ERR_PRIVSEP_MSG);
854 break;
857 imsg_free(&imsg);
859 return err;
862 static const struct got_error *
863 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
864 size_t logmsg_len)
866 const struct got_error *err = NULL;
867 size_t offset, remain;
869 offset = 0;
870 remain = logmsg_len;
871 while (remain > 0) {
872 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
874 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
875 commit->logmsg + offset, n) == -1) {
876 err = got_error_from_errno("imsg_compose "
877 "COMMIT_LOGMSG");
878 break;
881 err = flush_imsg(ibuf);
882 if (err)
883 break;
885 offset += n;
886 remain -= n;
889 return err;
892 const struct got_error *
893 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
895 const struct got_error *err = NULL;
896 struct got_imsg_commit_object *icommit;
897 uint8_t *buf;
898 size_t len, total;
899 struct got_object_qid *qid;
900 size_t author_len = strlen(commit->author);
901 size_t committer_len = strlen(commit->committer);
902 size_t logmsg_len = strlen(commit->logmsg);
904 total = sizeof(*icommit) + author_len + committer_len +
905 commit->nparents * SHA1_DIGEST_LENGTH;
907 buf = malloc(total);
908 if (buf == NULL)
909 return got_error_from_errno("malloc");
911 icommit = (struct got_imsg_commit_object *)buf;
912 memcpy(icommit->tree_id, commit->tree_id->sha1,
913 sizeof(icommit->tree_id));
914 icommit->author_len = author_len;
915 icommit->author_time = commit->author_time;
916 icommit->author_gmtoff = commit->author_gmtoff;
917 icommit->committer_len = committer_len;
918 icommit->committer_time = commit->committer_time;
919 icommit->committer_gmtoff = commit->committer_gmtoff;
920 icommit->logmsg_len = logmsg_len;
921 icommit->nparents = commit->nparents;
923 len = sizeof(*icommit);
924 memcpy(buf + len, commit->author, author_len);
925 len += author_len;
926 memcpy(buf + len, commit->committer, committer_len);
927 len += committer_len;
928 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
929 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
930 len += SHA1_DIGEST_LENGTH;
933 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
934 err = got_error_from_errno("imsg_compose COMMIT");
935 goto done;
938 if (logmsg_len == 0 ||
939 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
940 err = flush_imsg(ibuf);
941 if (err)
942 goto done;
944 err = send_commit_logmsg(ibuf, commit, logmsg_len);
945 done:
946 free(buf);
947 return err;
950 static const struct got_error *
951 get_commit_from_imsg(struct got_commit_object **commit,
952 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
954 const struct got_error *err = NULL;
955 struct got_imsg_commit_object *icommit;
956 size_t len = 0;
957 int i;
959 if (datalen < sizeof(*icommit))
960 return got_error(GOT_ERR_PRIVSEP_LEN);
962 icommit = imsg->data;
963 if (datalen != sizeof(*icommit) + icommit->author_len +
964 icommit->committer_len +
965 icommit->nparents * SHA1_DIGEST_LENGTH)
966 return got_error(GOT_ERR_PRIVSEP_LEN);
968 if (icommit->nparents < 0)
969 return got_error(GOT_ERR_PRIVSEP_LEN);
971 len += sizeof(*icommit);
973 *commit = got_object_commit_alloc_partial();
974 if (*commit == NULL)
975 return got_error_from_errno(
976 "got_object_commit_alloc_partial");
978 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
979 SHA1_DIGEST_LENGTH);
980 (*commit)->author_time = icommit->author_time;
981 (*commit)->author_gmtoff = icommit->author_gmtoff;
982 (*commit)->committer_time = icommit->committer_time;
983 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
985 if (icommit->author_len == 0) {
986 (*commit)->author = strdup("");
987 if ((*commit)->author == NULL) {
988 err = got_error_from_errno("strdup");
989 goto done;
991 } else {
992 (*commit)->author = malloc(icommit->author_len + 1);
993 if ((*commit)->author == NULL) {
994 err = got_error_from_errno("malloc");
995 goto done;
997 memcpy((*commit)->author, imsg->data + len,
998 icommit->author_len);
999 (*commit)->author[icommit->author_len] = '\0';
1001 len += icommit->author_len;
1003 if (icommit->committer_len == 0) {
1004 (*commit)->committer = strdup("");
1005 if ((*commit)->committer == NULL) {
1006 err = got_error_from_errno("strdup");
1007 goto done;
1009 } else {
1010 (*commit)->committer =
1011 malloc(icommit->committer_len + 1);
1012 if ((*commit)->committer == NULL) {
1013 err = got_error_from_errno("malloc");
1014 goto done;
1016 memcpy((*commit)->committer, imsg->data + len,
1017 icommit->committer_len);
1018 (*commit)->committer[icommit->committer_len] = '\0';
1020 len += icommit->committer_len;
1022 if (icommit->logmsg_len == 0) {
1023 (*commit)->logmsg = strdup("");
1024 if ((*commit)->logmsg == NULL) {
1025 err = got_error_from_errno("strdup");
1026 goto done;
1028 } else {
1029 size_t offset = 0, remain = icommit->logmsg_len;
1031 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1032 if ((*commit)->logmsg == NULL) {
1033 err = got_error_from_errno("malloc");
1034 goto done;
1036 while (remain > 0) {
1037 struct imsg imsg_log;
1038 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1039 remain);
1041 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1042 if (err)
1043 goto done;
1045 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1046 err = got_error(GOT_ERR_PRIVSEP_MSG);
1047 goto done;
1050 memcpy((*commit)->logmsg + offset,
1051 imsg_log.data, n);
1052 imsg_free(&imsg_log);
1053 offset += n;
1054 remain -= n;
1056 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1059 for (i = 0; i < icommit->nparents; i++) {
1060 struct got_object_qid *qid;
1062 err = got_object_qid_alloc_partial(&qid);
1063 if (err)
1064 break;
1065 memcpy(qid->id, imsg->data + len +
1066 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1067 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1068 (*commit)->nparents++;
1070 done:
1071 if (err) {
1072 got_object_commit_close(*commit);
1073 *commit = NULL;
1075 return err;
1078 const struct got_error *
1079 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1081 const struct got_error *err = NULL;
1082 struct imsg imsg;
1083 size_t datalen;
1084 const size_t min_datalen =
1085 MIN(sizeof(struct got_imsg_error),
1086 sizeof(struct got_imsg_commit_object));
1088 *commit = NULL;
1090 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1091 if (err)
1092 return err;
1094 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1096 switch (imsg.hdr.type) {
1097 case GOT_IMSG_COMMIT:
1098 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1099 break;
1100 default:
1101 err = got_error(GOT_ERR_PRIVSEP_MSG);
1102 break;
1105 imsg_free(&imsg);
1107 return err;
1110 const struct got_error *
1111 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1112 int nentries)
1114 const struct got_error *err = NULL;
1115 struct got_imsg_tree_object itree;
1116 struct got_pathlist_entry *pe;
1117 size_t totlen;
1118 int nimsg; /* number of imsg queued in ibuf */
1120 itree.nentries = nentries;
1121 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1122 == -1)
1123 return got_error_from_errno("imsg_compose TREE");
1125 totlen = sizeof(itree);
1126 nimsg = 1;
1127 TAILQ_FOREACH(pe, entries, entry) {
1128 const char *name = pe->path;
1129 struct got_parsed_tree_entry *pte = pe->data;
1130 struct ibuf *wbuf;
1131 size_t namelen = strlen(name);
1132 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1134 if (len > MAX_IMSGSIZE)
1135 return got_error(GOT_ERR_NO_SPACE);
1137 nimsg++;
1138 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1139 err = flush_imsg(ibuf);
1140 if (err)
1141 return err;
1142 nimsg = 0;
1145 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1146 if (wbuf == NULL)
1147 return got_error_from_errno("imsg_create TREE_ENTRY");
1149 /* Keep in sync with struct got_imsg_tree_object definition! */
1150 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1151 err = got_error_from_errno("imsg_add TREE_ENTRY");
1152 ibuf_free(wbuf);
1153 return err;
1155 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1156 err = got_error_from_errno("imsg_add TREE_ENTRY");
1157 ibuf_free(wbuf);
1158 return err;
1161 if (imsg_add(wbuf, name, namelen) == -1) {
1162 err = got_error_from_errno("imsg_add TREE_ENTRY");
1163 ibuf_free(wbuf);
1164 return err;
1167 wbuf->fd = -1;
1168 imsg_close(ibuf, wbuf);
1170 totlen += len;
1173 return flush_imsg(ibuf);
1176 const struct got_error *
1177 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1179 const struct got_error *err = NULL;
1180 const size_t min_datalen =
1181 MIN(sizeof(struct got_imsg_error),
1182 sizeof(struct got_imsg_tree_object));
1183 struct got_imsg_tree_object *itree;
1184 int nentries = 0;
1186 *tree = NULL;
1187 get_more:
1188 err = read_imsg(ibuf);
1189 if (err)
1190 goto done;
1192 for (;;) {
1193 struct imsg imsg;
1194 size_t n;
1195 size_t datalen;
1196 struct got_imsg_tree_entry *ite;
1197 struct got_tree_entry *te = NULL;
1199 n = imsg_get(ibuf, &imsg);
1200 if (n == 0) {
1201 if (*tree && (*tree)->nentries != nentries)
1202 goto get_more;
1203 break;
1206 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1207 return got_error(GOT_ERR_PRIVSEP_LEN);
1209 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1211 switch (imsg.hdr.type) {
1212 case GOT_IMSG_ERROR:
1213 err = recv_imsg_error(&imsg, datalen);
1214 break;
1215 case GOT_IMSG_TREE:
1216 /* This message should only appear once. */
1217 if (*tree != NULL) {
1218 err = got_error(GOT_ERR_PRIVSEP_MSG);
1219 break;
1221 if (datalen != sizeof(*itree)) {
1222 err = got_error(GOT_ERR_PRIVSEP_LEN);
1223 break;
1225 itree = imsg.data;
1226 *tree = malloc(sizeof(**tree));
1227 if (*tree == NULL) {
1228 err = got_error_from_errno("malloc");
1229 break;
1231 (*tree)->entries = calloc(itree->nentries,
1232 sizeof(struct got_tree_entry));
1233 if ((*tree)->entries == NULL) {
1234 err = got_error_from_errno("malloc");
1235 break;
1237 (*tree)->nentries = itree->nentries;
1238 (*tree)->refcnt = 0;
1239 break;
1240 case GOT_IMSG_TREE_ENTRY:
1241 /* This message should be preceeded by GOT_IMSG_TREE. */
1242 if (*tree == NULL) {
1243 err = got_error(GOT_ERR_PRIVSEP_MSG);
1244 break;
1246 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1247 err = got_error(GOT_ERR_PRIVSEP_LEN);
1248 break;
1251 /* Remaining data contains the entry's name. */
1252 datalen -= sizeof(*ite);
1253 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1254 err = got_error(GOT_ERR_PRIVSEP_LEN);
1255 break;
1257 ite = imsg.data;
1259 if (datalen + 1 > sizeof(te->name)) {
1260 err = got_error(GOT_ERR_NO_SPACE);
1261 break;
1263 te = &(*tree)->entries[nentries];
1264 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1265 te->name[datalen] = '\0';
1267 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1268 te->mode = ite->mode;
1269 te->idx = nentries;
1270 nentries++;
1271 break;
1272 default:
1273 err = got_error(GOT_ERR_PRIVSEP_MSG);
1274 break;
1277 imsg_free(&imsg);
1279 done:
1280 if (*tree && (*tree)->nentries != nentries) {
1281 if (err == NULL)
1282 err = got_error(GOT_ERR_PRIVSEP_LEN);
1283 got_object_tree_close(*tree);
1284 *tree = NULL;
1287 return err;
1290 const struct got_error *
1291 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1292 const uint8_t *data)
1294 struct got_imsg_blob iblob;
1296 iblob.size = size;
1297 iblob.hdrlen = hdrlen;
1299 if (data) {
1300 uint8_t *buf;
1302 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1303 return got_error(GOT_ERR_NO_SPACE);
1305 buf = malloc(sizeof(iblob) + size);
1306 if (buf == NULL)
1307 return got_error_from_errno("malloc");
1309 memcpy(buf, &iblob, sizeof(iblob));
1310 memcpy(buf + sizeof(iblob), data, size);
1311 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1312 sizeof(iblob) + size) == -1) {
1313 free(buf);
1314 return got_error_from_errno("imsg_compose BLOB");
1316 free(buf);
1317 } else {
1318 /* Data has already been written to file descriptor. */
1319 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1320 sizeof(iblob)) == -1)
1321 return got_error_from_errno("imsg_compose BLOB");
1325 return flush_imsg(ibuf);
1328 const struct got_error *
1329 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1330 struct imsgbuf *ibuf)
1332 const struct got_error *err = NULL;
1333 struct imsg imsg;
1334 struct got_imsg_blob *iblob;
1335 size_t datalen;
1337 *outbuf = NULL;
1339 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1340 if (err)
1341 return err;
1343 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1345 switch (imsg.hdr.type) {
1346 case GOT_IMSG_BLOB:
1347 if (datalen < sizeof(*iblob)) {
1348 err = got_error(GOT_ERR_PRIVSEP_LEN);
1349 break;
1351 iblob = imsg.data;
1352 *size = iblob->size;
1353 *hdrlen = iblob->hdrlen;
1355 if (datalen == sizeof(*iblob)) {
1356 /* Data has been written to file descriptor. */
1357 break;
1360 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1361 err = got_error(GOT_ERR_PRIVSEP_LEN);
1362 break;
1365 *outbuf = malloc(*size);
1366 if (*outbuf == NULL) {
1367 err = got_error_from_errno("malloc");
1368 break;
1370 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1371 break;
1372 default:
1373 err = got_error(GOT_ERR_PRIVSEP_MSG);
1374 break;
1377 imsg_free(&imsg);
1379 return err;
1382 static const struct got_error *
1383 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1385 const struct got_error *err = NULL;
1386 size_t offset, remain;
1388 offset = 0;
1389 remain = tagmsg_len;
1390 while (remain > 0) {
1391 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1393 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1394 tag->tagmsg + offset, n) == -1) {
1395 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1396 break;
1399 err = flush_imsg(ibuf);
1400 if (err)
1401 break;
1403 offset += n;
1404 remain -= n;
1407 return err;
1410 const struct got_error *
1411 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1413 const struct got_error *err = NULL;
1414 struct got_imsg_tag_object *itag;
1415 uint8_t *buf;
1416 size_t len, total;
1417 size_t tag_len = strlen(tag->tag);
1418 size_t tagger_len = strlen(tag->tagger);
1419 size_t tagmsg_len = strlen(tag->tagmsg);
1421 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1423 buf = malloc(total);
1424 if (buf == NULL)
1425 return got_error_from_errno("malloc");
1427 itag = (struct got_imsg_tag_object *)buf;
1428 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1429 itag->obj_type = tag->obj_type;
1430 itag->tag_len = tag_len;
1431 itag->tagger_len = tagger_len;
1432 itag->tagger_time = tag->tagger_time;
1433 itag->tagger_gmtoff = tag->tagger_gmtoff;
1434 itag->tagmsg_len = tagmsg_len;
1436 len = sizeof(*itag);
1437 memcpy(buf + len, tag->tag, tag_len);
1438 len += tag_len;
1439 memcpy(buf + len, tag->tagger, tagger_len);
1440 len += tagger_len;
1442 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1443 err = got_error_from_errno("imsg_compose TAG");
1444 goto done;
1447 if (tagmsg_len == 0 ||
1448 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1449 err = flush_imsg(ibuf);
1450 if (err)
1451 goto done;
1453 err = send_tagmsg(ibuf, tag, tagmsg_len);
1454 done:
1455 free(buf);
1456 return err;
1459 const struct got_error *
1460 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1462 const struct got_error *err = NULL;
1463 struct imsg imsg;
1464 struct got_imsg_tag_object *itag;
1465 size_t len, datalen;
1466 const size_t min_datalen =
1467 MIN(sizeof(struct got_imsg_error),
1468 sizeof(struct got_imsg_tag_object));
1470 *tag = NULL;
1472 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1473 if (err)
1474 return err;
1476 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1477 len = 0;
1479 switch (imsg.hdr.type) {
1480 case GOT_IMSG_TAG:
1481 if (datalen < sizeof(*itag)) {
1482 err = got_error(GOT_ERR_PRIVSEP_LEN);
1483 break;
1485 itag = imsg.data;
1486 if (datalen != sizeof(*itag) + itag->tag_len +
1487 itag->tagger_len) {
1488 err = got_error(GOT_ERR_PRIVSEP_LEN);
1489 break;
1491 len += sizeof(*itag);
1493 *tag = calloc(1, sizeof(**tag));
1494 if (*tag == NULL) {
1495 err = got_error_from_errno("calloc");
1496 break;
1499 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1501 if (itag->tag_len == 0) {
1502 (*tag)->tag = strdup("");
1503 if ((*tag)->tag == NULL) {
1504 err = got_error_from_errno("strdup");
1505 break;
1507 } else {
1508 (*tag)->tag = malloc(itag->tag_len + 1);
1509 if ((*tag)->tag == NULL) {
1510 err = got_error_from_errno("malloc");
1511 break;
1513 memcpy((*tag)->tag, imsg.data + len,
1514 itag->tag_len);
1515 (*tag)->tag[itag->tag_len] = '\0';
1517 len += itag->tag_len;
1519 (*tag)->obj_type = itag->obj_type;
1520 (*tag)->tagger_time = itag->tagger_time;
1521 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1523 if (itag->tagger_len == 0) {
1524 (*tag)->tagger = strdup("");
1525 if ((*tag)->tagger == NULL) {
1526 err = got_error_from_errno("strdup");
1527 break;
1529 } else {
1530 (*tag)->tagger = malloc(itag->tagger_len + 1);
1531 if ((*tag)->tagger == NULL) {
1532 err = got_error_from_errno("malloc");
1533 break;
1535 memcpy((*tag)->tagger, imsg.data + len,
1536 itag->tagger_len);
1537 (*tag)->tagger[itag->tagger_len] = '\0';
1539 len += itag->tagger_len;
1541 if (itag->tagmsg_len == 0) {
1542 (*tag)->tagmsg = strdup("");
1543 if ((*tag)->tagmsg == NULL) {
1544 err = got_error_from_errno("strdup");
1545 break;
1547 } else {
1548 size_t offset = 0, remain = itag->tagmsg_len;
1550 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1551 if ((*tag)->tagmsg == NULL) {
1552 err = got_error_from_errno("malloc");
1553 break;
1555 while (remain > 0) {
1556 struct imsg imsg_log;
1557 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1558 remain);
1560 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1561 if (err)
1562 return err;
1564 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1565 return got_error(GOT_ERR_PRIVSEP_MSG);
1567 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1568 n);
1569 imsg_free(&imsg_log);
1570 offset += n;
1571 remain -= n;
1573 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1576 break;
1577 default:
1578 err = got_error(GOT_ERR_PRIVSEP_MSG);
1579 break;
1582 imsg_free(&imsg);
1584 return err;
1587 const struct got_error *
1588 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1589 struct got_packidx *packidx)
1591 const struct got_error *err = NULL;
1592 struct got_imsg_packidx ipackidx;
1593 struct got_imsg_pack ipack;
1594 int fd;
1596 ipackidx.len = packidx->len;
1597 fd = dup(packidx->fd);
1598 if (fd == -1)
1599 return got_error_from_errno("dup");
1601 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1602 sizeof(ipackidx)) == -1) {
1603 err = got_error_from_errno("imsg_compose PACKIDX");
1604 close(fd);
1605 return err;
1608 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1609 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1610 return got_error(GOT_ERR_NO_SPACE);
1611 ipack.filesize = pack->filesize;
1613 fd = dup(pack->fd);
1614 if (fd == -1)
1615 return got_error_from_errno("dup");
1617 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1618 == -1) {
1619 err = got_error_from_errno("imsg_compose PACK");
1620 close(fd);
1621 return err;
1624 return flush_imsg(ibuf);
1627 const struct got_error *
1628 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1629 struct got_object_id *id)
1631 struct got_imsg_packed_object iobj;
1633 iobj.idx = idx;
1634 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1636 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1637 &iobj, sizeof(iobj)) == -1)
1638 return got_error_from_errno("imsg_compose "
1639 "PACKED_OBJECT_REQUEST");
1641 return flush_imsg(ibuf);
1644 const struct got_error *
1645 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1647 const struct got_error *err = NULL;
1649 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1650 NULL, 0) == -1) {
1651 err = got_error_from_errno("imsg_compose "
1652 "GITCONFIG_PARSE_REQUEST");
1653 close(fd);
1654 return err;
1657 return flush_imsg(ibuf);
1660 const struct got_error *
1661 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1663 if (imsg_compose(ibuf,
1664 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1665 NULL, 0) == -1)
1666 return got_error_from_errno("imsg_compose "
1667 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1669 return flush_imsg(ibuf);
1672 const struct got_error *
1673 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1675 if (imsg_compose(ibuf,
1676 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1677 return got_error_from_errno("imsg_compose "
1678 "GITCONFIG_AUTHOR_NAME_REQUEST");
1680 return flush_imsg(ibuf);
1683 const struct got_error *
1684 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1686 if (imsg_compose(ibuf,
1687 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1688 return got_error_from_errno("imsg_compose "
1689 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1691 return flush_imsg(ibuf);
1694 const struct got_error *
1695 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1697 if (imsg_compose(ibuf,
1698 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1699 return got_error_from_errno("imsg_compose "
1700 "GITCONFIG_REMOTE_REQUEST");
1702 return flush_imsg(ibuf);
1705 const struct got_error *
1706 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1708 if (imsg_compose(ibuf,
1709 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1710 return got_error_from_errno("imsg_compose "
1711 "GITCONFIG_OWNER_REQUEST");
1713 return flush_imsg(ibuf);
1716 const struct got_error *
1717 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1719 const struct got_error *err = NULL;
1720 struct imsg imsg;
1721 size_t datalen;
1722 const size_t min_datalen = 0;
1724 *str = NULL;
1726 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1727 if (err)
1728 return err;
1729 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1731 switch (imsg.hdr.type) {
1732 case GOT_IMSG_GITCONFIG_STR_VAL:
1733 if (datalen == 0)
1734 break;
1735 *str = malloc(datalen);
1736 if (*str == NULL) {
1737 err = got_error_from_errno("malloc");
1738 break;
1740 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1741 err = got_error(GOT_ERR_NO_SPACE);
1742 break;
1743 default:
1744 err = got_error(GOT_ERR_PRIVSEP_MSG);
1745 break;
1748 imsg_free(&imsg);
1749 return err;
1752 const struct got_error *
1753 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1755 const struct got_error *err = NULL;
1756 struct imsg imsg;
1757 size_t datalen;
1758 const size_t min_datalen =
1759 MIN(sizeof(struct got_imsg_error), sizeof(int));
1761 *val = 0;
1763 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1764 if (err)
1765 return err;
1766 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1768 switch (imsg.hdr.type) {
1769 case GOT_IMSG_GITCONFIG_INT_VAL:
1770 if (datalen != sizeof(*val)) {
1771 err = got_error(GOT_ERR_PRIVSEP_LEN);
1772 break;
1774 memcpy(val, imsg.data, sizeof(*val));
1775 break;
1776 default:
1777 err = got_error(GOT_ERR_PRIVSEP_MSG);
1778 break;
1781 imsg_free(&imsg);
1782 return err;
1785 const struct got_error *
1786 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1787 int *nremotes, struct imsgbuf *ibuf)
1789 const struct got_error *err = NULL;
1790 struct imsg imsg;
1791 size_t datalen;
1792 struct got_imsg_remotes iremotes;
1793 struct got_imsg_remote iremote;
1795 *remotes = NULL;
1796 *nremotes = 0;
1797 iremotes.nremotes = 0;
1799 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1800 if (err)
1801 return err;
1802 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1804 switch (imsg.hdr.type) {
1805 case GOT_IMSG_GITCONFIG_REMOTES:
1806 if (datalen != sizeof(iremotes)) {
1807 err = got_error(GOT_ERR_PRIVSEP_LEN);
1808 break;
1810 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1811 if (iremotes.nremotes == 0) {
1812 imsg_free(&imsg);
1813 return NULL;
1815 break;
1816 default:
1817 imsg_free(&imsg);
1818 return got_error(GOT_ERR_PRIVSEP_MSG);
1821 imsg_free(&imsg);
1823 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
1824 if (*remotes == NULL)
1825 return got_error_from_errno("recallocarray");
1827 while (*nremotes < iremotes.nremotes) {
1828 struct got_remote_repo *remote;
1830 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1831 if (err)
1832 break;
1833 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1835 switch (imsg.hdr.type) {
1836 case GOT_IMSG_GITCONFIG_REMOTE:
1837 remote = &(*remotes)[*nremotes];
1838 if (datalen < sizeof(iremote)) {
1839 err = got_error(GOT_ERR_PRIVSEP_LEN);
1840 break;
1842 memcpy(&iremote, imsg.data, sizeof(iremote));
1843 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1844 (sizeof(iremote) + iremote.name_len +
1845 iremote.url_len) > datalen) {
1846 err = got_error(GOT_ERR_PRIVSEP_LEN);
1847 break;
1849 remote->name = strndup(imsg.data + sizeof(iremote),
1850 iremote.name_len);
1851 if (remote->name == NULL) {
1852 err = got_error_from_errno("strndup");
1853 break;
1855 remote->url = strndup(imsg.data + sizeof(iremote) +
1856 iremote.name_len, iremote.url_len);
1857 if (remote->url == NULL) {
1858 err = got_error_from_errno("strndup");
1859 free(remote->name);
1860 break;
1862 remote->mirror_references = iremote.mirror_references;
1863 (*nremotes)++;
1864 break;
1865 default:
1866 err = got_error(GOT_ERR_PRIVSEP_MSG);
1867 break;
1870 imsg_free(&imsg);
1871 if (err)
1872 break;
1875 if (err) {
1876 int i;
1877 for (i = 0; i < *nremotes; i++) {
1878 free((*remotes)[i].name);
1879 free((*remotes)[i].url);
1881 free(*remotes);
1882 *remotes = NULL;
1883 *nremotes = 0;
1885 return err;
1888 const struct got_error *
1889 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1890 struct got_object_id *id, int idx, const char *path)
1892 const struct got_error *err = NULL;
1893 struct ibuf *wbuf;
1894 size_t path_len = strlen(path) + 1;
1896 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1897 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1898 if (wbuf == NULL)
1899 return got_error_from_errno(
1900 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1901 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1902 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1903 ibuf_free(wbuf);
1904 return err;
1906 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1907 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1908 ibuf_free(wbuf);
1909 return err;
1911 if (imsg_add(wbuf, path, path_len) == -1) {
1912 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1913 ibuf_free(wbuf);
1914 return err;
1917 wbuf->fd = -1;
1918 imsg_close(ibuf, wbuf);
1920 return flush_imsg(ibuf);
1923 const struct got_error *
1924 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1925 struct got_object_id **changed_commit_id,
1926 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1928 const struct got_error *err = NULL;
1929 struct imsg imsg;
1930 struct got_imsg_traversed_commits *icommits;
1931 size_t datalen;
1932 int i, done = 0;
1934 *changed_commit = NULL;
1935 *changed_commit_id = NULL;
1937 while (!done) {
1938 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1939 if (err)
1940 return err;
1942 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1943 switch (imsg.hdr.type) {
1944 case GOT_IMSG_TRAVERSED_COMMITS:
1945 icommits = imsg.data;
1946 if (datalen != sizeof(*icommits) +
1947 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1948 err = got_error(GOT_ERR_PRIVSEP_LEN);
1949 break;
1951 for (i = 0; i < icommits->ncommits; i++) {
1952 struct got_object_qid *qid;
1953 uint8_t *sha1 = (uint8_t *)imsg.data +
1954 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1955 err = got_object_qid_alloc_partial(&qid);
1956 if (err)
1957 break;
1958 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1959 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1961 /* The last commit may contain a change. */
1962 if (i == icommits->ncommits - 1) {
1963 *changed_commit_id =
1964 got_object_id_dup(qid->id);
1965 if (*changed_commit_id == NULL) {
1966 err = got_error_from_errno(
1967 "got_object_id_dup");
1968 break;
1972 break;
1973 case GOT_IMSG_COMMIT:
1974 if (*changed_commit_id == NULL) {
1975 err = got_error(GOT_ERR_PRIVSEP_MSG);
1976 break;
1978 err = get_commit_from_imsg(changed_commit, &imsg,
1979 datalen, ibuf);
1980 break;
1981 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1982 done = 1;
1983 break;
1984 default:
1985 err = got_error(GOT_ERR_PRIVSEP_MSG);
1986 break;
1989 imsg_free(&imsg);
1990 if (err)
1991 break;
1994 if (err)
1995 got_object_id_queue_free(commit_ids);
1996 return err;
1999 const struct got_error *
2000 got_privsep_unveil_exec_helpers(void)
2002 const char *helpers[] = {
2003 GOT_PATH_PROG_READ_PACK,
2004 GOT_PATH_PROG_READ_OBJECT,
2005 GOT_PATH_PROG_READ_COMMIT,
2006 GOT_PATH_PROG_READ_TREE,
2007 GOT_PATH_PROG_READ_BLOB,
2008 GOT_PATH_PROG_READ_TAG,
2009 GOT_PATH_PROG_READ_GITCONFIG,
2010 GOT_PATH_PROG_FETCH_PACK,
2011 GOT_PATH_PROG_INDEX_PACK,
2013 int i;
2015 for (i = 0; i < nitems(helpers); i++) {
2016 if (unveil(helpers[i], "x") == 0)
2017 continue;
2018 return got_error_from_errno2("unveil", helpers[i]);
2021 return NULL;
2024 void
2025 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2027 if (close(imsg_fds[0]) != 0) {
2028 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2029 _exit(1);
2032 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2033 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2034 _exit(1);
2036 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2037 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2038 _exit(1);
2041 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2042 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2043 strerror(errno));
2044 _exit(1);