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)
418 const struct got_error *err = NULL;
419 struct ibuf *wbuf;
420 size_t len;
421 struct got_pathlist_entry *pe;
422 struct got_imsg_fetch_request fetchreq;
424 memset(&fetchreq, 0, sizeof(fetchreq));
425 fetchreq.fetch_all_branches = fetch_all_branches;
426 fetchreq.list_refs_only = list_refs_only;
427 TAILQ_FOREACH(pe, have_refs, entry)
428 fetchreq.n_have_refs++;
429 TAILQ_FOREACH(pe, wanted_branches, entry)
430 fetchreq.n_wanted_branches++;
431 len = sizeof(struct got_imsg_fetch_request);
432 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
433 close(fd);
434 return got_error(GOT_ERR_NO_SPACE);
437 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
438 &fetchreq, sizeof(fetchreq)) == -1)
439 return got_error_from_errno(
440 "imsg_compose FETCH_SERVER_PROGRESS");
442 err = flush_imsg(ibuf);
443 if (err) {
444 close(fd);
445 return err;
447 fd = -1;
449 TAILQ_FOREACH(pe, have_refs, entry) {
450 const char *name = pe->path;
451 size_t name_len = pe->path_len;
452 struct got_object_id *id = pe->data;
454 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
455 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
456 if (wbuf == NULL)
457 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
459 /* Keep in sync with struct got_imsg_fetch_have_ref! */
460 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
461 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
462 ibuf_free(wbuf);
463 return err;
465 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
466 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
467 ibuf_free(wbuf);
468 return err;
470 if (imsg_add(wbuf, name, name_len) == -1) {
471 err = got_error_from_errno("imsg_add FETCH_HAVE_REF");
472 ibuf_free(wbuf);
473 return err;
476 wbuf->fd = -1;
477 imsg_close(ibuf, wbuf);
478 err = flush_imsg(ibuf);
479 if (err)
480 return err;
483 TAILQ_FOREACH(pe, wanted_branches, entry) {
484 const char *name = pe->path;
485 size_t name_len = pe->path_len;
487 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
488 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
489 len);
490 if (wbuf == NULL)
491 return got_error_from_errno(
492 "imsg_create FETCH_WANTED_BRANCH");
494 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
495 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
496 err = got_error_from_errno(
497 "imsg_add FETCH_WANTED_BRANCH");
498 ibuf_free(wbuf);
499 return err;
501 if (imsg_add(wbuf, name, name_len) == -1) {
502 err = got_error_from_errno(
503 "imsg_add FETCH_WANTED_BRANCH");
504 ibuf_free(wbuf);
505 return err;
508 wbuf->fd = -1;
509 imsg_close(ibuf, wbuf);
510 err = flush_imsg(ibuf);
511 if (err)
512 return err;
515 return NULL;
519 const struct got_error *
520 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
522 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
526 const struct got_error *
527 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
528 struct got_pathlist_head *symrefs)
530 const struct got_error *err = NULL;
531 struct ibuf *wbuf;
532 size_t len, nsymrefs = 0;
533 struct got_pathlist_entry *pe;
535 len = sizeof(struct got_imsg_fetch_symrefs);
536 TAILQ_FOREACH(pe, symrefs, entry) {
537 const char *target = pe->data;
538 len += sizeof(struct got_imsg_fetch_symref) +
539 pe->path_len + strlen(target);
540 nsymrefs++;
543 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
544 return got_error(GOT_ERR_NO_SPACE);
546 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
547 if (wbuf == NULL)
548 return got_error_from_errno("imsg_create FETCH_SYMREFS");
550 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
551 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
552 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
553 ibuf_free(wbuf);
554 return err;
557 TAILQ_FOREACH(pe, symrefs, entry) {
558 const char *name = pe->path;
559 size_t name_len = pe->path_len;
560 const char *target = pe->data;
561 size_t target_len = strlen(target);
563 /* Keep in sync with struct got_imsg_fetch_symref definition! */
564 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
565 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
566 ibuf_free(wbuf);
567 return err;
569 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
570 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
571 ibuf_free(wbuf);
572 return err;
574 if (imsg_add(wbuf, name, name_len) == -1) {
575 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
576 ibuf_free(wbuf);
577 return err;
579 if (imsg_add(wbuf, target, target_len) == -1) {
580 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
581 ibuf_free(wbuf);
582 return err;
586 wbuf->fd = -1;
587 imsg_close(ibuf, wbuf);
588 return flush_imsg(ibuf);
591 const struct got_error *
592 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
593 struct got_object_id *refid, const char *refname)
595 const struct got_error *err = NULL;
596 struct ibuf *wbuf;
597 size_t len, reflen = strlen(refname);
599 len = sizeof(struct got_imsg_fetch_ref) + reflen;
600 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
601 return got_error(GOT_ERR_NO_SPACE);
603 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
604 if (wbuf == NULL)
605 return got_error_from_errno("imsg_create FETCH_REF");
607 /* Keep in sync with struct got_imsg_fetch_ref definition! */
608 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
609 err = got_error_from_errno("imsg_add FETCH_REF");
610 ibuf_free(wbuf);
611 return err;
613 if (imsg_add(wbuf, refname, reflen) == -1) {
614 err = got_error_from_errno("imsg_add FETCH_REF");
615 ibuf_free(wbuf);
616 return err;
619 wbuf->fd = -1;
620 imsg_close(ibuf, wbuf);
621 return flush_imsg(ibuf);
624 const struct got_error *
625 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
626 size_t msglen)
628 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
629 return got_error(GOT_ERR_NO_SPACE);
631 if (msglen == 0)
632 return NULL;
634 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
635 msg, msglen) == -1)
636 return got_error_from_errno(
637 "imsg_compose FETCH_SERVER_PROGRESS");
639 return flush_imsg(ibuf);
642 const struct got_error *
643 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
645 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
646 &bytes, sizeof(bytes)) == -1)
647 return got_error_from_errno(
648 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
650 return flush_imsg(ibuf);
653 const struct got_error *
654 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
656 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
657 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
658 return got_error_from_errno("imsg_compose FETCH");
659 return flush_imsg(ibuf);
663 const struct got_error *
664 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
665 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
666 off_t *packfile_size, struct imsgbuf *ibuf)
668 const struct got_error *err = NULL;
669 struct imsg imsg;
670 size_t datalen;
671 struct got_imsg_fetch_symrefs *isymrefs = NULL;
672 size_t n, remain;
673 off_t off;
674 int i;
676 *done = 0;
677 *id = NULL;
678 *refname = NULL;
679 *server_progress = NULL;
680 *packfile_size = 0;
682 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
683 if (err)
684 return err;
686 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
687 switch (imsg.hdr.type) {
688 case GOT_IMSG_ERROR:
689 if (datalen < sizeof(struct got_imsg_error)) {
690 err = got_error(GOT_ERR_PRIVSEP_LEN);
691 break;
693 err = recv_imsg_error(&imsg, datalen);
694 break;
695 case GOT_IMSG_FETCH_SYMREFS:
696 if (datalen < sizeof(*isymrefs)) {
697 err = got_error(GOT_ERR_PRIVSEP_LEN);
698 break;
700 if (isymrefs != NULL) {
701 err = got_error(GOT_ERR_PRIVSEP_MSG);
702 break;
704 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
705 off = sizeof(*isymrefs);
706 remain = datalen - off;
707 for (n = 0; n < isymrefs->nsymrefs; n++) {
708 struct got_imsg_fetch_symref *s;
709 char *name, *target;
710 if (remain < sizeof(struct got_imsg_fetch_symref)) {
711 err = got_error(GOT_ERR_PRIVSEP_LEN);
712 goto done;
714 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
715 off += sizeof(*s);
716 remain -= sizeof(*s);
717 if (remain < s->name_len) {
718 err = got_error(GOT_ERR_PRIVSEP_LEN);
719 goto done;
721 name = strndup(imsg.data + off, s->name_len);
722 if (name == NULL) {
723 err = got_error_from_errno("strndup");
724 goto done;
726 off += s->name_len;
727 remain -= s->name_len;
728 if (remain < s->target_len) {
729 err = got_error(GOT_ERR_PRIVSEP_LEN);
730 free(name);
731 goto done;
733 target = strndup(imsg.data + off, s->target_len);
734 if (target == NULL) {
735 err = got_error_from_errno("strndup");
736 free(name);
737 goto done;
739 off += s->target_len;
740 remain -= s->target_len;
741 err = got_pathlist_append(symrefs, name, target);
742 if (err) {
743 free(name);
744 free(target);
745 goto done;
748 break;
749 case GOT_IMSG_FETCH_REF:
750 if (datalen <= SHA1_DIGEST_LENGTH) {
751 err = got_error(GOT_ERR_PRIVSEP_MSG);
752 break;
754 *id = malloc(sizeof(**id));
755 if (*id == NULL) {
756 err = got_error_from_errno("malloc");
757 break;
759 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
760 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
761 datalen - SHA1_DIGEST_LENGTH);
762 if (*refname == NULL) {
763 err = got_error_from_errno("strndup");
764 break;
766 break;
767 case GOT_IMSG_FETCH_SERVER_PROGRESS:
768 if (datalen == 0) {
769 err = got_error(GOT_ERR_PRIVSEP_LEN);
770 break;
772 *server_progress = strndup(imsg.data, datalen);
773 if (*server_progress == NULL) {
774 err = got_error_from_errno("strndup");
775 break;
777 for (i = 0; i < datalen; i++) {
778 if (!isprint((unsigned char)(*server_progress)[i]) &&
779 !isspace((unsigned char)(*server_progress)[i])) {
780 err = got_error(GOT_ERR_PRIVSEP_MSG);
781 free(*server_progress);
782 *server_progress = NULL;
783 goto done;
786 break;
787 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
788 if (datalen < sizeof(*packfile_size)) {
789 err = got_error(GOT_ERR_PRIVSEP_MSG);
790 break;
792 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
793 break;
794 case GOT_IMSG_FETCH_DONE:
795 *id = malloc(sizeof(**id));
796 if (*id == NULL) {
797 err = got_error_from_errno("malloc");
798 break;
800 if (datalen != SHA1_DIGEST_LENGTH) {
801 err = got_error(GOT_ERR_PRIVSEP_MSG);
802 break;
804 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
805 *done = 1;
806 break;
807 default:
808 err = got_error(GOT_ERR_PRIVSEP_MSG);
809 break;
811 done:
812 if (err) {
813 free(*id);
814 *id = NULL;
815 free(*refname);
816 *refname = NULL;
818 imsg_free(&imsg);
819 return err;
822 const struct got_error *
823 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
824 int fd)
826 const struct got_error *err = NULL;
828 /* Keep in sync with struct got_imsg_index_pack_request */
829 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
830 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
831 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
832 close(fd);
833 return err;
835 return flush_imsg(ibuf);
838 const struct got_error *
839 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
841 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
844 const struct got_error *
845 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
846 int nobj_indexed, int nobj_loose, int nobj_resolved)
848 struct got_imsg_index_pack_progress iprogress;
850 iprogress.nobj_total = nobj_total;
851 iprogress.nobj_indexed = nobj_indexed;
852 iprogress.nobj_loose = nobj_loose;
853 iprogress.nobj_resolved = nobj_resolved;
855 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
856 &iprogress, sizeof(iprogress)) == -1)
857 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
859 return flush_imsg(ibuf);
862 const struct got_error *
863 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
865 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
866 return got_error_from_errno("imsg_compose FETCH");
867 return flush_imsg(ibuf);
870 const struct got_error *
871 got_privsep_recv_index_progress(int *done, int *nobj_total,
872 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
873 struct imsgbuf *ibuf)
875 const struct got_error *err = NULL;
876 struct imsg imsg;
877 struct got_imsg_index_pack_progress *iprogress;
878 size_t datalen;
880 *done = 0;
881 *nobj_total = 0;
882 *nobj_indexed = 0;
883 *nobj_resolved = 0;
885 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
886 if (err)
887 return err;
889 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
890 switch (imsg.hdr.type) {
891 case GOT_IMSG_ERROR:
892 if (datalen < sizeof(struct got_imsg_error)) {
893 err = got_error(GOT_ERR_PRIVSEP_LEN);
894 break;
896 err = recv_imsg_error(&imsg, datalen);
897 break;
898 case GOT_IMSG_IDXPACK_PROGRESS:
899 if (datalen < sizeof(*iprogress)) {
900 err = got_error(GOT_ERR_PRIVSEP_LEN);
901 break;
903 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
904 *nobj_total = iprogress->nobj_total;
905 *nobj_indexed = iprogress->nobj_indexed;
906 *nobj_loose = iprogress->nobj_loose;
907 *nobj_resolved = iprogress->nobj_resolved;
908 break;
909 case GOT_IMSG_IDXPACK_DONE:
910 if (datalen != 0) {
911 err = got_error(GOT_ERR_PRIVSEP_LEN);
912 break;
914 *done = 1;
915 break;
916 default:
917 err = got_error(GOT_ERR_PRIVSEP_MSG);
918 break;
921 imsg_free(&imsg);
922 return err;
925 const struct got_error *
926 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
927 struct imsgbuf *ibuf)
929 const struct got_error *err = NULL;
930 struct got_imsg_object *iobj;
931 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
933 if (datalen != sizeof(*iobj))
934 return got_error(GOT_ERR_PRIVSEP_LEN);
935 iobj = imsg->data;
937 *obj = calloc(1, sizeof(**obj));
938 if (*obj == NULL)
939 return got_error_from_errno("calloc");
941 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
942 (*obj)->type = iobj->type;
943 (*obj)->flags = iobj->flags;
944 (*obj)->hdrlen = iobj->hdrlen;
945 (*obj)->size = iobj->size;
946 /* path_packfile is handled by caller */
947 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
948 (*obj)->pack_offset = iobj->pack_offset;
949 (*obj)->pack_idx = iobj->pack_idx;
952 return err;
955 const struct got_error *
956 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
958 const struct got_error *err = NULL;
959 struct imsg imsg;
960 const size_t min_datalen =
961 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
963 *obj = NULL;
965 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
966 if (err)
967 return err;
969 switch (imsg.hdr.type) {
970 case GOT_IMSG_OBJECT:
971 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
972 break;
973 default:
974 err = got_error(GOT_ERR_PRIVSEP_MSG);
975 break;
978 imsg_free(&imsg);
980 return err;
983 static const struct got_error *
984 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
985 size_t logmsg_len)
987 const struct got_error *err = NULL;
988 size_t offset, remain;
990 offset = 0;
991 remain = logmsg_len;
992 while (remain > 0) {
993 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
995 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
996 commit->logmsg + offset, n) == -1) {
997 err = got_error_from_errno("imsg_compose "
998 "COMMIT_LOGMSG");
999 break;
1002 err = flush_imsg(ibuf);
1003 if (err)
1004 break;
1006 offset += n;
1007 remain -= n;
1010 return err;
1013 const struct got_error *
1014 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1016 const struct got_error *err = NULL;
1017 struct got_imsg_commit_object *icommit;
1018 uint8_t *buf;
1019 size_t len, total;
1020 struct got_object_qid *qid;
1021 size_t author_len = strlen(commit->author);
1022 size_t committer_len = strlen(commit->committer);
1023 size_t logmsg_len = strlen(commit->logmsg);
1025 total = sizeof(*icommit) + author_len + committer_len +
1026 commit->nparents * SHA1_DIGEST_LENGTH;
1028 buf = malloc(total);
1029 if (buf == NULL)
1030 return got_error_from_errno("malloc");
1032 icommit = (struct got_imsg_commit_object *)buf;
1033 memcpy(icommit->tree_id, commit->tree_id->sha1,
1034 sizeof(icommit->tree_id));
1035 icommit->author_len = author_len;
1036 icommit->author_time = commit->author_time;
1037 icommit->author_gmtoff = commit->author_gmtoff;
1038 icommit->committer_len = committer_len;
1039 icommit->committer_time = commit->committer_time;
1040 icommit->committer_gmtoff = commit->committer_gmtoff;
1041 icommit->logmsg_len = logmsg_len;
1042 icommit->nparents = commit->nparents;
1044 len = sizeof(*icommit);
1045 memcpy(buf + len, commit->author, author_len);
1046 len += author_len;
1047 memcpy(buf + len, commit->committer, committer_len);
1048 len += committer_len;
1049 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1050 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1051 len += SHA1_DIGEST_LENGTH;
1054 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1055 err = got_error_from_errno("imsg_compose COMMIT");
1056 goto done;
1059 if (logmsg_len == 0 ||
1060 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1061 err = flush_imsg(ibuf);
1062 if (err)
1063 goto done;
1065 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1066 done:
1067 free(buf);
1068 return err;
1071 static const struct got_error *
1072 get_commit_from_imsg(struct got_commit_object **commit,
1073 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1075 const struct got_error *err = NULL;
1076 struct got_imsg_commit_object *icommit;
1077 size_t len = 0;
1078 int i;
1080 if (datalen < sizeof(*icommit))
1081 return got_error(GOT_ERR_PRIVSEP_LEN);
1083 icommit = imsg->data;
1084 if (datalen != sizeof(*icommit) + icommit->author_len +
1085 icommit->committer_len +
1086 icommit->nparents * SHA1_DIGEST_LENGTH)
1087 return got_error(GOT_ERR_PRIVSEP_LEN);
1089 if (icommit->nparents < 0)
1090 return got_error(GOT_ERR_PRIVSEP_LEN);
1092 len += sizeof(*icommit);
1094 *commit = got_object_commit_alloc_partial();
1095 if (*commit == NULL)
1096 return got_error_from_errno(
1097 "got_object_commit_alloc_partial");
1099 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1100 SHA1_DIGEST_LENGTH);
1101 (*commit)->author_time = icommit->author_time;
1102 (*commit)->author_gmtoff = icommit->author_gmtoff;
1103 (*commit)->committer_time = icommit->committer_time;
1104 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1106 if (icommit->author_len == 0) {
1107 (*commit)->author = strdup("");
1108 if ((*commit)->author == NULL) {
1109 err = got_error_from_errno("strdup");
1110 goto done;
1112 } else {
1113 (*commit)->author = malloc(icommit->author_len + 1);
1114 if ((*commit)->author == NULL) {
1115 err = got_error_from_errno("malloc");
1116 goto done;
1118 memcpy((*commit)->author, imsg->data + len,
1119 icommit->author_len);
1120 (*commit)->author[icommit->author_len] = '\0';
1122 len += icommit->author_len;
1124 if (icommit->committer_len == 0) {
1125 (*commit)->committer = strdup("");
1126 if ((*commit)->committer == NULL) {
1127 err = got_error_from_errno("strdup");
1128 goto done;
1130 } else {
1131 (*commit)->committer =
1132 malloc(icommit->committer_len + 1);
1133 if ((*commit)->committer == NULL) {
1134 err = got_error_from_errno("malloc");
1135 goto done;
1137 memcpy((*commit)->committer, imsg->data + len,
1138 icommit->committer_len);
1139 (*commit)->committer[icommit->committer_len] = '\0';
1141 len += icommit->committer_len;
1143 if (icommit->logmsg_len == 0) {
1144 (*commit)->logmsg = strdup("");
1145 if ((*commit)->logmsg == NULL) {
1146 err = got_error_from_errno("strdup");
1147 goto done;
1149 } else {
1150 size_t offset = 0, remain = icommit->logmsg_len;
1152 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1153 if ((*commit)->logmsg == NULL) {
1154 err = got_error_from_errno("malloc");
1155 goto done;
1157 while (remain > 0) {
1158 struct imsg imsg_log;
1159 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1160 remain);
1162 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1163 if (err)
1164 goto done;
1166 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1167 err = got_error(GOT_ERR_PRIVSEP_MSG);
1168 goto done;
1171 memcpy((*commit)->logmsg + offset,
1172 imsg_log.data, n);
1173 imsg_free(&imsg_log);
1174 offset += n;
1175 remain -= n;
1177 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1180 for (i = 0; i < icommit->nparents; i++) {
1181 struct got_object_qid *qid;
1183 err = got_object_qid_alloc_partial(&qid);
1184 if (err)
1185 break;
1186 memcpy(qid->id, imsg->data + len +
1187 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1188 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1189 (*commit)->nparents++;
1191 done:
1192 if (err) {
1193 got_object_commit_close(*commit);
1194 *commit = NULL;
1196 return err;
1199 const struct got_error *
1200 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1202 const struct got_error *err = NULL;
1203 struct imsg imsg;
1204 size_t datalen;
1205 const size_t min_datalen =
1206 MIN(sizeof(struct got_imsg_error),
1207 sizeof(struct got_imsg_commit_object));
1209 *commit = NULL;
1211 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1212 if (err)
1213 return err;
1215 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1217 switch (imsg.hdr.type) {
1218 case GOT_IMSG_COMMIT:
1219 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1220 break;
1221 default:
1222 err = got_error(GOT_ERR_PRIVSEP_MSG);
1223 break;
1226 imsg_free(&imsg);
1228 return err;
1231 const struct got_error *
1232 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1233 int nentries)
1235 const struct got_error *err = NULL;
1236 struct got_imsg_tree_object itree;
1237 struct got_pathlist_entry *pe;
1238 size_t totlen;
1239 int nimsg; /* number of imsg queued in ibuf */
1241 itree.nentries = nentries;
1242 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1243 == -1)
1244 return got_error_from_errno("imsg_compose TREE");
1246 totlen = sizeof(itree);
1247 nimsg = 1;
1248 TAILQ_FOREACH(pe, entries, entry) {
1249 const char *name = pe->path;
1250 struct got_parsed_tree_entry *pte = pe->data;
1251 struct ibuf *wbuf;
1252 size_t namelen = strlen(name);
1253 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1255 if (len > MAX_IMSGSIZE)
1256 return got_error(GOT_ERR_NO_SPACE);
1258 nimsg++;
1259 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1260 err = flush_imsg(ibuf);
1261 if (err)
1262 return err;
1263 nimsg = 0;
1266 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1267 if (wbuf == NULL)
1268 return got_error_from_errno("imsg_create TREE_ENTRY");
1270 /* Keep in sync with struct got_imsg_tree_object definition! */
1271 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1272 err = got_error_from_errno("imsg_add TREE_ENTRY");
1273 ibuf_free(wbuf);
1274 return err;
1276 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1277 err = got_error_from_errno("imsg_add TREE_ENTRY");
1278 ibuf_free(wbuf);
1279 return err;
1282 if (imsg_add(wbuf, name, namelen) == -1) {
1283 err = got_error_from_errno("imsg_add TREE_ENTRY");
1284 ibuf_free(wbuf);
1285 return err;
1288 wbuf->fd = -1;
1289 imsg_close(ibuf, wbuf);
1291 totlen += len;
1294 return flush_imsg(ibuf);
1297 const struct got_error *
1298 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1300 const struct got_error *err = NULL;
1301 const size_t min_datalen =
1302 MIN(sizeof(struct got_imsg_error),
1303 sizeof(struct got_imsg_tree_object));
1304 struct got_imsg_tree_object *itree;
1305 int nentries = 0;
1307 *tree = NULL;
1308 get_more:
1309 err = read_imsg(ibuf);
1310 if (err)
1311 goto done;
1313 for (;;) {
1314 struct imsg imsg;
1315 size_t n;
1316 size_t datalen;
1317 struct got_imsg_tree_entry *ite;
1318 struct got_tree_entry *te = NULL;
1320 n = imsg_get(ibuf, &imsg);
1321 if (n == 0) {
1322 if (*tree && (*tree)->nentries != nentries)
1323 goto get_more;
1324 break;
1327 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1328 return got_error(GOT_ERR_PRIVSEP_LEN);
1330 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1332 switch (imsg.hdr.type) {
1333 case GOT_IMSG_ERROR:
1334 err = recv_imsg_error(&imsg, datalen);
1335 break;
1336 case GOT_IMSG_TREE:
1337 /* This message should only appear once. */
1338 if (*tree != NULL) {
1339 err = got_error(GOT_ERR_PRIVSEP_MSG);
1340 break;
1342 if (datalen != sizeof(*itree)) {
1343 err = got_error(GOT_ERR_PRIVSEP_LEN);
1344 break;
1346 itree = imsg.data;
1347 *tree = malloc(sizeof(**tree));
1348 if (*tree == NULL) {
1349 err = got_error_from_errno("malloc");
1350 break;
1352 (*tree)->entries = calloc(itree->nentries,
1353 sizeof(struct got_tree_entry));
1354 if ((*tree)->entries == NULL) {
1355 err = got_error_from_errno("malloc");
1356 break;
1358 (*tree)->nentries = itree->nentries;
1359 (*tree)->refcnt = 0;
1360 break;
1361 case GOT_IMSG_TREE_ENTRY:
1362 /* This message should be preceeded by GOT_IMSG_TREE. */
1363 if (*tree == NULL) {
1364 err = got_error(GOT_ERR_PRIVSEP_MSG);
1365 break;
1367 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1368 err = got_error(GOT_ERR_PRIVSEP_LEN);
1369 break;
1372 /* Remaining data contains the entry's name. */
1373 datalen -= sizeof(*ite);
1374 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1375 err = got_error(GOT_ERR_PRIVSEP_LEN);
1376 break;
1378 ite = imsg.data;
1380 if (datalen + 1 > sizeof(te->name)) {
1381 err = got_error(GOT_ERR_NO_SPACE);
1382 break;
1384 te = &(*tree)->entries[nentries];
1385 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1386 te->name[datalen] = '\0';
1388 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1389 te->mode = ite->mode;
1390 te->idx = nentries;
1391 nentries++;
1392 break;
1393 default:
1394 err = got_error(GOT_ERR_PRIVSEP_MSG);
1395 break;
1398 imsg_free(&imsg);
1400 done:
1401 if (*tree && (*tree)->nentries != nentries) {
1402 if (err == NULL)
1403 err = got_error(GOT_ERR_PRIVSEP_LEN);
1404 got_object_tree_close(*tree);
1405 *tree = NULL;
1408 return err;
1411 const struct got_error *
1412 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1413 const uint8_t *data)
1415 struct got_imsg_blob iblob;
1417 iblob.size = size;
1418 iblob.hdrlen = hdrlen;
1420 if (data) {
1421 uint8_t *buf;
1423 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1424 return got_error(GOT_ERR_NO_SPACE);
1426 buf = malloc(sizeof(iblob) + size);
1427 if (buf == NULL)
1428 return got_error_from_errno("malloc");
1430 memcpy(buf, &iblob, sizeof(iblob));
1431 memcpy(buf + sizeof(iblob), data, size);
1432 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1433 sizeof(iblob) + size) == -1) {
1434 free(buf);
1435 return got_error_from_errno("imsg_compose BLOB");
1437 free(buf);
1438 } else {
1439 /* Data has already been written to file descriptor. */
1440 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1441 sizeof(iblob)) == -1)
1442 return got_error_from_errno("imsg_compose BLOB");
1446 return flush_imsg(ibuf);
1449 const struct got_error *
1450 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1451 struct imsgbuf *ibuf)
1453 const struct got_error *err = NULL;
1454 struct imsg imsg;
1455 struct got_imsg_blob *iblob;
1456 size_t datalen;
1458 *outbuf = NULL;
1460 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1461 if (err)
1462 return err;
1464 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1466 switch (imsg.hdr.type) {
1467 case GOT_IMSG_BLOB:
1468 if (datalen < sizeof(*iblob)) {
1469 err = got_error(GOT_ERR_PRIVSEP_LEN);
1470 break;
1472 iblob = imsg.data;
1473 *size = iblob->size;
1474 *hdrlen = iblob->hdrlen;
1476 if (datalen == sizeof(*iblob)) {
1477 /* Data has been written to file descriptor. */
1478 break;
1481 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1482 err = got_error(GOT_ERR_PRIVSEP_LEN);
1483 break;
1486 *outbuf = malloc(*size);
1487 if (*outbuf == NULL) {
1488 err = got_error_from_errno("malloc");
1489 break;
1491 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1492 break;
1493 default:
1494 err = got_error(GOT_ERR_PRIVSEP_MSG);
1495 break;
1498 imsg_free(&imsg);
1500 return err;
1503 static const struct got_error *
1504 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1506 const struct got_error *err = NULL;
1507 size_t offset, remain;
1509 offset = 0;
1510 remain = tagmsg_len;
1511 while (remain > 0) {
1512 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1514 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1515 tag->tagmsg + offset, n) == -1) {
1516 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1517 break;
1520 err = flush_imsg(ibuf);
1521 if (err)
1522 break;
1524 offset += n;
1525 remain -= n;
1528 return err;
1531 const struct got_error *
1532 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1534 const struct got_error *err = NULL;
1535 struct got_imsg_tag_object *itag;
1536 uint8_t *buf;
1537 size_t len, total;
1538 size_t tag_len = strlen(tag->tag);
1539 size_t tagger_len = strlen(tag->tagger);
1540 size_t tagmsg_len = strlen(tag->tagmsg);
1542 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1544 buf = malloc(total);
1545 if (buf == NULL)
1546 return got_error_from_errno("malloc");
1548 itag = (struct got_imsg_tag_object *)buf;
1549 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1550 itag->obj_type = tag->obj_type;
1551 itag->tag_len = tag_len;
1552 itag->tagger_len = tagger_len;
1553 itag->tagger_time = tag->tagger_time;
1554 itag->tagger_gmtoff = tag->tagger_gmtoff;
1555 itag->tagmsg_len = tagmsg_len;
1557 len = sizeof(*itag);
1558 memcpy(buf + len, tag->tag, tag_len);
1559 len += tag_len;
1560 memcpy(buf + len, tag->tagger, tagger_len);
1561 len += tagger_len;
1563 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1564 err = got_error_from_errno("imsg_compose TAG");
1565 goto done;
1568 if (tagmsg_len == 0 ||
1569 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1570 err = flush_imsg(ibuf);
1571 if (err)
1572 goto done;
1574 err = send_tagmsg(ibuf, tag, tagmsg_len);
1575 done:
1576 free(buf);
1577 return err;
1580 const struct got_error *
1581 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1583 const struct got_error *err = NULL;
1584 struct imsg imsg;
1585 struct got_imsg_tag_object *itag;
1586 size_t len, datalen;
1587 const size_t min_datalen =
1588 MIN(sizeof(struct got_imsg_error),
1589 sizeof(struct got_imsg_tag_object));
1591 *tag = NULL;
1593 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1594 if (err)
1595 return err;
1597 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1598 len = 0;
1600 switch (imsg.hdr.type) {
1601 case GOT_IMSG_TAG:
1602 if (datalen < sizeof(*itag)) {
1603 err = got_error(GOT_ERR_PRIVSEP_LEN);
1604 break;
1606 itag = imsg.data;
1607 if (datalen != sizeof(*itag) + itag->tag_len +
1608 itag->tagger_len) {
1609 err = got_error(GOT_ERR_PRIVSEP_LEN);
1610 break;
1612 len += sizeof(*itag);
1614 *tag = calloc(1, sizeof(**tag));
1615 if (*tag == NULL) {
1616 err = got_error_from_errno("calloc");
1617 break;
1620 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1622 if (itag->tag_len == 0) {
1623 (*tag)->tag = strdup("");
1624 if ((*tag)->tag == NULL) {
1625 err = got_error_from_errno("strdup");
1626 break;
1628 } else {
1629 (*tag)->tag = malloc(itag->tag_len + 1);
1630 if ((*tag)->tag == NULL) {
1631 err = got_error_from_errno("malloc");
1632 break;
1634 memcpy((*tag)->tag, imsg.data + len,
1635 itag->tag_len);
1636 (*tag)->tag[itag->tag_len] = '\0';
1638 len += itag->tag_len;
1640 (*tag)->obj_type = itag->obj_type;
1641 (*tag)->tagger_time = itag->tagger_time;
1642 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1644 if (itag->tagger_len == 0) {
1645 (*tag)->tagger = strdup("");
1646 if ((*tag)->tagger == NULL) {
1647 err = got_error_from_errno("strdup");
1648 break;
1650 } else {
1651 (*tag)->tagger = malloc(itag->tagger_len + 1);
1652 if ((*tag)->tagger == NULL) {
1653 err = got_error_from_errno("malloc");
1654 break;
1656 memcpy((*tag)->tagger, imsg.data + len,
1657 itag->tagger_len);
1658 (*tag)->tagger[itag->tagger_len] = '\0';
1660 len += itag->tagger_len;
1662 if (itag->tagmsg_len == 0) {
1663 (*tag)->tagmsg = strdup("");
1664 if ((*tag)->tagmsg == NULL) {
1665 err = got_error_from_errno("strdup");
1666 break;
1668 } else {
1669 size_t offset = 0, remain = itag->tagmsg_len;
1671 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1672 if ((*tag)->tagmsg == NULL) {
1673 err = got_error_from_errno("malloc");
1674 break;
1676 while (remain > 0) {
1677 struct imsg imsg_log;
1678 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1679 remain);
1681 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1682 if (err)
1683 return err;
1685 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1686 return got_error(GOT_ERR_PRIVSEP_MSG);
1688 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1689 n);
1690 imsg_free(&imsg_log);
1691 offset += n;
1692 remain -= n;
1694 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1697 break;
1698 default:
1699 err = got_error(GOT_ERR_PRIVSEP_MSG);
1700 break;
1703 imsg_free(&imsg);
1705 return err;
1708 const struct got_error *
1709 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1710 struct got_packidx *packidx)
1712 const struct got_error *err = NULL;
1713 struct got_imsg_packidx ipackidx;
1714 struct got_imsg_pack ipack;
1715 int fd;
1717 ipackidx.len = packidx->len;
1718 fd = dup(packidx->fd);
1719 if (fd == -1)
1720 return got_error_from_errno("dup");
1722 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1723 sizeof(ipackidx)) == -1) {
1724 err = got_error_from_errno("imsg_compose PACKIDX");
1725 close(fd);
1726 return err;
1729 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1730 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1731 return got_error(GOT_ERR_NO_SPACE);
1732 ipack.filesize = pack->filesize;
1734 fd = dup(pack->fd);
1735 if (fd == -1)
1736 return got_error_from_errno("dup");
1738 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1739 == -1) {
1740 err = got_error_from_errno("imsg_compose PACK");
1741 close(fd);
1742 return err;
1745 return flush_imsg(ibuf);
1748 const struct got_error *
1749 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1750 struct got_object_id *id)
1752 struct got_imsg_packed_object iobj;
1754 iobj.idx = idx;
1755 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1757 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1758 &iobj, sizeof(iobj)) == -1)
1759 return got_error_from_errno("imsg_compose "
1760 "PACKED_OBJECT_REQUEST");
1762 return flush_imsg(ibuf);
1765 const struct got_error *
1766 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1768 const struct got_error *err = NULL;
1770 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1771 NULL, 0) == -1) {
1772 err = got_error_from_errno("imsg_compose "
1773 "GITCONFIG_PARSE_REQUEST");
1774 close(fd);
1775 return err;
1778 return flush_imsg(ibuf);
1781 const struct got_error *
1782 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1784 if (imsg_compose(ibuf,
1785 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1786 NULL, 0) == -1)
1787 return got_error_from_errno("imsg_compose "
1788 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1790 return flush_imsg(ibuf);
1793 const struct got_error *
1794 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1796 if (imsg_compose(ibuf,
1797 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1798 return got_error_from_errno("imsg_compose "
1799 "GITCONFIG_AUTHOR_NAME_REQUEST");
1801 return flush_imsg(ibuf);
1804 const struct got_error *
1805 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1807 if (imsg_compose(ibuf,
1808 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1809 return got_error_from_errno("imsg_compose "
1810 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1812 return flush_imsg(ibuf);
1815 const struct got_error *
1816 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1818 if (imsg_compose(ibuf,
1819 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1820 return got_error_from_errno("imsg_compose "
1821 "GITCONFIG_REMOTE_REQUEST");
1823 return flush_imsg(ibuf);
1826 const struct got_error *
1827 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1829 if (imsg_compose(ibuf,
1830 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1831 return got_error_from_errno("imsg_compose "
1832 "GITCONFIG_OWNER_REQUEST");
1834 return flush_imsg(ibuf);
1837 const struct got_error *
1838 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1840 size_t len = value ? strlen(value) + 1 : 0;
1842 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1843 value, len) == -1)
1844 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1846 return flush_imsg(ibuf);
1849 const struct got_error *
1850 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1852 const struct got_error *err = NULL;
1853 struct imsg imsg;
1854 size_t datalen;
1855 const size_t min_datalen = 0;
1857 *str = NULL;
1859 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1860 if (err)
1861 return err;
1862 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1864 switch (imsg.hdr.type) {
1865 case GOT_IMSG_GITCONFIG_STR_VAL:
1866 if (datalen == 0)
1867 break;
1868 *str = malloc(datalen);
1869 if (*str == NULL) {
1870 err = got_error_from_errno("malloc");
1871 break;
1873 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1874 err = got_error(GOT_ERR_NO_SPACE);
1875 break;
1876 default:
1877 err = got_error(GOT_ERR_PRIVSEP_MSG);
1878 break;
1881 imsg_free(&imsg);
1882 return err;
1885 const struct got_error *
1886 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1888 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1889 &value, sizeof(value)) == -1)
1890 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1892 return flush_imsg(ibuf);
1895 const struct got_error *
1896 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1898 const struct got_error *err = NULL;
1899 struct imsg imsg;
1900 size_t datalen;
1901 const size_t min_datalen =
1902 MIN(sizeof(struct got_imsg_error), sizeof(int));
1904 *val = 0;
1906 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1907 if (err)
1908 return err;
1909 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1911 switch (imsg.hdr.type) {
1912 case GOT_IMSG_GITCONFIG_INT_VAL:
1913 if (datalen != sizeof(*val)) {
1914 err = got_error(GOT_ERR_PRIVSEP_LEN);
1915 break;
1917 memcpy(val, imsg.data, sizeof(*val));
1918 break;
1919 default:
1920 err = got_error(GOT_ERR_PRIVSEP_MSG);
1921 break;
1924 imsg_free(&imsg);
1925 return err;
1928 const struct got_error *
1929 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1930 struct got_remote_repo *remotes, int nremotes)
1932 const struct got_error *err = NULL;
1933 struct got_imsg_remotes iremotes;
1934 int i;
1936 iremotes.nremotes = nremotes;
1937 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1938 &iremotes, sizeof(iremotes)) == -1)
1939 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1941 err = flush_imsg(ibuf);
1942 imsg_clear(ibuf);
1943 if (err)
1944 return err;
1946 for (i = 0; i < nremotes; i++) {
1947 struct got_imsg_remote iremote;
1948 size_t len = sizeof(iremote);
1949 struct ibuf *wbuf;
1951 iremote.mirror_references = remotes[i].mirror_references;
1952 iremote.name_len = strlen(remotes[i].name);
1953 len += iremote.name_len;
1954 iremote.url_len = strlen(remotes[i].url);
1955 len += iremote.url_len;
1957 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1958 if (wbuf == NULL)
1959 return got_error_from_errno(
1960 "imsg_create GITCONFIG_REMOTE");
1962 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1963 err = got_error_from_errno(
1964 "imsg_add GITCONFIG_REMOTE");
1965 ibuf_free(wbuf);
1966 return err;
1969 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1970 err = got_error_from_errno(
1971 "imsg_add GITCONFIG_REMOTE");
1972 ibuf_free(wbuf);
1973 return err;
1975 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1976 err = got_error_from_errno(
1977 "imsg_add GITCONFIG_REMOTE");
1978 ibuf_free(wbuf);
1979 return err;
1982 wbuf->fd = -1;
1983 imsg_close(ibuf, wbuf);
1984 err = flush_imsg(ibuf);
1985 if (err)
1986 return err;
1989 return NULL;
1992 const struct got_error *
1993 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1994 int *nremotes, struct imsgbuf *ibuf)
1996 const struct got_error *err = NULL;
1997 struct imsg imsg;
1998 size_t datalen;
1999 struct got_imsg_remotes iremotes;
2000 struct got_imsg_remote iremote;
2002 *remotes = NULL;
2003 *nremotes = 0;
2004 iremotes.nremotes = 0;
2006 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2007 if (err)
2008 return err;
2009 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2011 switch (imsg.hdr.type) {
2012 case GOT_IMSG_GITCONFIG_REMOTES:
2013 if (datalen != sizeof(iremotes)) {
2014 err = got_error(GOT_ERR_PRIVSEP_LEN);
2015 break;
2017 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2018 if (iremotes.nremotes == 0) {
2019 imsg_free(&imsg);
2020 return NULL;
2022 break;
2023 default:
2024 imsg_free(&imsg);
2025 return got_error(GOT_ERR_PRIVSEP_MSG);
2028 imsg_free(&imsg);
2030 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2031 if (*remotes == NULL)
2032 return got_error_from_errno("recallocarray");
2034 while (*nremotes < iremotes.nremotes) {
2035 struct got_remote_repo *remote;
2037 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2038 if (err)
2039 break;
2040 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2042 switch (imsg.hdr.type) {
2043 case GOT_IMSG_GITCONFIG_REMOTE:
2044 remote = &(*remotes)[*nremotes];
2045 if (datalen < sizeof(iremote)) {
2046 err = got_error(GOT_ERR_PRIVSEP_LEN);
2047 break;
2049 memcpy(&iremote, imsg.data, sizeof(iremote));
2050 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2051 (sizeof(iremote) + iremote.name_len +
2052 iremote.url_len) > datalen) {
2053 err = got_error(GOT_ERR_PRIVSEP_LEN);
2054 break;
2056 remote->name = strndup(imsg.data + sizeof(iremote),
2057 iremote.name_len);
2058 if (remote->name == NULL) {
2059 err = got_error_from_errno("strndup");
2060 break;
2062 remote->url = strndup(imsg.data + sizeof(iremote) +
2063 iremote.name_len, iremote.url_len);
2064 if (remote->url == NULL) {
2065 err = got_error_from_errno("strndup");
2066 free(remote->name);
2067 break;
2069 remote->mirror_references = iremote.mirror_references;
2070 (*nremotes)++;
2071 break;
2072 default:
2073 err = got_error(GOT_ERR_PRIVSEP_MSG);
2074 break;
2077 imsg_free(&imsg);
2078 if (err)
2079 break;
2082 if (err) {
2083 int i;
2084 for (i = 0; i < *nremotes; i++) {
2085 free((*remotes)[i].name);
2086 free((*remotes)[i].url);
2088 free(*remotes);
2089 *remotes = NULL;
2090 *nremotes = 0;
2092 return err;
2095 const struct got_error *
2096 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2097 struct got_object_id *id, int idx, const char *path)
2099 const struct got_error *err = NULL;
2100 struct ibuf *wbuf;
2101 size_t path_len = strlen(path) + 1;
2103 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2104 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2105 if (wbuf == NULL)
2106 return got_error_from_errno(
2107 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2108 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2109 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2110 ibuf_free(wbuf);
2111 return err;
2113 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2114 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2115 ibuf_free(wbuf);
2116 return err;
2118 if (imsg_add(wbuf, path, path_len) == -1) {
2119 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2120 ibuf_free(wbuf);
2121 return err;
2124 wbuf->fd = -1;
2125 imsg_close(ibuf, wbuf);
2127 return flush_imsg(ibuf);
2130 const struct got_error *
2131 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2132 size_t ncommits, struct imsgbuf *ibuf)
2134 const struct got_error *err;
2135 struct ibuf *wbuf;
2136 int i;
2138 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2139 sizeof(struct got_imsg_traversed_commits) +
2140 ncommits * SHA1_DIGEST_LENGTH);
2141 if (wbuf == NULL)
2142 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2144 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2145 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2146 ibuf_free(wbuf);
2147 return err;
2149 for (i = 0; i < ncommits; i++) {
2150 struct got_object_id *id = &commit_ids[i];
2151 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2152 err = got_error_from_errno(
2153 "imsg_add TRAVERSED_COMMITS");
2154 ibuf_free(wbuf);
2155 return err;
2159 wbuf->fd = -1;
2160 imsg_close(ibuf, wbuf);
2162 return flush_imsg(ibuf);
2165 const struct got_error *
2166 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2167 struct got_object_id **changed_commit_id,
2168 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2170 const struct got_error *err = NULL;
2171 struct imsg imsg;
2172 struct got_imsg_traversed_commits *icommits;
2173 size_t datalen;
2174 int i, done = 0;
2176 *changed_commit = NULL;
2177 *changed_commit_id = NULL;
2179 while (!done) {
2180 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2181 if (err)
2182 return err;
2184 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2185 switch (imsg.hdr.type) {
2186 case GOT_IMSG_TRAVERSED_COMMITS:
2187 icommits = imsg.data;
2188 if (datalen != sizeof(*icommits) +
2189 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2190 err = got_error(GOT_ERR_PRIVSEP_LEN);
2191 break;
2193 for (i = 0; i < icommits->ncommits; i++) {
2194 struct got_object_qid *qid;
2195 uint8_t *sha1 = (uint8_t *)imsg.data +
2196 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2197 err = got_object_qid_alloc_partial(&qid);
2198 if (err)
2199 break;
2200 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2201 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2203 /* The last commit may contain a change. */
2204 if (i == icommits->ncommits - 1) {
2205 *changed_commit_id =
2206 got_object_id_dup(qid->id);
2207 if (*changed_commit_id == NULL) {
2208 err = got_error_from_errno(
2209 "got_object_id_dup");
2210 break;
2214 break;
2215 case GOT_IMSG_COMMIT:
2216 if (*changed_commit_id == NULL) {
2217 err = got_error(GOT_ERR_PRIVSEP_MSG);
2218 break;
2220 err = get_commit_from_imsg(changed_commit, &imsg,
2221 datalen, ibuf);
2222 break;
2223 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2224 done = 1;
2225 break;
2226 default:
2227 err = got_error(GOT_ERR_PRIVSEP_MSG);
2228 break;
2231 imsg_free(&imsg);
2232 if (err)
2233 break;
2236 if (err)
2237 got_object_id_queue_free(commit_ids);
2238 return err;
2241 const struct got_error *
2242 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2244 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2245 NULL, 0) == -1)
2246 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2248 return flush_imsg(ibuf);
2251 const struct got_error *
2252 got_privsep_unveil_exec_helpers(void)
2254 const char *helpers[] = {
2255 GOT_PATH_PROG_READ_PACK,
2256 GOT_PATH_PROG_READ_OBJECT,
2257 GOT_PATH_PROG_READ_COMMIT,
2258 GOT_PATH_PROG_READ_TREE,
2259 GOT_PATH_PROG_READ_BLOB,
2260 GOT_PATH_PROG_READ_TAG,
2261 GOT_PATH_PROG_READ_GITCONFIG,
2262 GOT_PATH_PROG_FETCH_PACK,
2263 GOT_PATH_PROG_INDEX_PACK,
2265 int i;
2267 for (i = 0; i < nitems(helpers); i++) {
2268 if (unveil(helpers[i], "x") == 0)
2269 continue;
2270 return got_error_from_errno2("unveil", helpers[i]);
2273 return NULL;
2276 void
2277 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2279 if (close(imsg_fds[0]) != 0) {
2280 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2281 _exit(1);
2284 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2285 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2286 _exit(1);
2288 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2289 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2290 _exit(1);
2293 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2294 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2295 strerror(errno));
2296 _exit(1);