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_send_stop(int fd)
221 const struct got_error *err = NULL;
222 struct imsgbuf ibuf;
224 imsg_init(&ibuf, fd);
226 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
227 return got_error_from_errno("imsg_compose STOP");
229 err = flush_imsg(&ibuf);
230 imsg_clear(&ibuf);
231 return err;
234 const struct got_error *
235 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
237 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
238 == -1)
239 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
241 return flush_imsg(ibuf);
244 const struct got_error *
245 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
246 struct got_object_id *id, int pack_idx)
248 const struct got_error *err = NULL;
249 struct got_imsg_packed_object iobj, *iobjp;
250 size_t len;
252 if (id) { /* commit is packed */
253 iobj.idx = pack_idx;
254 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
255 iobjp = &iobj;
256 len = sizeof(iobj);
257 } else {
258 iobjp = NULL;
259 len = 0;
262 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
263 == -1) {
264 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
265 close(fd);
266 return err;
269 return flush_imsg(ibuf);
272 const struct got_error *
273 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
274 struct got_object_id *id, int pack_idx)
276 const struct got_error *err = NULL;
277 struct ibuf *wbuf;
278 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
280 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
281 if (wbuf == NULL)
282 return got_error_from_errno("imsg_create TREE_REQUEST");
284 if (id) { /* tree is packed */
285 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
286 err = got_error_from_errno("imsg_add TREE_ENTRY");
287 ibuf_free(wbuf);
288 return err;
291 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
292 err = got_error_from_errno("imsg_add TREE_ENTRY");
293 ibuf_free(wbuf);
294 return err;
298 wbuf->fd = fd;
299 imsg_close(ibuf, wbuf);
301 return flush_imsg(ibuf);
304 const struct got_error *
305 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
306 struct got_object_id *id, int pack_idx)
308 struct got_imsg_packed_object iobj, *iobjp;
309 size_t len;
311 if (id) { /* tag is packed */
312 iobj.idx = pack_idx;
313 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
314 iobjp = &iobj;
315 len = sizeof(iobj);
316 } else {
317 iobjp = NULL;
318 len = 0;
321 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
322 == -1)
323 return got_error_from_errno("imsg_compose TAG_REQUEST");
325 return flush_imsg(ibuf);
328 const struct got_error *
329 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
330 struct got_object_id *id, int pack_idx)
332 const struct got_error *err = NULL;
333 struct got_imsg_packed_object iobj, *iobjp;
334 size_t len;
336 if (id) { /* blob is packed */
337 iobj.idx = pack_idx;
338 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
339 iobjp = &iobj;
340 len = sizeof(iobj);
341 } else {
342 iobjp = NULL;
343 len = 0;
346 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
347 == -1) {
348 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
349 close(infd);
350 return err;
353 return flush_imsg(ibuf);
356 const struct got_error *
357 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
359 const struct got_error *err = NULL;
361 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
362 == -1) {
363 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
364 close(outfd);
365 return err;
368 return flush_imsg(ibuf);
371 static const struct got_error *
372 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
374 const struct got_error *err = NULL;
376 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
377 err = got_error_from_errno("imsg_compose TMPFD");
378 close(fd);
379 return err;
382 return flush_imsg(ibuf);
385 const struct got_error *
386 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
388 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
391 const struct got_error *
392 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
394 struct got_imsg_object iobj;
396 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
397 iobj.type = obj->type;
398 iobj.flags = obj->flags;
399 iobj.hdrlen = obj->hdrlen;
400 iobj.size = obj->size;
401 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
402 iobj.pack_offset = obj->pack_offset;
403 iobj.pack_idx = obj->pack_idx;
406 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
407 == -1)
408 return got_error_from_errno("imsg_compose OBJECT");
410 return flush_imsg(ibuf);
413 const struct got_error *
414 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
415 struct got_pathlist_head *have_refs, int fetch_all_branches,
416 struct got_pathlist_head *wanted_branches, int list_refs_only,
417 int verbosity)
419 const struct got_error *err = NULL;
420 struct ibuf *wbuf;
421 size_t len;
422 struct got_pathlist_entry *pe;
423 struct got_imsg_fetch_request fetchreq;
425 memset(&fetchreq, 0, sizeof(fetchreq));
426 fetchreq.fetch_all_branches = fetch_all_branches;
427 fetchreq.list_refs_only = list_refs_only;
428 fetchreq.verbosity = verbosity;
429 TAILQ_FOREACH(pe, have_refs, entry)
430 fetchreq.n_have_refs++;
431 TAILQ_FOREACH(pe, wanted_branches, entry)
432 fetchreq.n_wanted_branches++;
433 len = sizeof(struct got_imsg_fetch_request);
434 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
435 close(fd);
436 return got_error(GOT_ERR_NO_SPACE);
439 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
440 &fetchreq, sizeof(fetchreq)) == -1)
441 return got_error_from_errno(
442 "imsg_compose FETCH_SERVER_PROGRESS");
444 err = flush_imsg(ibuf);
445 if (err) {
446 close(fd);
447 return err;
449 fd = -1;
451 TAILQ_FOREACH(pe, have_refs, entry) {
452 const char *name = pe->path;
453 size_t name_len = pe->path_len;
454 struct got_object_id *id = pe->data;
456 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
457 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
458 if (wbuf == NULL)
459 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
461 /* Keep in sync with struct got_imsg_fetch_have_ref! */
462 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
463 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
464 ibuf_free(wbuf);
465 return err;
467 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
468 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
469 ibuf_free(wbuf);
470 return err;
472 if (imsg_add(wbuf, name, name_len) == -1) {
473 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
474 ibuf_free(wbuf);
475 return err;
478 wbuf->fd = -1;
479 imsg_close(ibuf, wbuf);
480 err = flush_imsg(ibuf);
481 if (err)
482 return err;
485 TAILQ_FOREACH(pe, wanted_branches, entry) {
486 const char *name = pe->path;
487 size_t name_len = pe->path_len;
489 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
490 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
491 len);
492 if (wbuf == NULL)
493 return got_error_from_errno(
494 "imsg_create FETCH_WANTED_BRANCH");
496 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
497 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
498 err = got_error_from_errno(
499 "imsg_add FETCH_WANTED_BRANCH");
500 ibuf_free(wbuf);
501 return err;
503 if (imsg_add(wbuf, name, name_len) == -1) {
504 err = got_error_from_errno(
505 "imsg_add FETCH_WANTED_BRANCH");
506 ibuf_free(wbuf);
507 return err;
510 wbuf->fd = -1;
511 imsg_close(ibuf, wbuf);
512 err = flush_imsg(ibuf);
513 if (err)
514 return err;
517 return NULL;
521 const struct got_error *
522 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
524 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
528 const struct got_error *
529 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
530 struct got_pathlist_head *symrefs)
532 const struct got_error *err = NULL;
533 struct ibuf *wbuf;
534 size_t len, nsymrefs = 0;
535 struct got_pathlist_entry *pe;
537 len = sizeof(struct got_imsg_fetch_symrefs);
538 TAILQ_FOREACH(pe, symrefs, entry) {
539 const char *target = pe->data;
540 len += sizeof(struct got_imsg_fetch_symref) +
541 pe->path_len + strlen(target);
542 nsymrefs++;
545 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
546 return got_error(GOT_ERR_NO_SPACE);
548 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
549 if (wbuf == NULL)
550 return got_error_from_errno("imsg_create FETCH_SYMREFS");
552 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
553 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
554 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
555 ibuf_free(wbuf);
556 return err;
559 TAILQ_FOREACH(pe, symrefs, entry) {
560 const char *name = pe->path;
561 size_t name_len = pe->path_len;
562 const char *target = pe->data;
563 size_t target_len = strlen(target);
565 /* Keep in sync with struct got_imsg_fetch_symref definition! */
566 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
567 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
568 ibuf_free(wbuf);
569 return err;
571 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
572 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
573 ibuf_free(wbuf);
574 return err;
576 if (imsg_add(wbuf, name, name_len) == -1) {
577 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
578 ibuf_free(wbuf);
579 return err;
581 if (imsg_add(wbuf, target, target_len) == -1) {
582 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
583 ibuf_free(wbuf);
584 return err;
588 wbuf->fd = -1;
589 imsg_close(ibuf, wbuf);
590 return flush_imsg(ibuf);
593 const struct got_error *
594 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
595 struct got_object_id *refid, const char *refname)
597 const struct got_error *err = NULL;
598 struct ibuf *wbuf;
599 size_t len, reflen = strlen(refname);
601 len = sizeof(struct got_imsg_fetch_ref) + reflen;
602 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
603 return got_error(GOT_ERR_NO_SPACE);
605 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
606 if (wbuf == NULL)
607 return got_error_from_errno("imsg_create FETCH_REF");
609 /* Keep in sync with struct got_imsg_fetch_ref definition! */
610 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
611 err = got_error_from_errno("imsg_add FETCH_REF");
612 ibuf_free(wbuf);
613 return err;
615 if (imsg_add(wbuf, refname, reflen) == -1) {
616 err = got_error_from_errno("imsg_add FETCH_REF");
617 ibuf_free(wbuf);
618 return err;
621 wbuf->fd = -1;
622 imsg_close(ibuf, wbuf);
623 return flush_imsg(ibuf);
626 const struct got_error *
627 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
628 size_t msglen)
630 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
631 return got_error(GOT_ERR_NO_SPACE);
633 if (msglen == 0)
634 return NULL;
636 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
637 msg, msglen) == -1)
638 return got_error_from_errno(
639 "imsg_compose FETCH_SERVER_PROGRESS");
641 return flush_imsg(ibuf);
644 const struct got_error *
645 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
647 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
648 &bytes, sizeof(bytes)) == -1)
649 return got_error_from_errno(
650 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
652 return flush_imsg(ibuf);
655 const struct got_error *
656 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
658 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
659 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
660 return got_error_from_errno("imsg_compose FETCH");
661 return flush_imsg(ibuf);
665 const struct got_error *
666 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
667 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
668 off_t *packfile_size, struct imsgbuf *ibuf)
670 const struct got_error *err = NULL;
671 struct imsg imsg;
672 size_t datalen;
673 struct got_imsg_fetch_symrefs *isymrefs = NULL;
674 size_t n, remain;
675 off_t off;
676 int i;
678 *done = 0;
679 *id = NULL;
680 *refname = NULL;
681 *server_progress = NULL;
682 *packfile_size = 0;
684 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
685 if (err)
686 return err;
688 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
689 switch (imsg.hdr.type) {
690 case GOT_IMSG_ERROR:
691 if (datalen < sizeof(struct got_imsg_error)) {
692 err = got_error(GOT_ERR_PRIVSEP_LEN);
693 break;
695 err = recv_imsg_error(&imsg, datalen);
696 break;
697 case GOT_IMSG_FETCH_SYMREFS:
698 if (datalen < sizeof(*isymrefs)) {
699 err = got_error(GOT_ERR_PRIVSEP_LEN);
700 break;
702 if (isymrefs != NULL) {
703 err = got_error(GOT_ERR_PRIVSEP_MSG);
704 break;
706 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
707 off = sizeof(*isymrefs);
708 remain = datalen - off;
709 for (n = 0; n < isymrefs->nsymrefs; n++) {
710 struct got_imsg_fetch_symref *s;
711 char *name, *target;
712 if (remain < sizeof(struct got_imsg_fetch_symref)) {
713 err = got_error(GOT_ERR_PRIVSEP_LEN);
714 goto done;
716 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
717 off += sizeof(*s);
718 remain -= sizeof(*s);
719 if (remain < s->name_len) {
720 err = got_error(GOT_ERR_PRIVSEP_LEN);
721 goto done;
723 name = strndup(imsg.data + off, s->name_len);
724 if (name == NULL) {
725 err = got_error_from_errno("strndup");
726 goto done;
728 off += s->name_len;
729 remain -= s->name_len;
730 if (remain < s->target_len) {
731 err = got_error(GOT_ERR_PRIVSEP_LEN);
732 free(name);
733 goto done;
735 target = strndup(imsg.data + off, s->target_len);
736 if (target == NULL) {
737 err = got_error_from_errno("strndup");
738 free(name);
739 goto done;
741 off += s->target_len;
742 remain -= s->target_len;
743 err = got_pathlist_append(symrefs, name, target);
744 if (err) {
745 free(name);
746 free(target);
747 goto done;
750 break;
751 case GOT_IMSG_FETCH_REF:
752 if (datalen <= SHA1_DIGEST_LENGTH) {
753 err = got_error(GOT_ERR_PRIVSEP_MSG);
754 break;
756 *id = malloc(sizeof(**id));
757 if (*id == NULL) {
758 err = got_error_from_errno("malloc");
759 break;
761 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
762 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
763 datalen - SHA1_DIGEST_LENGTH);
764 if (*refname == NULL) {
765 err = got_error_from_errno("strndup");
766 break;
768 break;
769 case GOT_IMSG_FETCH_SERVER_PROGRESS:
770 if (datalen == 0) {
771 err = got_error(GOT_ERR_PRIVSEP_LEN);
772 break;
774 *server_progress = strndup(imsg.data, datalen);
775 if (*server_progress == NULL) {
776 err = got_error_from_errno("strndup");
777 break;
779 for (i = 0; i < datalen; i++) {
780 if (!isprint((unsigned char)(*server_progress)[i]) &&
781 !isspace((unsigned char)(*server_progress)[i])) {
782 err = got_error(GOT_ERR_PRIVSEP_MSG);
783 free(*server_progress);
784 *server_progress = NULL;
785 goto done;
788 break;
789 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
790 if (datalen < sizeof(*packfile_size)) {
791 err = got_error(GOT_ERR_PRIVSEP_MSG);
792 break;
794 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
795 break;
796 case GOT_IMSG_FETCH_DONE:
797 *id = malloc(sizeof(**id));
798 if (*id == NULL) {
799 err = got_error_from_errno("malloc");
800 break;
802 if (datalen != SHA1_DIGEST_LENGTH) {
803 err = got_error(GOT_ERR_PRIVSEP_MSG);
804 break;
806 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
807 *done = 1;
808 break;
809 default:
810 err = got_error(GOT_ERR_PRIVSEP_MSG);
811 break;
813 done:
814 if (err) {
815 free(*id);
816 *id = NULL;
817 free(*refname);
818 *refname = NULL;
820 imsg_free(&imsg);
821 return err;
824 const struct got_error *
825 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
826 int fd)
828 const struct got_error *err = NULL;
830 /* Keep in sync with struct got_imsg_index_pack_request */
831 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
832 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
833 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
834 close(fd);
835 return err;
837 return flush_imsg(ibuf);
840 const struct got_error *
841 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
843 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
846 const struct got_error *
847 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
848 int nobj_indexed, int nobj_loose, int nobj_resolved)
850 struct got_imsg_index_pack_progress iprogress;
852 iprogress.nobj_total = nobj_total;
853 iprogress.nobj_indexed = nobj_indexed;
854 iprogress.nobj_loose = nobj_loose;
855 iprogress.nobj_resolved = nobj_resolved;
857 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
858 &iprogress, sizeof(iprogress)) == -1)
859 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
861 return flush_imsg(ibuf);
864 const struct got_error *
865 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
867 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
868 return got_error_from_errno("imsg_compose FETCH");
869 return flush_imsg(ibuf);
872 const struct got_error *
873 got_privsep_recv_index_progress(int *done, int *nobj_total,
874 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
875 struct imsgbuf *ibuf)
877 const struct got_error *err = NULL;
878 struct imsg imsg;
879 struct got_imsg_index_pack_progress *iprogress;
880 size_t datalen;
882 *done = 0;
883 *nobj_total = 0;
884 *nobj_indexed = 0;
885 *nobj_resolved = 0;
887 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
888 if (err)
889 return err;
891 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
892 switch (imsg.hdr.type) {
893 case GOT_IMSG_ERROR:
894 if (datalen < sizeof(struct got_imsg_error)) {
895 err = got_error(GOT_ERR_PRIVSEP_LEN);
896 break;
898 err = recv_imsg_error(&imsg, datalen);
899 break;
900 case GOT_IMSG_IDXPACK_PROGRESS:
901 if (datalen < sizeof(*iprogress)) {
902 err = got_error(GOT_ERR_PRIVSEP_LEN);
903 break;
905 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
906 *nobj_total = iprogress->nobj_total;
907 *nobj_indexed = iprogress->nobj_indexed;
908 *nobj_loose = iprogress->nobj_loose;
909 *nobj_resolved = iprogress->nobj_resolved;
910 break;
911 case GOT_IMSG_IDXPACK_DONE:
912 if (datalen != 0) {
913 err = got_error(GOT_ERR_PRIVSEP_LEN);
914 break;
916 *done = 1;
917 break;
918 default:
919 err = got_error(GOT_ERR_PRIVSEP_MSG);
920 break;
923 imsg_free(&imsg);
924 return err;
927 const struct got_error *
928 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
929 struct imsgbuf *ibuf)
931 const struct got_error *err = NULL;
932 struct got_imsg_object *iobj;
933 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
935 if (datalen != sizeof(*iobj))
936 return got_error(GOT_ERR_PRIVSEP_LEN);
937 iobj = imsg->data;
939 *obj = calloc(1, sizeof(**obj));
940 if (*obj == NULL)
941 return got_error_from_errno("calloc");
943 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
944 (*obj)->type = iobj->type;
945 (*obj)->flags = iobj->flags;
946 (*obj)->hdrlen = iobj->hdrlen;
947 (*obj)->size = iobj->size;
948 /* path_packfile is handled by caller */
949 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
950 (*obj)->pack_offset = iobj->pack_offset;
951 (*obj)->pack_idx = iobj->pack_idx;
954 return err;
957 const struct got_error *
958 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
960 const struct got_error *err = NULL;
961 struct imsg imsg;
962 const size_t min_datalen =
963 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
965 *obj = NULL;
967 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
968 if (err)
969 return err;
971 switch (imsg.hdr.type) {
972 case GOT_IMSG_OBJECT:
973 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
974 break;
975 default:
976 err = got_error(GOT_ERR_PRIVSEP_MSG);
977 break;
980 imsg_free(&imsg);
982 return err;
985 static const struct got_error *
986 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
987 size_t logmsg_len)
989 const struct got_error *err = NULL;
990 size_t offset, remain;
992 offset = 0;
993 remain = logmsg_len;
994 while (remain > 0) {
995 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
997 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
998 commit->logmsg + offset, n) == -1) {
999 err = got_error_from_errno("imsg_compose "
1000 "COMMIT_LOGMSG");
1001 break;
1004 err = flush_imsg(ibuf);
1005 if (err)
1006 break;
1008 offset += n;
1009 remain -= n;
1012 return err;
1015 const struct got_error *
1016 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1018 const struct got_error *err = NULL;
1019 struct got_imsg_commit_object *icommit;
1020 uint8_t *buf;
1021 size_t len, total;
1022 struct got_object_qid *qid;
1023 size_t author_len = strlen(commit->author);
1024 size_t committer_len = strlen(commit->committer);
1025 size_t logmsg_len = strlen(commit->logmsg);
1027 total = sizeof(*icommit) + author_len + committer_len +
1028 commit->nparents * SHA1_DIGEST_LENGTH;
1030 buf = malloc(total);
1031 if (buf == NULL)
1032 return got_error_from_errno("malloc");
1034 icommit = (struct got_imsg_commit_object *)buf;
1035 memcpy(icommit->tree_id, commit->tree_id->sha1,
1036 sizeof(icommit->tree_id));
1037 icommit->author_len = author_len;
1038 icommit->author_time = commit->author_time;
1039 icommit->author_gmtoff = commit->author_gmtoff;
1040 icommit->committer_len = committer_len;
1041 icommit->committer_time = commit->committer_time;
1042 icommit->committer_gmtoff = commit->committer_gmtoff;
1043 icommit->logmsg_len = logmsg_len;
1044 icommit->nparents = commit->nparents;
1046 len = sizeof(*icommit);
1047 memcpy(buf + len, commit->author, author_len);
1048 len += author_len;
1049 memcpy(buf + len, commit->committer, committer_len);
1050 len += committer_len;
1051 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1052 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1053 len += SHA1_DIGEST_LENGTH;
1056 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1057 err = got_error_from_errno("imsg_compose COMMIT");
1058 goto done;
1061 if (logmsg_len == 0 ||
1062 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1063 err = flush_imsg(ibuf);
1064 if (err)
1065 goto done;
1067 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1068 done:
1069 free(buf);
1070 return err;
1073 static const struct got_error *
1074 get_commit_from_imsg(struct got_commit_object **commit,
1075 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1077 const struct got_error *err = NULL;
1078 struct got_imsg_commit_object *icommit;
1079 size_t len = 0;
1080 int i;
1082 if (datalen < sizeof(*icommit))
1083 return got_error(GOT_ERR_PRIVSEP_LEN);
1085 icommit = imsg->data;
1086 if (datalen != sizeof(*icommit) + icommit->author_len +
1087 icommit->committer_len +
1088 icommit->nparents * SHA1_DIGEST_LENGTH)
1089 return got_error(GOT_ERR_PRIVSEP_LEN);
1091 if (icommit->nparents < 0)
1092 return got_error(GOT_ERR_PRIVSEP_LEN);
1094 len += sizeof(*icommit);
1096 *commit = got_object_commit_alloc_partial();
1097 if (*commit == NULL)
1098 return got_error_from_errno(
1099 "got_object_commit_alloc_partial");
1101 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1102 SHA1_DIGEST_LENGTH);
1103 (*commit)->author_time = icommit->author_time;
1104 (*commit)->author_gmtoff = icommit->author_gmtoff;
1105 (*commit)->committer_time = icommit->committer_time;
1106 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1108 if (icommit->author_len == 0) {
1109 (*commit)->author = strdup("");
1110 if ((*commit)->author == NULL) {
1111 err = got_error_from_errno("strdup");
1112 goto done;
1114 } else {
1115 (*commit)->author = malloc(icommit->author_len + 1);
1116 if ((*commit)->author == NULL) {
1117 err = got_error_from_errno("malloc");
1118 goto done;
1120 memcpy((*commit)->author, imsg->data + len,
1121 icommit->author_len);
1122 (*commit)->author[icommit->author_len] = '\0';
1124 len += icommit->author_len;
1126 if (icommit->committer_len == 0) {
1127 (*commit)->committer = strdup("");
1128 if ((*commit)->committer == NULL) {
1129 err = got_error_from_errno("strdup");
1130 goto done;
1132 } else {
1133 (*commit)->committer =
1134 malloc(icommit->committer_len + 1);
1135 if ((*commit)->committer == NULL) {
1136 err = got_error_from_errno("malloc");
1137 goto done;
1139 memcpy((*commit)->committer, imsg->data + len,
1140 icommit->committer_len);
1141 (*commit)->committer[icommit->committer_len] = '\0';
1143 len += icommit->committer_len;
1145 if (icommit->logmsg_len == 0) {
1146 (*commit)->logmsg = strdup("");
1147 if ((*commit)->logmsg == NULL) {
1148 err = got_error_from_errno("strdup");
1149 goto done;
1151 } else {
1152 size_t offset = 0, remain = icommit->logmsg_len;
1154 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1155 if ((*commit)->logmsg == NULL) {
1156 err = got_error_from_errno("malloc");
1157 goto done;
1159 while (remain > 0) {
1160 struct imsg imsg_log;
1161 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1162 remain);
1164 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1165 if (err)
1166 goto done;
1168 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1169 err = got_error(GOT_ERR_PRIVSEP_MSG);
1170 goto done;
1173 memcpy((*commit)->logmsg + offset,
1174 imsg_log.data, n);
1175 imsg_free(&imsg_log);
1176 offset += n;
1177 remain -= n;
1179 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1182 for (i = 0; i < icommit->nparents; i++) {
1183 struct got_object_qid *qid;
1185 err = got_object_qid_alloc_partial(&qid);
1186 if (err)
1187 break;
1188 memcpy(qid->id, imsg->data + len +
1189 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1190 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1191 (*commit)->nparents++;
1193 done:
1194 if (err) {
1195 got_object_commit_close(*commit);
1196 *commit = NULL;
1198 return err;
1201 const struct got_error *
1202 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1204 const struct got_error *err = NULL;
1205 struct imsg imsg;
1206 size_t datalen;
1207 const size_t min_datalen =
1208 MIN(sizeof(struct got_imsg_error),
1209 sizeof(struct got_imsg_commit_object));
1211 *commit = NULL;
1213 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1214 if (err)
1215 return err;
1217 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1219 switch (imsg.hdr.type) {
1220 case GOT_IMSG_COMMIT:
1221 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1222 break;
1223 default:
1224 err = got_error(GOT_ERR_PRIVSEP_MSG);
1225 break;
1228 imsg_free(&imsg);
1230 return err;
1233 const struct got_error *
1234 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1235 int nentries)
1237 const struct got_error *err = NULL;
1238 struct got_imsg_tree_object itree;
1239 struct got_pathlist_entry *pe;
1240 size_t totlen;
1241 int nimsg; /* number of imsg queued in ibuf */
1243 itree.nentries = nentries;
1244 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1245 == -1)
1246 return got_error_from_errno("imsg_compose TREE");
1248 totlen = sizeof(itree);
1249 nimsg = 1;
1250 TAILQ_FOREACH(pe, entries, entry) {
1251 const char *name = pe->path;
1252 struct got_parsed_tree_entry *pte = pe->data;
1253 struct ibuf *wbuf;
1254 size_t namelen = strlen(name);
1255 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1257 if (len > MAX_IMSGSIZE)
1258 return got_error(GOT_ERR_NO_SPACE);
1260 nimsg++;
1261 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1262 err = flush_imsg(ibuf);
1263 if (err)
1264 return err;
1265 nimsg = 0;
1268 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1269 if (wbuf == NULL)
1270 return got_error_from_errno("imsg_create TREE_ENTRY");
1272 /* Keep in sync with struct got_imsg_tree_object definition! */
1273 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1274 err = got_error_from_errno("imsg_add TREE_ENTRY");
1275 ibuf_free(wbuf);
1276 return err;
1278 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1279 err = got_error_from_errno("imsg_add TREE_ENTRY");
1280 ibuf_free(wbuf);
1281 return err;
1284 if (imsg_add(wbuf, name, namelen) == -1) {
1285 err = got_error_from_errno("imsg_add TREE_ENTRY");
1286 ibuf_free(wbuf);
1287 return err;
1290 wbuf->fd = -1;
1291 imsg_close(ibuf, wbuf);
1293 totlen += len;
1296 return flush_imsg(ibuf);
1299 const struct got_error *
1300 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1302 const struct got_error *err = NULL;
1303 const size_t min_datalen =
1304 MIN(sizeof(struct got_imsg_error),
1305 sizeof(struct got_imsg_tree_object));
1306 struct got_imsg_tree_object *itree;
1307 int nentries = 0;
1309 *tree = NULL;
1310 get_more:
1311 err = read_imsg(ibuf);
1312 if (err)
1313 goto done;
1315 for (;;) {
1316 struct imsg imsg;
1317 size_t n;
1318 size_t datalen;
1319 struct got_imsg_tree_entry *ite;
1320 struct got_tree_entry *te = NULL;
1322 n = imsg_get(ibuf, &imsg);
1323 if (n == 0) {
1324 if (*tree && (*tree)->nentries != nentries)
1325 goto get_more;
1326 break;
1329 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1330 return got_error(GOT_ERR_PRIVSEP_LEN);
1332 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1334 switch (imsg.hdr.type) {
1335 case GOT_IMSG_ERROR:
1336 err = recv_imsg_error(&imsg, datalen);
1337 break;
1338 case GOT_IMSG_TREE:
1339 /* This message should only appear once. */
1340 if (*tree != NULL) {
1341 err = got_error(GOT_ERR_PRIVSEP_MSG);
1342 break;
1344 if (datalen != sizeof(*itree)) {
1345 err = got_error(GOT_ERR_PRIVSEP_LEN);
1346 break;
1348 itree = imsg.data;
1349 *tree = malloc(sizeof(**tree));
1350 if (*tree == NULL) {
1351 err = got_error_from_errno("malloc");
1352 break;
1354 (*tree)->entries = calloc(itree->nentries,
1355 sizeof(struct got_tree_entry));
1356 if ((*tree)->entries == NULL) {
1357 err = got_error_from_errno("malloc");
1358 break;
1360 (*tree)->nentries = itree->nentries;
1361 (*tree)->refcnt = 0;
1362 break;
1363 case GOT_IMSG_TREE_ENTRY:
1364 /* This message should be preceeded by GOT_IMSG_TREE. */
1365 if (*tree == NULL) {
1366 err = got_error(GOT_ERR_PRIVSEP_MSG);
1367 break;
1369 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1370 err = got_error(GOT_ERR_PRIVSEP_LEN);
1371 break;
1374 /* Remaining data contains the entry's name. */
1375 datalen -= sizeof(*ite);
1376 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1377 err = got_error(GOT_ERR_PRIVSEP_LEN);
1378 break;
1380 ite = imsg.data;
1382 if (datalen + 1 > sizeof(te->name)) {
1383 err = got_error(GOT_ERR_NO_SPACE);
1384 break;
1386 te = &(*tree)->entries[nentries];
1387 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1388 te->name[datalen] = '\0';
1390 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1391 te->mode = ite->mode;
1392 te->idx = nentries;
1393 nentries++;
1394 break;
1395 default:
1396 err = got_error(GOT_ERR_PRIVSEP_MSG);
1397 break;
1400 imsg_free(&imsg);
1402 done:
1403 if (*tree && (*tree)->nentries != nentries) {
1404 if (err == NULL)
1405 err = got_error(GOT_ERR_PRIVSEP_LEN);
1406 got_object_tree_close(*tree);
1407 *tree = NULL;
1410 return err;
1413 const struct got_error *
1414 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1415 const uint8_t *data)
1417 struct got_imsg_blob iblob;
1419 iblob.size = size;
1420 iblob.hdrlen = hdrlen;
1422 if (data) {
1423 uint8_t *buf;
1425 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1426 return got_error(GOT_ERR_NO_SPACE);
1428 buf = malloc(sizeof(iblob) + size);
1429 if (buf == NULL)
1430 return got_error_from_errno("malloc");
1432 memcpy(buf, &iblob, sizeof(iblob));
1433 memcpy(buf + sizeof(iblob), data, size);
1434 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1435 sizeof(iblob) + size) == -1) {
1436 free(buf);
1437 return got_error_from_errno("imsg_compose BLOB");
1439 free(buf);
1440 } else {
1441 /* Data has already been written to file descriptor. */
1442 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1443 sizeof(iblob)) == -1)
1444 return got_error_from_errno("imsg_compose BLOB");
1448 return flush_imsg(ibuf);
1451 const struct got_error *
1452 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1453 struct imsgbuf *ibuf)
1455 const struct got_error *err = NULL;
1456 struct imsg imsg;
1457 struct got_imsg_blob *iblob;
1458 size_t datalen;
1460 *outbuf = NULL;
1462 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1463 if (err)
1464 return err;
1466 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1468 switch (imsg.hdr.type) {
1469 case GOT_IMSG_BLOB:
1470 if (datalen < sizeof(*iblob)) {
1471 err = got_error(GOT_ERR_PRIVSEP_LEN);
1472 break;
1474 iblob = imsg.data;
1475 *size = iblob->size;
1476 *hdrlen = iblob->hdrlen;
1478 if (datalen == sizeof(*iblob)) {
1479 /* Data has been written to file descriptor. */
1480 break;
1483 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1484 err = got_error(GOT_ERR_PRIVSEP_LEN);
1485 break;
1488 *outbuf = malloc(*size);
1489 if (*outbuf == NULL) {
1490 err = got_error_from_errno("malloc");
1491 break;
1493 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1494 break;
1495 default:
1496 err = got_error(GOT_ERR_PRIVSEP_MSG);
1497 break;
1500 imsg_free(&imsg);
1502 return err;
1505 static const struct got_error *
1506 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1508 const struct got_error *err = NULL;
1509 size_t offset, remain;
1511 offset = 0;
1512 remain = tagmsg_len;
1513 while (remain > 0) {
1514 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1516 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1517 tag->tagmsg + offset, n) == -1) {
1518 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1519 break;
1522 err = flush_imsg(ibuf);
1523 if (err)
1524 break;
1526 offset += n;
1527 remain -= n;
1530 return err;
1533 const struct got_error *
1534 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1536 const struct got_error *err = NULL;
1537 struct got_imsg_tag_object *itag;
1538 uint8_t *buf;
1539 size_t len, total;
1540 size_t tag_len = strlen(tag->tag);
1541 size_t tagger_len = strlen(tag->tagger);
1542 size_t tagmsg_len = strlen(tag->tagmsg);
1544 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1546 buf = malloc(total);
1547 if (buf == NULL)
1548 return got_error_from_errno("malloc");
1550 itag = (struct got_imsg_tag_object *)buf;
1551 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1552 itag->obj_type = tag->obj_type;
1553 itag->tag_len = tag_len;
1554 itag->tagger_len = tagger_len;
1555 itag->tagger_time = tag->tagger_time;
1556 itag->tagger_gmtoff = tag->tagger_gmtoff;
1557 itag->tagmsg_len = tagmsg_len;
1559 len = sizeof(*itag);
1560 memcpy(buf + len, tag->tag, tag_len);
1561 len += tag_len;
1562 memcpy(buf + len, tag->tagger, tagger_len);
1563 len += tagger_len;
1565 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1566 err = got_error_from_errno("imsg_compose TAG");
1567 goto done;
1570 if (tagmsg_len == 0 ||
1571 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1572 err = flush_imsg(ibuf);
1573 if (err)
1574 goto done;
1576 err = send_tagmsg(ibuf, tag, tagmsg_len);
1577 done:
1578 free(buf);
1579 return err;
1582 const struct got_error *
1583 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1585 const struct got_error *err = NULL;
1586 struct imsg imsg;
1587 struct got_imsg_tag_object *itag;
1588 size_t len, datalen;
1589 const size_t min_datalen =
1590 MIN(sizeof(struct got_imsg_error),
1591 sizeof(struct got_imsg_tag_object));
1593 *tag = NULL;
1595 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1596 if (err)
1597 return err;
1599 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1600 len = 0;
1602 switch (imsg.hdr.type) {
1603 case GOT_IMSG_TAG:
1604 if (datalen < sizeof(*itag)) {
1605 err = got_error(GOT_ERR_PRIVSEP_LEN);
1606 break;
1608 itag = imsg.data;
1609 if (datalen != sizeof(*itag) + itag->tag_len +
1610 itag->tagger_len) {
1611 err = got_error(GOT_ERR_PRIVSEP_LEN);
1612 break;
1614 len += sizeof(*itag);
1616 *tag = calloc(1, sizeof(**tag));
1617 if (*tag == NULL) {
1618 err = got_error_from_errno("calloc");
1619 break;
1622 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1624 if (itag->tag_len == 0) {
1625 (*tag)->tag = strdup("");
1626 if ((*tag)->tag == NULL) {
1627 err = got_error_from_errno("strdup");
1628 break;
1630 } else {
1631 (*tag)->tag = malloc(itag->tag_len + 1);
1632 if ((*tag)->tag == NULL) {
1633 err = got_error_from_errno("malloc");
1634 break;
1636 memcpy((*tag)->tag, imsg.data + len,
1637 itag->tag_len);
1638 (*tag)->tag[itag->tag_len] = '\0';
1640 len += itag->tag_len;
1642 (*tag)->obj_type = itag->obj_type;
1643 (*tag)->tagger_time = itag->tagger_time;
1644 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1646 if (itag->tagger_len == 0) {
1647 (*tag)->tagger = strdup("");
1648 if ((*tag)->tagger == NULL) {
1649 err = got_error_from_errno("strdup");
1650 break;
1652 } else {
1653 (*tag)->tagger = malloc(itag->tagger_len + 1);
1654 if ((*tag)->tagger == NULL) {
1655 err = got_error_from_errno("malloc");
1656 break;
1658 memcpy((*tag)->tagger, imsg.data + len,
1659 itag->tagger_len);
1660 (*tag)->tagger[itag->tagger_len] = '\0';
1662 len += itag->tagger_len;
1664 if (itag->tagmsg_len == 0) {
1665 (*tag)->tagmsg = strdup("");
1666 if ((*tag)->tagmsg == NULL) {
1667 err = got_error_from_errno("strdup");
1668 break;
1670 } else {
1671 size_t offset = 0, remain = itag->tagmsg_len;
1673 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1674 if ((*tag)->tagmsg == NULL) {
1675 err = got_error_from_errno("malloc");
1676 break;
1678 while (remain > 0) {
1679 struct imsg imsg_log;
1680 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1681 remain);
1683 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1684 if (err)
1685 return err;
1687 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1688 return got_error(GOT_ERR_PRIVSEP_MSG);
1690 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1691 n);
1692 imsg_free(&imsg_log);
1693 offset += n;
1694 remain -= n;
1696 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1699 break;
1700 default:
1701 err = got_error(GOT_ERR_PRIVSEP_MSG);
1702 break;
1705 imsg_free(&imsg);
1707 return err;
1710 const struct got_error *
1711 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1712 struct got_packidx *packidx)
1714 const struct got_error *err = NULL;
1715 struct got_imsg_packidx ipackidx;
1716 struct got_imsg_pack ipack;
1717 int fd;
1719 ipackidx.len = packidx->len;
1720 fd = dup(packidx->fd);
1721 if (fd == -1)
1722 return got_error_from_errno("dup");
1724 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1725 sizeof(ipackidx)) == -1) {
1726 err = got_error_from_errno("imsg_compose PACKIDX");
1727 close(fd);
1728 return err;
1731 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1732 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1733 return got_error(GOT_ERR_NO_SPACE);
1734 ipack.filesize = pack->filesize;
1736 fd = dup(pack->fd);
1737 if (fd == -1)
1738 return got_error_from_errno("dup");
1740 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1741 == -1) {
1742 err = got_error_from_errno("imsg_compose PACK");
1743 close(fd);
1744 return err;
1747 return flush_imsg(ibuf);
1750 const struct got_error *
1751 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1752 struct got_object_id *id)
1754 struct got_imsg_packed_object iobj;
1756 iobj.idx = idx;
1757 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1759 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1760 &iobj, sizeof(iobj)) == -1)
1761 return got_error_from_errno("imsg_compose "
1762 "PACKED_OBJECT_REQUEST");
1764 return flush_imsg(ibuf);
1767 const struct got_error *
1768 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1770 const struct got_error *err = NULL;
1772 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1773 NULL, 0) == -1) {
1774 err = got_error_from_errno("imsg_compose "
1775 "GITCONFIG_PARSE_REQUEST");
1776 close(fd);
1777 return err;
1780 return flush_imsg(ibuf);
1783 const struct got_error *
1784 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1786 if (imsg_compose(ibuf,
1787 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1788 NULL, 0) == -1)
1789 return got_error_from_errno("imsg_compose "
1790 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1792 return flush_imsg(ibuf);
1795 const struct got_error *
1796 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1798 if (imsg_compose(ibuf,
1799 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1800 return got_error_from_errno("imsg_compose "
1801 "GITCONFIG_AUTHOR_NAME_REQUEST");
1803 return flush_imsg(ibuf);
1806 const struct got_error *
1807 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1809 if (imsg_compose(ibuf,
1810 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1811 return got_error_from_errno("imsg_compose "
1812 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1814 return flush_imsg(ibuf);
1817 const struct got_error *
1818 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1820 if (imsg_compose(ibuf,
1821 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1822 return got_error_from_errno("imsg_compose "
1823 "GITCONFIG_REMOTE_REQUEST");
1825 return flush_imsg(ibuf);
1828 const struct got_error *
1829 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1831 if (imsg_compose(ibuf,
1832 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1833 return got_error_from_errno("imsg_compose "
1834 "GITCONFIG_OWNER_REQUEST");
1836 return flush_imsg(ibuf);
1839 const struct got_error *
1840 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1842 size_t len = value ? strlen(value) + 1 : 0;
1844 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1845 value, len) == -1)
1846 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1848 return flush_imsg(ibuf);
1851 const struct got_error *
1852 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1854 const struct got_error *err = NULL;
1855 struct imsg imsg;
1856 size_t datalen;
1857 const size_t min_datalen = 0;
1859 *str = NULL;
1861 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1862 if (err)
1863 return err;
1864 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1866 switch (imsg.hdr.type) {
1867 case GOT_IMSG_GITCONFIG_STR_VAL:
1868 if (datalen == 0)
1869 break;
1870 *str = malloc(datalen);
1871 if (*str == NULL) {
1872 err = got_error_from_errno("malloc");
1873 break;
1875 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1876 err = got_error(GOT_ERR_NO_SPACE);
1877 break;
1878 default:
1879 err = got_error(GOT_ERR_PRIVSEP_MSG);
1880 break;
1883 imsg_free(&imsg);
1884 return err;
1887 const struct got_error *
1888 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1890 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1891 &value, sizeof(value)) == -1)
1892 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1894 return flush_imsg(ibuf);
1897 const struct got_error *
1898 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1900 const struct got_error *err = NULL;
1901 struct imsg imsg;
1902 size_t datalen;
1903 const size_t min_datalen =
1904 MIN(sizeof(struct got_imsg_error), sizeof(int));
1906 *val = 0;
1908 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1909 if (err)
1910 return err;
1911 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1913 switch (imsg.hdr.type) {
1914 case GOT_IMSG_GITCONFIG_INT_VAL:
1915 if (datalen != sizeof(*val)) {
1916 err = got_error(GOT_ERR_PRIVSEP_LEN);
1917 break;
1919 memcpy(val, imsg.data, sizeof(*val));
1920 break;
1921 default:
1922 err = got_error(GOT_ERR_PRIVSEP_MSG);
1923 break;
1926 imsg_free(&imsg);
1927 return err;
1930 const struct got_error *
1931 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1932 struct got_remote_repo *remotes, int nremotes)
1934 const struct got_error *err = NULL;
1935 struct got_imsg_remotes iremotes;
1936 int i;
1938 iremotes.nremotes = nremotes;
1939 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1940 &iremotes, sizeof(iremotes)) == -1)
1941 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1943 err = flush_imsg(ibuf);
1944 imsg_clear(ibuf);
1945 if (err)
1946 return err;
1948 for (i = 0; i < nremotes; i++) {
1949 struct got_imsg_remote iremote;
1950 size_t len = sizeof(iremote);
1951 struct ibuf *wbuf;
1953 iremote.mirror_references = remotes[i].mirror_references;
1954 iremote.name_len = strlen(remotes[i].name);
1955 len += iremote.name_len;
1956 iremote.url_len = strlen(remotes[i].url);
1957 len += iremote.url_len;
1959 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1960 if (wbuf == NULL)
1961 return got_error_from_errno(
1962 "imsg_create GITCONFIG_REMOTE");
1964 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1965 err = got_error_from_errno(
1966 "imsg_add GITCONFIG_REMOTE");
1967 ibuf_free(wbuf);
1968 return err;
1971 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1972 err = got_error_from_errno(
1973 "imsg_add GITCONFIG_REMOTE");
1974 ibuf_free(wbuf);
1975 return err;
1977 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1978 err = got_error_from_errno(
1979 "imsg_add GITCONFIG_REMOTE");
1980 ibuf_free(wbuf);
1981 return err;
1984 wbuf->fd = -1;
1985 imsg_close(ibuf, wbuf);
1986 err = flush_imsg(ibuf);
1987 if (err)
1988 return err;
1991 return NULL;
1994 const struct got_error *
1995 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1996 int *nremotes, struct imsgbuf *ibuf)
1998 const struct got_error *err = NULL;
1999 struct imsg imsg;
2000 size_t datalen;
2001 struct got_imsg_remotes iremotes;
2002 struct got_imsg_remote iremote;
2004 *remotes = NULL;
2005 *nremotes = 0;
2006 iremotes.nremotes = 0;
2008 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2009 if (err)
2010 return err;
2011 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2013 switch (imsg.hdr.type) {
2014 case GOT_IMSG_GITCONFIG_REMOTES:
2015 if (datalen != sizeof(iremotes)) {
2016 err = got_error(GOT_ERR_PRIVSEP_LEN);
2017 break;
2019 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2020 if (iremotes.nremotes == 0) {
2021 imsg_free(&imsg);
2022 return NULL;
2024 break;
2025 default:
2026 imsg_free(&imsg);
2027 return got_error(GOT_ERR_PRIVSEP_MSG);
2030 imsg_free(&imsg);
2032 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2033 if (*remotes == NULL)
2034 return got_error_from_errno("recallocarray");
2036 while (*nremotes < iremotes.nremotes) {
2037 struct got_remote_repo *remote;
2039 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2040 if (err)
2041 break;
2042 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2044 switch (imsg.hdr.type) {
2045 case GOT_IMSG_GITCONFIG_REMOTE:
2046 remote = &(*remotes)[*nremotes];
2047 if (datalen < sizeof(iremote)) {
2048 err = got_error(GOT_ERR_PRIVSEP_LEN);
2049 break;
2051 memcpy(&iremote, imsg.data, sizeof(iremote));
2052 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2053 (sizeof(iremote) + iremote.name_len +
2054 iremote.url_len) > datalen) {
2055 err = got_error(GOT_ERR_PRIVSEP_LEN);
2056 break;
2058 remote->name = strndup(imsg.data + sizeof(iremote),
2059 iremote.name_len);
2060 if (remote->name == NULL) {
2061 err = got_error_from_errno("strndup");
2062 break;
2064 remote->url = strndup(imsg.data + sizeof(iremote) +
2065 iremote.name_len, iremote.url_len);
2066 if (remote->url == NULL) {
2067 err = got_error_from_errno("strndup");
2068 free(remote->name);
2069 break;
2071 remote->mirror_references = iremote.mirror_references;
2072 (*nremotes)++;
2073 break;
2074 default:
2075 err = got_error(GOT_ERR_PRIVSEP_MSG);
2076 break;
2079 imsg_free(&imsg);
2080 if (err)
2081 break;
2084 if (err) {
2085 int i;
2086 for (i = 0; i < *nremotes; i++) {
2087 free((*remotes)[i].name);
2088 free((*remotes)[i].url);
2090 free(*remotes);
2091 *remotes = NULL;
2092 *nremotes = 0;
2094 return err;
2097 const struct got_error *
2098 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2099 struct got_object_id *id, int idx, const char *path)
2101 const struct got_error *err = NULL;
2102 struct ibuf *wbuf;
2103 size_t path_len = strlen(path) + 1;
2105 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2106 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2107 if (wbuf == NULL)
2108 return got_error_from_errno(
2109 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2110 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2111 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2112 ibuf_free(wbuf);
2113 return err;
2115 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2116 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2117 ibuf_free(wbuf);
2118 return err;
2120 if (imsg_add(wbuf, path, path_len) == -1) {
2121 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2122 ibuf_free(wbuf);
2123 return err;
2126 wbuf->fd = -1;
2127 imsg_close(ibuf, wbuf);
2129 return flush_imsg(ibuf);
2132 const struct got_error *
2133 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2134 size_t ncommits, struct imsgbuf *ibuf)
2136 const struct got_error *err;
2137 struct ibuf *wbuf;
2138 int i;
2140 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2141 sizeof(struct got_imsg_traversed_commits) +
2142 ncommits * SHA1_DIGEST_LENGTH);
2143 if (wbuf == NULL)
2144 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2146 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2147 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2148 ibuf_free(wbuf);
2149 return err;
2151 for (i = 0; i < ncommits; i++) {
2152 struct got_object_id *id = &commit_ids[i];
2153 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2154 err = got_error_from_errno(
2155 "imsg_add TRAVERSED_COMMITS");
2156 ibuf_free(wbuf);
2157 return err;
2161 wbuf->fd = -1;
2162 imsg_close(ibuf, wbuf);
2164 return flush_imsg(ibuf);
2167 const struct got_error *
2168 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2169 struct got_object_id **changed_commit_id,
2170 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2172 const struct got_error *err = NULL;
2173 struct imsg imsg;
2174 struct got_imsg_traversed_commits *icommits;
2175 size_t datalen;
2176 int i, done = 0;
2178 *changed_commit = NULL;
2179 *changed_commit_id = NULL;
2181 while (!done) {
2182 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2183 if (err)
2184 return err;
2186 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2187 switch (imsg.hdr.type) {
2188 case GOT_IMSG_TRAVERSED_COMMITS:
2189 icommits = imsg.data;
2190 if (datalen != sizeof(*icommits) +
2191 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2192 err = got_error(GOT_ERR_PRIVSEP_LEN);
2193 break;
2195 for (i = 0; i < icommits->ncommits; i++) {
2196 struct got_object_qid *qid;
2197 uint8_t *sha1 = (uint8_t *)imsg.data +
2198 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2199 err = got_object_qid_alloc_partial(&qid);
2200 if (err)
2201 break;
2202 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2203 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2205 /* The last commit may contain a change. */
2206 if (i == icommits->ncommits - 1) {
2207 *changed_commit_id =
2208 got_object_id_dup(qid->id);
2209 if (*changed_commit_id == NULL) {
2210 err = got_error_from_errno(
2211 "got_object_id_dup");
2212 break;
2216 break;
2217 case GOT_IMSG_COMMIT:
2218 if (*changed_commit_id == NULL) {
2219 err = got_error(GOT_ERR_PRIVSEP_MSG);
2220 break;
2222 err = get_commit_from_imsg(changed_commit, &imsg,
2223 datalen, ibuf);
2224 break;
2225 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2226 done = 1;
2227 break;
2228 default:
2229 err = got_error(GOT_ERR_PRIVSEP_MSG);
2230 break;
2233 imsg_free(&imsg);
2234 if (err)
2235 break;
2238 if (err)
2239 got_object_id_queue_free(commit_ids);
2240 return err;
2243 const struct got_error *
2244 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2246 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2247 NULL, 0) == -1)
2248 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2250 return flush_imsg(ibuf);
2253 const struct got_error *
2254 got_privsep_unveil_exec_helpers(void)
2256 const char *helpers[] = {
2257 GOT_PATH_PROG_READ_PACK,
2258 GOT_PATH_PROG_READ_OBJECT,
2259 GOT_PATH_PROG_READ_COMMIT,
2260 GOT_PATH_PROG_READ_TREE,
2261 GOT_PATH_PROG_READ_BLOB,
2262 GOT_PATH_PROG_READ_TAG,
2263 GOT_PATH_PROG_READ_GITCONFIG,
2264 GOT_PATH_PROG_FETCH_PACK,
2265 GOT_PATH_PROG_INDEX_PACK,
2267 int i;
2269 for (i = 0; i < nitems(helpers); i++) {
2270 if (unveil(helpers[i], "x") == 0)
2271 continue;
2272 return got_error_from_errno2("unveil", helpers[i]);
2275 return NULL;
2278 void
2279 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2281 if (close(imsg_fds[0]) != 0) {
2282 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2283 _exit(1);
2286 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2287 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2288 _exit(1);
2290 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2291 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2292 _exit(1);
2295 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2296 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2297 strerror(errno));
2298 _exit(1);