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, uint8_t *pack_sha1, 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;
586 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
588 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
589 if (err)
590 return err;
592 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
593 switch (imsg.hdr.type) {
594 case GOT_IMSG_ERROR:
595 if (datalen < sizeof(struct got_imsg_error)) {
596 err = got_error(GOT_ERR_PRIVSEP_LEN);
597 break;
599 err = recv_imsg_error(&imsg, datalen);
600 break;
601 case GOT_IMSG_FETCH_SYMREFS:
602 if (datalen < sizeof(*isymrefs)) {
603 err = got_error(GOT_ERR_PRIVSEP_LEN);
604 break;
606 if (isymrefs != NULL) {
607 err = got_error(GOT_ERR_PRIVSEP_MSG);
608 break;
610 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
611 off = sizeof(*isymrefs);
612 remain = datalen - off;
613 for (n = 0; n < isymrefs->nsymrefs; n++) {
614 struct got_imsg_fetch_symref *s;
615 char *name, *target;
616 if (remain < sizeof(struct got_imsg_fetch_symref)) {
617 err = got_error(GOT_ERR_PRIVSEP_LEN);
618 goto done;
620 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
621 off += sizeof(*s);
622 remain -= sizeof(*s);
623 if (remain < s->name_len) {
624 err = got_error(GOT_ERR_PRIVSEP_LEN);
625 goto done;
627 name = strndup(imsg.data + off, s->name_len);
628 if (name == NULL) {
629 err = got_error_from_errno("strndup");
630 goto done;
632 off += s->name_len;
633 remain -= s->name_len;
634 if (remain < s->target_len) {
635 err = got_error(GOT_ERR_PRIVSEP_LEN);
636 free(name);
637 goto done;
639 target = strndup(imsg.data + off, s->target_len);
640 if (target == NULL) {
641 err = got_error_from_errno("strndup");
642 free(name);
643 goto done;
645 off += s->target_len;
646 remain -= s->target_len;
647 err = got_pathlist_append(symrefs, name, target);
648 if (err) {
649 free(name);
650 free(target);
651 goto done;
654 break;
655 case GOT_IMSG_FETCH_REF:
656 if (datalen <= SHA1_DIGEST_LENGTH) {
657 err = got_error(GOT_ERR_PRIVSEP_MSG);
658 break;
660 *id = malloc(sizeof(**id));
661 if (*id == NULL) {
662 err = got_error_from_errno("malloc");
663 break;
665 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
666 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
667 datalen - SHA1_DIGEST_LENGTH);
668 if (*refname == NULL) {
669 err = got_error_from_errno("strndup");
670 break;
672 break;
673 case GOT_IMSG_FETCH_SERVER_PROGRESS:
674 if (datalen == 0) {
675 err = got_error(GOT_ERR_PRIVSEP_LEN);
676 break;
678 *server_progress = strndup(imsg.data, datalen);
679 if (*server_progress == NULL) {
680 err = got_error_from_errno("strndup");
681 break;
683 for (i = 0; i < datalen; i++) {
684 if (!isprint((unsigned char)(*server_progress)[i]) &&
685 !isspace((unsigned char)(*server_progress)[i])) {
686 err = got_error(GOT_ERR_PRIVSEP_MSG);
687 free(*server_progress);
688 *server_progress = NULL;
689 goto done;
692 break;
693 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
694 if (datalen < sizeof(*packfile_size)) {
695 err = got_error(GOT_ERR_PRIVSEP_MSG);
696 break;
698 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
699 break;
700 case GOT_IMSG_FETCH_DONE:
701 if (datalen != SHA1_DIGEST_LENGTH) {
702 err = got_error(GOT_ERR_PRIVSEP_MSG);
703 break;
705 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
706 *done = 1;
707 break;
708 default:
709 err = got_error(GOT_ERR_PRIVSEP_MSG);
710 break;
712 done:
713 if (err) {
714 free(*id);
715 *id = NULL;
716 free(*refname);
717 *refname = NULL;
719 imsg_free(&imsg);
720 return err;
723 const struct got_error *
724 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
725 int fd)
727 const struct got_error *err = NULL;
729 /* Keep in sync with struct got_imsg_index_pack_request */
730 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
731 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
732 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
733 close(fd);
734 return err;
736 return flush_imsg(ibuf);
739 const struct got_error *
740 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
742 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
745 const struct got_error *
746 got_privsep_recv_index_progress(int *done, int *nobj_total,
747 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
748 struct imsgbuf *ibuf)
750 const struct got_error *err = NULL;
751 struct imsg imsg;
752 struct got_imsg_index_pack_progress *iprogress;
753 size_t datalen;
755 *done = 0;
756 *nobj_total = 0;
757 *nobj_indexed = 0;
758 *nobj_resolved = 0;
760 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
761 if (err)
762 return err;
764 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
765 switch (imsg.hdr.type) {
766 case GOT_IMSG_ERROR:
767 if (datalen < sizeof(struct got_imsg_error)) {
768 err = got_error(GOT_ERR_PRIVSEP_LEN);
769 break;
771 err = recv_imsg_error(&imsg, datalen);
772 break;
773 case GOT_IMSG_IDXPACK_PROGRESS:
774 if (datalen < sizeof(*iprogress)) {
775 err = got_error(GOT_ERR_PRIVSEP_LEN);
776 break;
778 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
779 *nobj_total = iprogress->nobj_total;
780 *nobj_indexed = iprogress->nobj_indexed;
781 *nobj_loose = iprogress->nobj_loose;
782 *nobj_resolved = iprogress->nobj_resolved;
783 break;
784 case GOT_IMSG_IDXPACK_DONE:
785 if (datalen != 0) {
786 err = got_error(GOT_ERR_PRIVSEP_LEN);
787 break;
789 *done = 1;
790 break;
791 default:
792 err = got_error(GOT_ERR_PRIVSEP_MSG);
793 break;
796 imsg_free(&imsg);
797 return err;
800 const struct got_error *
801 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
802 struct imsgbuf *ibuf)
804 const struct got_error *err = NULL;
805 struct got_imsg_object *iobj;
806 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
808 if (datalen != sizeof(*iobj))
809 return got_error(GOT_ERR_PRIVSEP_LEN);
810 iobj = imsg->data;
812 *obj = calloc(1, sizeof(**obj));
813 if (*obj == NULL)
814 return got_error_from_errno("calloc");
816 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
817 (*obj)->type = iobj->type;
818 (*obj)->flags = iobj->flags;
819 (*obj)->hdrlen = iobj->hdrlen;
820 (*obj)->size = iobj->size;
821 /* path_packfile is handled by caller */
822 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
823 (*obj)->pack_offset = iobj->pack_offset;
824 (*obj)->pack_idx = iobj->pack_idx;
827 return err;
830 const struct got_error *
831 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
833 const struct got_error *err = NULL;
834 struct imsg imsg;
835 const size_t min_datalen =
836 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
838 *obj = NULL;
840 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
841 if (err)
842 return err;
844 switch (imsg.hdr.type) {
845 case GOT_IMSG_OBJECT:
846 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
847 break;
848 default:
849 err = got_error(GOT_ERR_PRIVSEP_MSG);
850 break;
853 imsg_free(&imsg);
855 return err;
858 static const struct got_error *
859 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
860 size_t logmsg_len)
862 const struct got_error *err = NULL;
863 size_t offset, remain;
865 offset = 0;
866 remain = logmsg_len;
867 while (remain > 0) {
868 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
870 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
871 commit->logmsg + offset, n) == -1) {
872 err = got_error_from_errno("imsg_compose "
873 "COMMIT_LOGMSG");
874 break;
877 err = flush_imsg(ibuf);
878 if (err)
879 break;
881 offset += n;
882 remain -= n;
885 return err;
888 const struct got_error *
889 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
891 const struct got_error *err = NULL;
892 struct got_imsg_commit_object *icommit;
893 uint8_t *buf;
894 size_t len, total;
895 struct got_object_qid *qid;
896 size_t author_len = strlen(commit->author);
897 size_t committer_len = strlen(commit->committer);
898 size_t logmsg_len = strlen(commit->logmsg);
900 total = sizeof(*icommit) + author_len + committer_len +
901 commit->nparents * SHA1_DIGEST_LENGTH;
903 buf = malloc(total);
904 if (buf == NULL)
905 return got_error_from_errno("malloc");
907 icommit = (struct got_imsg_commit_object *)buf;
908 memcpy(icommit->tree_id, commit->tree_id->sha1,
909 sizeof(icommit->tree_id));
910 icommit->author_len = author_len;
911 icommit->author_time = commit->author_time;
912 icommit->author_gmtoff = commit->author_gmtoff;
913 icommit->committer_len = committer_len;
914 icommit->committer_time = commit->committer_time;
915 icommit->committer_gmtoff = commit->committer_gmtoff;
916 icommit->logmsg_len = logmsg_len;
917 icommit->nparents = commit->nparents;
919 len = sizeof(*icommit);
920 memcpy(buf + len, commit->author, author_len);
921 len += author_len;
922 memcpy(buf + len, commit->committer, committer_len);
923 len += committer_len;
924 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
925 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
926 len += SHA1_DIGEST_LENGTH;
929 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
930 err = got_error_from_errno("imsg_compose COMMIT");
931 goto done;
934 if (logmsg_len == 0 ||
935 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
936 err = flush_imsg(ibuf);
937 if (err)
938 goto done;
940 err = send_commit_logmsg(ibuf, commit, logmsg_len);
941 done:
942 free(buf);
943 return err;
946 static const struct got_error *
947 get_commit_from_imsg(struct got_commit_object **commit,
948 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
950 const struct got_error *err = NULL;
951 struct got_imsg_commit_object *icommit;
952 size_t len = 0;
953 int i;
955 if (datalen < sizeof(*icommit))
956 return got_error(GOT_ERR_PRIVSEP_LEN);
958 icommit = imsg->data;
959 if (datalen != sizeof(*icommit) + icommit->author_len +
960 icommit->committer_len +
961 icommit->nparents * SHA1_DIGEST_LENGTH)
962 return got_error(GOT_ERR_PRIVSEP_LEN);
964 if (icommit->nparents < 0)
965 return got_error(GOT_ERR_PRIVSEP_LEN);
967 len += sizeof(*icommit);
969 *commit = got_object_commit_alloc_partial();
970 if (*commit == NULL)
971 return got_error_from_errno(
972 "got_object_commit_alloc_partial");
974 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
975 SHA1_DIGEST_LENGTH);
976 (*commit)->author_time = icommit->author_time;
977 (*commit)->author_gmtoff = icommit->author_gmtoff;
978 (*commit)->committer_time = icommit->committer_time;
979 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
981 if (icommit->author_len == 0) {
982 (*commit)->author = strdup("");
983 if ((*commit)->author == NULL) {
984 err = got_error_from_errno("strdup");
985 goto done;
987 } else {
988 (*commit)->author = malloc(icommit->author_len + 1);
989 if ((*commit)->author == NULL) {
990 err = got_error_from_errno("malloc");
991 goto done;
993 memcpy((*commit)->author, imsg->data + len,
994 icommit->author_len);
995 (*commit)->author[icommit->author_len] = '\0';
997 len += icommit->author_len;
999 if (icommit->committer_len == 0) {
1000 (*commit)->committer = strdup("");
1001 if ((*commit)->committer == NULL) {
1002 err = got_error_from_errno("strdup");
1003 goto done;
1005 } else {
1006 (*commit)->committer =
1007 malloc(icommit->committer_len + 1);
1008 if ((*commit)->committer == NULL) {
1009 err = got_error_from_errno("malloc");
1010 goto done;
1012 memcpy((*commit)->committer, imsg->data + len,
1013 icommit->committer_len);
1014 (*commit)->committer[icommit->committer_len] = '\0';
1016 len += icommit->committer_len;
1018 if (icommit->logmsg_len == 0) {
1019 (*commit)->logmsg = strdup("");
1020 if ((*commit)->logmsg == NULL) {
1021 err = got_error_from_errno("strdup");
1022 goto done;
1024 } else {
1025 size_t offset = 0, remain = icommit->logmsg_len;
1027 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1028 if ((*commit)->logmsg == NULL) {
1029 err = got_error_from_errno("malloc");
1030 goto done;
1032 while (remain > 0) {
1033 struct imsg imsg_log;
1034 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1035 remain);
1037 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1038 if (err)
1039 goto done;
1041 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1042 err = got_error(GOT_ERR_PRIVSEP_MSG);
1043 goto done;
1046 memcpy((*commit)->logmsg + offset,
1047 imsg_log.data, n);
1048 imsg_free(&imsg_log);
1049 offset += n;
1050 remain -= n;
1052 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1055 for (i = 0; i < icommit->nparents; i++) {
1056 struct got_object_qid *qid;
1058 err = got_object_qid_alloc_partial(&qid);
1059 if (err)
1060 break;
1061 memcpy(qid->id, imsg->data + len +
1062 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1063 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1064 (*commit)->nparents++;
1066 done:
1067 if (err) {
1068 got_object_commit_close(*commit);
1069 *commit = NULL;
1071 return err;
1074 const struct got_error *
1075 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1077 const struct got_error *err = NULL;
1078 struct imsg imsg;
1079 size_t datalen;
1080 const size_t min_datalen =
1081 MIN(sizeof(struct got_imsg_error),
1082 sizeof(struct got_imsg_commit_object));
1084 *commit = NULL;
1086 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1087 if (err)
1088 return err;
1090 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1092 switch (imsg.hdr.type) {
1093 case GOT_IMSG_COMMIT:
1094 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1095 break;
1096 default:
1097 err = got_error(GOT_ERR_PRIVSEP_MSG);
1098 break;
1101 imsg_free(&imsg);
1103 return err;
1106 const struct got_error *
1107 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1108 int nentries)
1110 const struct got_error *err = NULL;
1111 struct got_imsg_tree_object itree;
1112 struct got_pathlist_entry *pe;
1113 size_t totlen;
1114 int nimsg; /* number of imsg queued in ibuf */
1116 itree.nentries = nentries;
1117 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1118 == -1)
1119 return got_error_from_errno("imsg_compose TREE");
1121 totlen = sizeof(itree);
1122 nimsg = 1;
1123 TAILQ_FOREACH(pe, entries, entry) {
1124 const char *name = pe->path;
1125 struct got_parsed_tree_entry *pte = pe->data;
1126 struct ibuf *wbuf;
1127 size_t namelen = strlen(name);
1128 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1130 if (len > MAX_IMSGSIZE)
1131 return got_error(GOT_ERR_NO_SPACE);
1133 nimsg++;
1134 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1135 err = flush_imsg(ibuf);
1136 if (err)
1137 return err;
1138 nimsg = 0;
1141 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1142 if (wbuf == NULL)
1143 return got_error_from_errno("imsg_create TREE_ENTRY");
1145 /* Keep in sync with struct got_imsg_tree_object definition! */
1146 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1147 err = got_error_from_errno("imsg_add TREE_ENTRY");
1148 ibuf_free(wbuf);
1149 return err;
1151 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1152 err = got_error_from_errno("imsg_add TREE_ENTRY");
1153 ibuf_free(wbuf);
1154 return err;
1157 if (imsg_add(wbuf, name, namelen) == -1) {
1158 err = got_error_from_errno("imsg_add TREE_ENTRY");
1159 ibuf_free(wbuf);
1160 return err;
1163 wbuf->fd = -1;
1164 imsg_close(ibuf, wbuf);
1166 totlen += len;
1169 return flush_imsg(ibuf);
1172 const struct got_error *
1173 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1175 const struct got_error *err = NULL;
1176 const size_t min_datalen =
1177 MIN(sizeof(struct got_imsg_error),
1178 sizeof(struct got_imsg_tree_object));
1179 struct got_imsg_tree_object *itree;
1180 int nentries = 0;
1182 *tree = NULL;
1183 get_more:
1184 err = read_imsg(ibuf);
1185 if (err)
1186 goto done;
1188 for (;;) {
1189 struct imsg imsg;
1190 size_t n;
1191 size_t datalen;
1192 struct got_imsg_tree_entry *ite;
1193 struct got_tree_entry *te = NULL;
1195 n = imsg_get(ibuf, &imsg);
1196 if (n == 0) {
1197 if (*tree && (*tree)->nentries != nentries)
1198 goto get_more;
1199 break;
1202 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1203 return got_error(GOT_ERR_PRIVSEP_LEN);
1205 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1207 switch (imsg.hdr.type) {
1208 case GOT_IMSG_ERROR:
1209 err = recv_imsg_error(&imsg, datalen);
1210 break;
1211 case GOT_IMSG_TREE:
1212 /* This message should only appear once. */
1213 if (*tree != NULL) {
1214 err = got_error(GOT_ERR_PRIVSEP_MSG);
1215 break;
1217 if (datalen != sizeof(*itree)) {
1218 err = got_error(GOT_ERR_PRIVSEP_LEN);
1219 break;
1221 itree = imsg.data;
1222 *tree = malloc(sizeof(**tree));
1223 if (*tree == NULL) {
1224 err = got_error_from_errno("malloc");
1225 break;
1227 (*tree)->entries = calloc(itree->nentries,
1228 sizeof(struct got_tree_entry));
1229 if ((*tree)->entries == NULL) {
1230 err = got_error_from_errno("malloc");
1231 break;
1233 (*tree)->nentries = itree->nentries;
1234 (*tree)->refcnt = 0;
1235 break;
1236 case GOT_IMSG_TREE_ENTRY:
1237 /* This message should be preceeded by GOT_IMSG_TREE. */
1238 if (*tree == NULL) {
1239 err = got_error(GOT_ERR_PRIVSEP_MSG);
1240 break;
1242 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1243 err = got_error(GOT_ERR_PRIVSEP_LEN);
1244 break;
1247 /* Remaining data contains the entry's name. */
1248 datalen -= sizeof(*ite);
1249 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1250 err = got_error(GOT_ERR_PRIVSEP_LEN);
1251 break;
1253 ite = imsg.data;
1255 if (datalen + 1 > sizeof(te->name)) {
1256 err = got_error(GOT_ERR_NO_SPACE);
1257 break;
1259 te = &(*tree)->entries[nentries];
1260 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1261 te->name[datalen] = '\0';
1263 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1264 te->mode = ite->mode;
1265 te->idx = nentries;
1266 nentries++;
1267 break;
1268 default:
1269 err = got_error(GOT_ERR_PRIVSEP_MSG);
1270 break;
1273 imsg_free(&imsg);
1275 done:
1276 if (*tree && (*tree)->nentries != nentries) {
1277 if (err == NULL)
1278 err = got_error(GOT_ERR_PRIVSEP_LEN);
1279 got_object_tree_close(*tree);
1280 *tree = NULL;
1283 return err;
1286 const struct got_error *
1287 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1288 const uint8_t *data)
1290 struct got_imsg_blob iblob;
1292 iblob.size = size;
1293 iblob.hdrlen = hdrlen;
1295 if (data) {
1296 uint8_t *buf;
1298 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1299 return got_error(GOT_ERR_NO_SPACE);
1301 buf = malloc(sizeof(iblob) + size);
1302 if (buf == NULL)
1303 return got_error_from_errno("malloc");
1305 memcpy(buf, &iblob, sizeof(iblob));
1306 memcpy(buf + sizeof(iblob), data, size);
1307 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1308 sizeof(iblob) + size) == -1) {
1309 free(buf);
1310 return got_error_from_errno("imsg_compose BLOB");
1312 free(buf);
1313 } else {
1314 /* Data has already been written to file descriptor. */
1315 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1316 sizeof(iblob)) == -1)
1317 return got_error_from_errno("imsg_compose BLOB");
1321 return flush_imsg(ibuf);
1324 const struct got_error *
1325 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1326 struct imsgbuf *ibuf)
1328 const struct got_error *err = NULL;
1329 struct imsg imsg;
1330 struct got_imsg_blob *iblob;
1331 size_t datalen;
1333 *outbuf = NULL;
1335 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1336 if (err)
1337 return err;
1339 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1341 switch (imsg.hdr.type) {
1342 case GOT_IMSG_BLOB:
1343 if (datalen < sizeof(*iblob)) {
1344 err = got_error(GOT_ERR_PRIVSEP_LEN);
1345 break;
1347 iblob = imsg.data;
1348 *size = iblob->size;
1349 *hdrlen = iblob->hdrlen;
1351 if (datalen == sizeof(*iblob)) {
1352 /* Data has been written to file descriptor. */
1353 break;
1356 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1357 err = got_error(GOT_ERR_PRIVSEP_LEN);
1358 break;
1361 *outbuf = malloc(*size);
1362 if (*outbuf == NULL) {
1363 err = got_error_from_errno("malloc");
1364 break;
1366 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1367 break;
1368 default:
1369 err = got_error(GOT_ERR_PRIVSEP_MSG);
1370 break;
1373 imsg_free(&imsg);
1375 return err;
1378 static const struct got_error *
1379 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1381 const struct got_error *err = NULL;
1382 size_t offset, remain;
1384 offset = 0;
1385 remain = tagmsg_len;
1386 while (remain > 0) {
1387 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1389 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1390 tag->tagmsg + offset, n) == -1) {
1391 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1392 break;
1395 err = flush_imsg(ibuf);
1396 if (err)
1397 break;
1399 offset += n;
1400 remain -= n;
1403 return err;
1406 const struct got_error *
1407 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1409 const struct got_error *err = NULL;
1410 struct got_imsg_tag_object *itag;
1411 uint8_t *buf;
1412 size_t len, total;
1413 size_t tag_len = strlen(tag->tag);
1414 size_t tagger_len = strlen(tag->tagger);
1415 size_t tagmsg_len = strlen(tag->tagmsg);
1417 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1419 buf = malloc(total);
1420 if (buf == NULL)
1421 return got_error_from_errno("malloc");
1423 itag = (struct got_imsg_tag_object *)buf;
1424 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1425 itag->obj_type = tag->obj_type;
1426 itag->tag_len = tag_len;
1427 itag->tagger_len = tagger_len;
1428 itag->tagger_time = tag->tagger_time;
1429 itag->tagger_gmtoff = tag->tagger_gmtoff;
1430 itag->tagmsg_len = tagmsg_len;
1432 len = sizeof(*itag);
1433 memcpy(buf + len, tag->tag, tag_len);
1434 len += tag_len;
1435 memcpy(buf + len, tag->tagger, tagger_len);
1436 len += tagger_len;
1438 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1439 err = got_error_from_errno("imsg_compose TAG");
1440 goto done;
1443 if (tagmsg_len == 0 ||
1444 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1445 err = flush_imsg(ibuf);
1446 if (err)
1447 goto done;
1449 err = send_tagmsg(ibuf, tag, tagmsg_len);
1450 done:
1451 free(buf);
1452 return err;
1455 const struct got_error *
1456 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1458 const struct got_error *err = NULL;
1459 struct imsg imsg;
1460 struct got_imsg_tag_object *itag;
1461 size_t len, datalen;
1462 const size_t min_datalen =
1463 MIN(sizeof(struct got_imsg_error),
1464 sizeof(struct got_imsg_tag_object));
1466 *tag = NULL;
1468 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1469 if (err)
1470 return err;
1472 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1473 len = 0;
1475 switch (imsg.hdr.type) {
1476 case GOT_IMSG_TAG:
1477 if (datalen < sizeof(*itag)) {
1478 err = got_error(GOT_ERR_PRIVSEP_LEN);
1479 break;
1481 itag = imsg.data;
1482 if (datalen != sizeof(*itag) + itag->tag_len +
1483 itag->tagger_len) {
1484 err = got_error(GOT_ERR_PRIVSEP_LEN);
1485 break;
1487 len += sizeof(*itag);
1489 *tag = calloc(1, sizeof(**tag));
1490 if (*tag == NULL) {
1491 err = got_error_from_errno("calloc");
1492 break;
1495 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1497 if (itag->tag_len == 0) {
1498 (*tag)->tag = strdup("");
1499 if ((*tag)->tag == NULL) {
1500 err = got_error_from_errno("strdup");
1501 break;
1503 } else {
1504 (*tag)->tag = malloc(itag->tag_len + 1);
1505 if ((*tag)->tag == NULL) {
1506 err = got_error_from_errno("malloc");
1507 break;
1509 memcpy((*tag)->tag, imsg.data + len,
1510 itag->tag_len);
1511 (*tag)->tag[itag->tag_len] = '\0';
1513 len += itag->tag_len;
1515 (*tag)->obj_type = itag->obj_type;
1516 (*tag)->tagger_time = itag->tagger_time;
1517 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1519 if (itag->tagger_len == 0) {
1520 (*tag)->tagger = strdup("");
1521 if ((*tag)->tagger == NULL) {
1522 err = got_error_from_errno("strdup");
1523 break;
1525 } else {
1526 (*tag)->tagger = malloc(itag->tagger_len + 1);
1527 if ((*tag)->tagger == NULL) {
1528 err = got_error_from_errno("malloc");
1529 break;
1531 memcpy((*tag)->tagger, imsg.data + len,
1532 itag->tagger_len);
1533 (*tag)->tagger[itag->tagger_len] = '\0';
1535 len += itag->tagger_len;
1537 if (itag->tagmsg_len == 0) {
1538 (*tag)->tagmsg = strdup("");
1539 if ((*tag)->tagmsg == NULL) {
1540 err = got_error_from_errno("strdup");
1541 break;
1543 } else {
1544 size_t offset = 0, remain = itag->tagmsg_len;
1546 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1547 if ((*tag)->tagmsg == NULL) {
1548 err = got_error_from_errno("malloc");
1549 break;
1551 while (remain > 0) {
1552 struct imsg imsg_log;
1553 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1554 remain);
1556 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1557 if (err)
1558 return err;
1560 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1561 return got_error(GOT_ERR_PRIVSEP_MSG);
1563 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1564 n);
1565 imsg_free(&imsg_log);
1566 offset += n;
1567 remain -= n;
1569 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1572 break;
1573 default:
1574 err = got_error(GOT_ERR_PRIVSEP_MSG);
1575 break;
1578 imsg_free(&imsg);
1580 return err;
1583 const struct got_error *
1584 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1585 struct got_packidx *packidx)
1587 const struct got_error *err = NULL;
1588 struct got_imsg_packidx ipackidx;
1589 struct got_imsg_pack ipack;
1590 int fd;
1592 ipackidx.len = packidx->len;
1593 fd = dup(packidx->fd);
1594 if (fd == -1)
1595 return got_error_from_errno("dup");
1597 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1598 sizeof(ipackidx)) == -1) {
1599 err = got_error_from_errno("imsg_compose PACKIDX");
1600 close(fd);
1601 return err;
1604 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1605 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1606 return got_error(GOT_ERR_NO_SPACE);
1607 ipack.filesize = pack->filesize;
1609 fd = dup(pack->fd);
1610 if (fd == -1)
1611 return got_error_from_errno("dup");
1613 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1614 == -1) {
1615 err = got_error_from_errno("imsg_compose PACK");
1616 close(fd);
1617 return err;
1620 return flush_imsg(ibuf);
1623 const struct got_error *
1624 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1625 struct got_object_id *id)
1627 struct got_imsg_packed_object iobj;
1629 iobj.idx = idx;
1630 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1632 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1633 &iobj, sizeof(iobj)) == -1)
1634 return got_error_from_errno("imsg_compose "
1635 "PACKED_OBJECT_REQUEST");
1637 return flush_imsg(ibuf);
1640 const struct got_error *
1641 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1643 const struct got_error *err = NULL;
1645 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1646 NULL, 0) == -1) {
1647 err = got_error_from_errno("imsg_compose "
1648 "GITCONFIG_PARSE_REQUEST");
1649 close(fd);
1650 return err;
1653 return flush_imsg(ibuf);
1656 const struct got_error *
1657 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1659 if (imsg_compose(ibuf,
1660 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1661 NULL, 0) == -1)
1662 return got_error_from_errno("imsg_compose "
1663 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1665 return flush_imsg(ibuf);
1668 const struct got_error *
1669 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1671 if (imsg_compose(ibuf,
1672 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1673 return got_error_from_errno("imsg_compose "
1674 "GITCONFIG_AUTHOR_NAME_REQUEST");
1676 return flush_imsg(ibuf);
1679 const struct got_error *
1680 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1682 if (imsg_compose(ibuf,
1683 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1684 return got_error_from_errno("imsg_compose "
1685 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1687 return flush_imsg(ibuf);
1690 const struct got_error *
1691 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1693 if (imsg_compose(ibuf,
1694 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1695 return got_error_from_errno("imsg_compose "
1696 "GITCONFIG_REMOTE_REQUEST");
1698 return flush_imsg(ibuf);
1701 const struct got_error *
1702 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1704 if (imsg_compose(ibuf,
1705 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1706 return got_error_from_errno("imsg_compose "
1707 "GITCONFIG_OWNER_REQUEST");
1709 return flush_imsg(ibuf);
1712 const struct got_error *
1713 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1715 const struct got_error *err = NULL;
1716 struct imsg imsg;
1717 size_t datalen;
1718 const size_t min_datalen = 0;
1720 *str = NULL;
1722 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1723 if (err)
1724 return err;
1725 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1727 switch (imsg.hdr.type) {
1728 case GOT_IMSG_GITCONFIG_STR_VAL:
1729 if (datalen == 0)
1730 break;
1731 *str = malloc(datalen);
1732 if (*str == NULL) {
1733 err = got_error_from_errno("malloc");
1734 break;
1736 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1737 err = got_error(GOT_ERR_NO_SPACE);
1738 break;
1739 default:
1740 err = got_error(GOT_ERR_PRIVSEP_MSG);
1741 break;
1744 imsg_free(&imsg);
1745 return err;
1748 const struct got_error *
1749 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1751 const struct got_error *err = NULL;
1752 struct imsg imsg;
1753 size_t datalen;
1754 const size_t min_datalen =
1755 MIN(sizeof(struct got_imsg_error), sizeof(int));
1757 *val = 0;
1759 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1760 if (err)
1761 return err;
1762 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1764 switch (imsg.hdr.type) {
1765 case GOT_IMSG_GITCONFIG_INT_VAL:
1766 if (datalen != sizeof(*val)) {
1767 err = got_error(GOT_ERR_PRIVSEP_LEN);
1768 break;
1770 memcpy(val, imsg.data, sizeof(*val));
1771 break;
1772 default:
1773 err = got_error(GOT_ERR_PRIVSEP_MSG);
1774 break;
1777 imsg_free(&imsg);
1778 return err;
1781 const struct got_error *
1782 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1783 int *nremotes, struct imsgbuf *ibuf)
1785 const struct got_error *err = NULL;
1786 struct imsg imsg;
1787 size_t datalen;
1788 struct got_imsg_remotes iremotes;
1789 struct got_imsg_remote iremote;
1791 *remotes = NULL;
1792 *nremotes = 0;
1793 iremotes.nremotes = 0;
1795 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1796 if (err)
1797 return err;
1798 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1800 switch (imsg.hdr.type) {
1801 case GOT_IMSG_GITCONFIG_REMOTES:
1802 if (datalen != sizeof(iremotes)) {
1803 err = got_error(GOT_ERR_PRIVSEP_LEN);
1804 break;
1806 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1807 if (iremotes.nremotes == 0) {
1808 imsg_free(&imsg);
1809 return NULL;
1811 break;
1812 default:
1813 imsg_free(&imsg);
1814 return got_error(GOT_ERR_PRIVSEP_MSG);
1817 imsg_free(&imsg);
1819 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
1820 if (*remotes == NULL)
1821 return got_error_from_errno("recallocarray");
1823 while (*nremotes < iremotes.nremotes) {
1824 struct got_remote_repo *remote;
1826 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1827 if (err)
1828 break;
1829 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1831 switch (imsg.hdr.type) {
1832 case GOT_IMSG_GITCONFIG_REMOTE:
1833 remote = &(*remotes)[*nremotes];
1834 if (datalen < sizeof(iremote)) {
1835 err = got_error(GOT_ERR_PRIVSEP_LEN);
1836 break;
1838 memcpy(&iremote, imsg.data, sizeof(iremote));
1839 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1840 (sizeof(iremote) + iremote.name_len +
1841 iremote.url_len) > datalen) {
1842 err = got_error(GOT_ERR_PRIVSEP_LEN);
1843 break;
1845 remote->name = strndup(imsg.data + sizeof(iremote),
1846 iremote.name_len);
1847 if (remote->name == NULL) {
1848 err = got_error_from_errno("strndup");
1849 break;
1851 remote->url = strndup(imsg.data + sizeof(iremote) +
1852 iremote.name_len, iremote.url_len);
1853 if (remote->url == NULL) {
1854 err = got_error_from_errno("strndup");
1855 free(remote->name);
1856 break;
1858 remote->mirror_references = iremote.mirror_references;
1859 (*nremotes)++;
1860 break;
1861 default:
1862 err = got_error(GOT_ERR_PRIVSEP_MSG);
1863 break;
1866 imsg_free(&imsg);
1867 if (err)
1868 break;
1871 if (err) {
1872 int i;
1873 for (i = 0; i < *nremotes; i++) {
1874 free((*remotes)[i].name);
1875 free((*remotes)[i].url);
1877 free(*remotes);
1878 *remotes = NULL;
1879 *nremotes = 0;
1881 return err;
1884 const struct got_error *
1885 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1886 struct got_object_id *id, int idx, const char *path)
1888 const struct got_error *err = NULL;
1889 struct ibuf *wbuf;
1890 size_t path_len = strlen(path) + 1;
1892 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1893 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1894 if (wbuf == NULL)
1895 return got_error_from_errno(
1896 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1897 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1898 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1899 ibuf_free(wbuf);
1900 return err;
1902 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1903 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1904 ibuf_free(wbuf);
1905 return err;
1907 if (imsg_add(wbuf, path, path_len) == -1) {
1908 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1909 ibuf_free(wbuf);
1910 return err;
1913 wbuf->fd = -1;
1914 imsg_close(ibuf, wbuf);
1916 return flush_imsg(ibuf);
1919 const struct got_error *
1920 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
1921 struct got_object_id **changed_commit_id,
1922 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
1924 const struct got_error *err = NULL;
1925 struct imsg imsg;
1926 struct got_imsg_traversed_commits *icommits;
1927 size_t datalen;
1928 int i, done = 0;
1930 *changed_commit = NULL;
1931 *changed_commit_id = NULL;
1933 while (!done) {
1934 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1935 if (err)
1936 return err;
1938 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1939 switch (imsg.hdr.type) {
1940 case GOT_IMSG_TRAVERSED_COMMITS:
1941 icommits = imsg.data;
1942 if (datalen != sizeof(*icommits) +
1943 icommits->ncommits * SHA1_DIGEST_LENGTH) {
1944 err = got_error(GOT_ERR_PRIVSEP_LEN);
1945 break;
1947 for (i = 0; i < icommits->ncommits; i++) {
1948 struct got_object_qid *qid;
1949 uint8_t *sha1 = (uint8_t *)imsg.data +
1950 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
1951 err = got_object_qid_alloc_partial(&qid);
1952 if (err)
1953 break;
1954 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
1955 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
1957 /* The last commit may contain a change. */
1958 if (i == icommits->ncommits - 1) {
1959 *changed_commit_id =
1960 got_object_id_dup(qid->id);
1961 if (*changed_commit_id == NULL) {
1962 err = got_error_from_errno(
1963 "got_object_id_dup");
1964 break;
1968 break;
1969 case GOT_IMSG_COMMIT:
1970 if (*changed_commit_id == NULL) {
1971 err = got_error(GOT_ERR_PRIVSEP_MSG);
1972 break;
1974 err = get_commit_from_imsg(changed_commit, &imsg,
1975 datalen, ibuf);
1976 break;
1977 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
1978 done = 1;
1979 break;
1980 default:
1981 err = got_error(GOT_ERR_PRIVSEP_MSG);
1982 break;
1985 imsg_free(&imsg);
1986 if (err)
1987 break;
1990 if (err)
1991 got_object_id_queue_free(commit_ids);
1992 return err;
1995 const struct got_error *
1996 got_privsep_unveil_exec_helpers(void)
1998 const char *helpers[] = {
1999 GOT_PATH_PROG_READ_PACK,
2000 GOT_PATH_PROG_READ_OBJECT,
2001 GOT_PATH_PROG_READ_COMMIT,
2002 GOT_PATH_PROG_READ_TREE,
2003 GOT_PATH_PROG_READ_BLOB,
2004 GOT_PATH_PROG_READ_TAG,
2005 GOT_PATH_PROG_READ_GITCONFIG,
2006 GOT_PATH_PROG_FETCH_PACK,
2007 GOT_PATH_PROG_INDEX_PACK,
2009 int i;
2011 for (i = 0; i < nitems(helpers); i++) {
2012 if (unveil(helpers[i], "x") == 0)
2013 continue;
2014 return got_error_from_errno2("unveil", helpers[i]);
2017 return NULL;
2020 void
2021 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2023 if (close(imsg_fds[0]) != 0) {
2024 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2025 _exit(1);
2028 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2029 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2030 _exit(1);
2032 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2033 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2034 _exit(1);
2037 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2038 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2039 strerror(errno));
2040 _exit(1);