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)
417 const struct got_error *err = NULL;
418 struct ibuf *wbuf;
419 size_t len, n_have_refs = 0;
420 struct got_pathlist_entry *pe;
422 len = sizeof(struct got_imsg_fetch_symrefs);
423 TAILQ_FOREACH(pe, have_refs, entry) {
424 struct got_object_id *id = pe->data;
425 len += sizeof(struct got_imsg_fetch_have_ref) +
426 pe->path_len + sizeof(id->sha1);
427 n_have_refs++;
429 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
430 close(fd);
431 return got_error(GOT_ERR_NO_SPACE);
434 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
435 if (wbuf == NULL) {
436 close(fd);
437 return got_error_from_errno("imsg_create FETCH_REQUEST");
440 /* Keep in sync with struct got_imsg_fetch_have_refs definition! */
441 if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
442 err = got_error_from_errno("imsg_add FETCH_REQUEST");
443 ibuf_free(wbuf);
444 close(fd);
445 return err;
448 TAILQ_FOREACH(pe, have_refs, entry) {
449 const char *name = pe->path;
450 size_t name_len = pe->path_len;
451 struct got_object_id *id = pe->data;
453 /* Keep in sync with struct got_imsg_fetch_have_ref! */
454 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
455 err = got_error_from_errno("imsg_add FETCH_REQUEST");
456 ibuf_free(wbuf);
457 close(fd);
458 return err;
460 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
461 err = got_error_from_errno("imsg_add FETCH_REQUEST");
462 ibuf_free(wbuf);
463 close(fd);
464 return err;
466 if (imsg_add(wbuf, name, name_len) == -1) {
467 err = got_error_from_errno("imsg_add FETCH_REQUEST");
468 ibuf_free(wbuf);
469 close(fd);
470 return err;
474 wbuf->fd = fd;
475 imsg_close(ibuf, wbuf);
476 return flush_imsg(ibuf);
479 const struct got_error *
480 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
482 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
486 const struct got_error *
487 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
488 struct got_pathlist_head *symrefs)
490 const struct got_error *err = NULL;
491 struct ibuf *wbuf;
492 size_t len, nsymrefs = 0;
493 struct got_pathlist_entry *pe;
495 len = sizeof(struct got_imsg_fetch_symrefs);
496 TAILQ_FOREACH(pe, symrefs, entry) {
497 const char *target = pe->data;
498 len += sizeof(struct got_imsg_fetch_symref) +
499 pe->path_len + strlen(target);
500 nsymrefs++;
503 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
504 return got_error(GOT_ERR_NO_SPACE);
506 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
507 if (wbuf == NULL)
508 return got_error_from_errno("imsg_create FETCH_SYMREFS");
510 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
511 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
512 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
513 ibuf_free(wbuf);
514 return err;
517 TAILQ_FOREACH(pe, symrefs, entry) {
518 const char *name = pe->path;
519 size_t name_len = pe->path_len;
520 const char *target = pe->data;
521 size_t target_len = strlen(target);
523 /* Keep in sync with struct got_imsg_fetch_symref definition! */
524 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
525 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
526 ibuf_free(wbuf);
527 return err;
529 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
530 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
531 ibuf_free(wbuf);
532 return err;
534 if (imsg_add(wbuf, name, name_len) == -1) {
535 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
536 ibuf_free(wbuf);
537 return err;
539 if (imsg_add(wbuf, target, target_len) == -1) {
540 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
541 ibuf_free(wbuf);
542 return err;
546 wbuf->fd = -1;
547 imsg_close(ibuf, wbuf);
548 return flush_imsg(ibuf);
551 const struct got_error *
552 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
553 struct got_object_id *refid, const char *refname)
555 const struct got_error *err = NULL;
556 struct ibuf *wbuf;
557 size_t len, reflen = strlen(refname);
559 len = sizeof(struct got_imsg_fetch_ref) + reflen;
560 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
561 return got_error(GOT_ERR_NO_SPACE);
563 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
564 if (wbuf == NULL)
565 return got_error_from_errno("imsg_create FETCH_REF");
567 /* Keep in sync with struct got_imsg_fetch_ref definition! */
568 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
569 err = got_error_from_errno("imsg_add FETCH_REF");
570 ibuf_free(wbuf);
571 return err;
573 if (imsg_add(wbuf, refname, reflen) == -1) {
574 err = got_error_from_errno("imsg_add FETCH_REF");
575 ibuf_free(wbuf);
576 return err;
579 wbuf->fd = -1;
580 imsg_close(ibuf, wbuf);
581 return flush_imsg(ibuf);
584 const struct got_error *
585 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
586 size_t msglen)
588 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
589 return got_error(GOT_ERR_NO_SPACE);
591 if (msglen == 0)
592 return NULL;
594 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
595 msg, msglen) == -1)
596 return got_error_from_errno(
597 "imsg_compose FETCH_SERVER_PROGRESS");
599 return flush_imsg(ibuf);
602 const struct got_error *
603 got_privsep_send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
605 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
606 &bytes, sizeof(bytes)) == -1)
607 return got_error_from_errno(
608 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
610 return flush_imsg(ibuf);
613 const struct got_error *
614 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
616 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
617 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
618 return got_error_from_errno("imsg_compose FETCH");
619 return flush_imsg(ibuf);
623 const struct got_error *
624 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
625 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
626 off_t *packfile_size, struct imsgbuf *ibuf)
628 const struct got_error *err = NULL;
629 struct imsg imsg;
630 size_t datalen;
631 struct got_imsg_fetch_symrefs *isymrefs = NULL;
632 size_t n, remain;
633 off_t off;
634 int i;
636 *done = 0;
637 *id = NULL;
638 *refname = NULL;
639 *server_progress = NULL;
640 *packfile_size = 0;
642 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
643 if (err)
644 return err;
646 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
647 switch (imsg.hdr.type) {
648 case GOT_IMSG_ERROR:
649 if (datalen < sizeof(struct got_imsg_error)) {
650 err = got_error(GOT_ERR_PRIVSEP_LEN);
651 break;
653 err = recv_imsg_error(&imsg, datalen);
654 break;
655 case GOT_IMSG_FETCH_SYMREFS:
656 if (datalen < sizeof(*isymrefs)) {
657 err = got_error(GOT_ERR_PRIVSEP_LEN);
658 break;
660 if (isymrefs != NULL) {
661 err = got_error(GOT_ERR_PRIVSEP_MSG);
662 break;
664 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
665 off = sizeof(*isymrefs);
666 remain = datalen - off;
667 for (n = 0; n < isymrefs->nsymrefs; n++) {
668 struct got_imsg_fetch_symref *s;
669 char *name, *target;
670 if (remain < sizeof(struct got_imsg_fetch_symref)) {
671 err = got_error(GOT_ERR_PRIVSEP_LEN);
672 goto done;
674 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
675 off += sizeof(*s);
676 remain -= sizeof(*s);
677 if (remain < s->name_len) {
678 err = got_error(GOT_ERR_PRIVSEP_LEN);
679 goto done;
681 name = strndup(imsg.data + off, s->name_len);
682 if (name == NULL) {
683 err = got_error_from_errno("strndup");
684 goto done;
686 off += s->name_len;
687 remain -= s->name_len;
688 if (remain < s->target_len) {
689 err = got_error(GOT_ERR_PRIVSEP_LEN);
690 free(name);
691 goto done;
693 target = strndup(imsg.data + off, s->target_len);
694 if (target == NULL) {
695 err = got_error_from_errno("strndup");
696 free(name);
697 goto done;
699 off += s->target_len;
700 remain -= s->target_len;
701 err = got_pathlist_append(symrefs, name, target);
702 if (err) {
703 free(name);
704 free(target);
705 goto done;
708 break;
709 case GOT_IMSG_FETCH_REF:
710 if (datalen <= SHA1_DIGEST_LENGTH) {
711 err = got_error(GOT_ERR_PRIVSEP_MSG);
712 break;
714 *id = malloc(sizeof(**id));
715 if (*id == NULL) {
716 err = got_error_from_errno("malloc");
717 break;
719 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
720 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
721 datalen - SHA1_DIGEST_LENGTH);
722 if (*refname == NULL) {
723 err = got_error_from_errno("strndup");
724 break;
726 break;
727 case GOT_IMSG_FETCH_SERVER_PROGRESS:
728 if (datalen == 0) {
729 err = got_error(GOT_ERR_PRIVSEP_LEN);
730 break;
732 *server_progress = strndup(imsg.data, datalen);
733 if (*server_progress == NULL) {
734 err = got_error_from_errno("strndup");
735 break;
737 for (i = 0; i < datalen; i++) {
738 if (!isprint((unsigned char)(*server_progress)[i]) &&
739 !isspace((unsigned char)(*server_progress)[i])) {
740 err = got_error(GOT_ERR_PRIVSEP_MSG);
741 free(*server_progress);
742 *server_progress = NULL;
743 goto done;
746 break;
747 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
748 if (datalen < sizeof(*packfile_size)) {
749 err = got_error(GOT_ERR_PRIVSEP_MSG);
750 break;
752 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
753 break;
754 case GOT_IMSG_FETCH_DONE:
755 *id = malloc(sizeof(**id));
756 if (*id == NULL) {
757 err = got_error_from_errno("malloc");
758 break;
760 if (datalen != SHA1_DIGEST_LENGTH) {
761 err = got_error(GOT_ERR_PRIVSEP_MSG);
762 break;
764 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
765 *done = 1;
766 break;
767 default:
768 err = got_error(GOT_ERR_PRIVSEP_MSG);
769 break;
771 done:
772 if (err) {
773 free(*id);
774 *id = NULL;
775 free(*refname);
776 *refname = NULL;
778 imsg_free(&imsg);
779 return err;
782 const struct got_error *
783 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
784 int fd)
786 const struct got_error *err = NULL;
788 /* Keep in sync with struct got_imsg_index_pack_request */
789 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
790 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
791 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
792 close(fd);
793 return err;
795 return flush_imsg(ibuf);
798 const struct got_error *
799 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
801 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
804 const struct got_error *
805 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
806 int nobj_indexed, int nobj_loose, int nobj_resolved)
808 struct got_imsg_index_pack_progress iprogress;
810 iprogress.nobj_total = nobj_total;
811 iprogress.nobj_indexed = nobj_indexed;
812 iprogress.nobj_loose = nobj_loose;
813 iprogress.nobj_resolved = nobj_resolved;
815 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
816 &iprogress, sizeof(iprogress)) == -1)
817 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
819 return flush_imsg(ibuf);
822 const struct got_error *
823 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
825 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
826 return got_error_from_errno("imsg_compose FETCH");
827 return flush_imsg(ibuf);
830 const struct got_error *
831 got_privsep_recv_index_progress(int *done, int *nobj_total,
832 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
833 struct imsgbuf *ibuf)
835 const struct got_error *err = NULL;
836 struct imsg imsg;
837 struct got_imsg_index_pack_progress *iprogress;
838 size_t datalen;
840 *done = 0;
841 *nobj_total = 0;
842 *nobj_indexed = 0;
843 *nobj_resolved = 0;
845 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
846 if (err)
847 return err;
849 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
850 switch (imsg.hdr.type) {
851 case GOT_IMSG_ERROR:
852 if (datalen < sizeof(struct got_imsg_error)) {
853 err = got_error(GOT_ERR_PRIVSEP_LEN);
854 break;
856 err = recv_imsg_error(&imsg, datalen);
857 break;
858 case GOT_IMSG_IDXPACK_PROGRESS:
859 if (datalen < sizeof(*iprogress)) {
860 err = got_error(GOT_ERR_PRIVSEP_LEN);
861 break;
863 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
864 *nobj_total = iprogress->nobj_total;
865 *nobj_indexed = iprogress->nobj_indexed;
866 *nobj_loose = iprogress->nobj_loose;
867 *nobj_resolved = iprogress->nobj_resolved;
868 break;
869 case GOT_IMSG_IDXPACK_DONE:
870 if (datalen != 0) {
871 err = got_error(GOT_ERR_PRIVSEP_LEN);
872 break;
874 *done = 1;
875 break;
876 default:
877 err = got_error(GOT_ERR_PRIVSEP_MSG);
878 break;
881 imsg_free(&imsg);
882 return err;
885 const struct got_error *
886 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
887 struct imsgbuf *ibuf)
889 const struct got_error *err = NULL;
890 struct got_imsg_object *iobj;
891 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
893 if (datalen != sizeof(*iobj))
894 return got_error(GOT_ERR_PRIVSEP_LEN);
895 iobj = imsg->data;
897 *obj = calloc(1, sizeof(**obj));
898 if (*obj == NULL)
899 return got_error_from_errno("calloc");
901 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
902 (*obj)->type = iobj->type;
903 (*obj)->flags = iobj->flags;
904 (*obj)->hdrlen = iobj->hdrlen;
905 (*obj)->size = iobj->size;
906 /* path_packfile is handled by caller */
907 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
908 (*obj)->pack_offset = iobj->pack_offset;
909 (*obj)->pack_idx = iobj->pack_idx;
912 return err;
915 const struct got_error *
916 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
918 const struct got_error *err = NULL;
919 struct imsg imsg;
920 const size_t min_datalen =
921 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
923 *obj = NULL;
925 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
926 if (err)
927 return err;
929 switch (imsg.hdr.type) {
930 case GOT_IMSG_OBJECT:
931 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
932 break;
933 default:
934 err = got_error(GOT_ERR_PRIVSEP_MSG);
935 break;
938 imsg_free(&imsg);
940 return err;
943 static const struct got_error *
944 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
945 size_t logmsg_len)
947 const struct got_error *err = NULL;
948 size_t offset, remain;
950 offset = 0;
951 remain = logmsg_len;
952 while (remain > 0) {
953 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
955 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
956 commit->logmsg + offset, n) == -1) {
957 err = got_error_from_errno("imsg_compose "
958 "COMMIT_LOGMSG");
959 break;
962 err = flush_imsg(ibuf);
963 if (err)
964 break;
966 offset += n;
967 remain -= n;
970 return err;
973 const struct got_error *
974 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
976 const struct got_error *err = NULL;
977 struct got_imsg_commit_object *icommit;
978 uint8_t *buf;
979 size_t len, total;
980 struct got_object_qid *qid;
981 size_t author_len = strlen(commit->author);
982 size_t committer_len = strlen(commit->committer);
983 size_t logmsg_len = strlen(commit->logmsg);
985 total = sizeof(*icommit) + author_len + committer_len +
986 commit->nparents * SHA1_DIGEST_LENGTH;
988 buf = malloc(total);
989 if (buf == NULL)
990 return got_error_from_errno("malloc");
992 icommit = (struct got_imsg_commit_object *)buf;
993 memcpy(icommit->tree_id, commit->tree_id->sha1,
994 sizeof(icommit->tree_id));
995 icommit->author_len = author_len;
996 icommit->author_time = commit->author_time;
997 icommit->author_gmtoff = commit->author_gmtoff;
998 icommit->committer_len = committer_len;
999 icommit->committer_time = commit->committer_time;
1000 icommit->committer_gmtoff = commit->committer_gmtoff;
1001 icommit->logmsg_len = logmsg_len;
1002 icommit->nparents = commit->nparents;
1004 len = sizeof(*icommit);
1005 memcpy(buf + len, commit->author, author_len);
1006 len += author_len;
1007 memcpy(buf + len, commit->committer, committer_len);
1008 len += committer_len;
1009 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1010 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1011 len += SHA1_DIGEST_LENGTH;
1014 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1015 err = got_error_from_errno("imsg_compose COMMIT");
1016 goto done;
1019 if (logmsg_len == 0 ||
1020 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1021 err = flush_imsg(ibuf);
1022 if (err)
1023 goto done;
1025 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1026 done:
1027 free(buf);
1028 return err;
1031 static const struct got_error *
1032 get_commit_from_imsg(struct got_commit_object **commit,
1033 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1035 const struct got_error *err = NULL;
1036 struct got_imsg_commit_object *icommit;
1037 size_t len = 0;
1038 int i;
1040 if (datalen < sizeof(*icommit))
1041 return got_error(GOT_ERR_PRIVSEP_LEN);
1043 icommit = imsg->data;
1044 if (datalen != sizeof(*icommit) + icommit->author_len +
1045 icommit->committer_len +
1046 icommit->nparents * SHA1_DIGEST_LENGTH)
1047 return got_error(GOT_ERR_PRIVSEP_LEN);
1049 if (icommit->nparents < 0)
1050 return got_error(GOT_ERR_PRIVSEP_LEN);
1052 len += sizeof(*icommit);
1054 *commit = got_object_commit_alloc_partial();
1055 if (*commit == NULL)
1056 return got_error_from_errno(
1057 "got_object_commit_alloc_partial");
1059 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1060 SHA1_DIGEST_LENGTH);
1061 (*commit)->author_time = icommit->author_time;
1062 (*commit)->author_gmtoff = icommit->author_gmtoff;
1063 (*commit)->committer_time = icommit->committer_time;
1064 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1066 if (icommit->author_len == 0) {
1067 (*commit)->author = strdup("");
1068 if ((*commit)->author == NULL) {
1069 err = got_error_from_errno("strdup");
1070 goto done;
1072 } else {
1073 (*commit)->author = malloc(icommit->author_len + 1);
1074 if ((*commit)->author == NULL) {
1075 err = got_error_from_errno("malloc");
1076 goto done;
1078 memcpy((*commit)->author, imsg->data + len,
1079 icommit->author_len);
1080 (*commit)->author[icommit->author_len] = '\0';
1082 len += icommit->author_len;
1084 if (icommit->committer_len == 0) {
1085 (*commit)->committer = strdup("");
1086 if ((*commit)->committer == NULL) {
1087 err = got_error_from_errno("strdup");
1088 goto done;
1090 } else {
1091 (*commit)->committer =
1092 malloc(icommit->committer_len + 1);
1093 if ((*commit)->committer == NULL) {
1094 err = got_error_from_errno("malloc");
1095 goto done;
1097 memcpy((*commit)->committer, imsg->data + len,
1098 icommit->committer_len);
1099 (*commit)->committer[icommit->committer_len] = '\0';
1101 len += icommit->committer_len;
1103 if (icommit->logmsg_len == 0) {
1104 (*commit)->logmsg = strdup("");
1105 if ((*commit)->logmsg == NULL) {
1106 err = got_error_from_errno("strdup");
1107 goto done;
1109 } else {
1110 size_t offset = 0, remain = icommit->logmsg_len;
1112 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1113 if ((*commit)->logmsg == NULL) {
1114 err = got_error_from_errno("malloc");
1115 goto done;
1117 while (remain > 0) {
1118 struct imsg imsg_log;
1119 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1120 remain);
1122 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1123 if (err)
1124 goto done;
1126 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1127 err = got_error(GOT_ERR_PRIVSEP_MSG);
1128 goto done;
1131 memcpy((*commit)->logmsg + offset,
1132 imsg_log.data, n);
1133 imsg_free(&imsg_log);
1134 offset += n;
1135 remain -= n;
1137 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1140 for (i = 0; i < icommit->nparents; i++) {
1141 struct got_object_qid *qid;
1143 err = got_object_qid_alloc_partial(&qid);
1144 if (err)
1145 break;
1146 memcpy(qid->id, imsg->data + len +
1147 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1148 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1149 (*commit)->nparents++;
1151 done:
1152 if (err) {
1153 got_object_commit_close(*commit);
1154 *commit = NULL;
1156 return err;
1159 const struct got_error *
1160 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1162 const struct got_error *err = NULL;
1163 struct imsg imsg;
1164 size_t datalen;
1165 const size_t min_datalen =
1166 MIN(sizeof(struct got_imsg_error),
1167 sizeof(struct got_imsg_commit_object));
1169 *commit = NULL;
1171 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1172 if (err)
1173 return err;
1175 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1177 switch (imsg.hdr.type) {
1178 case GOT_IMSG_COMMIT:
1179 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1180 break;
1181 default:
1182 err = got_error(GOT_ERR_PRIVSEP_MSG);
1183 break;
1186 imsg_free(&imsg);
1188 return err;
1191 const struct got_error *
1192 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1193 int nentries)
1195 const struct got_error *err = NULL;
1196 struct got_imsg_tree_object itree;
1197 struct got_pathlist_entry *pe;
1198 size_t totlen;
1199 int nimsg; /* number of imsg queued in ibuf */
1201 itree.nentries = nentries;
1202 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1203 == -1)
1204 return got_error_from_errno("imsg_compose TREE");
1206 totlen = sizeof(itree);
1207 nimsg = 1;
1208 TAILQ_FOREACH(pe, entries, entry) {
1209 const char *name = pe->path;
1210 struct got_parsed_tree_entry *pte = pe->data;
1211 struct ibuf *wbuf;
1212 size_t namelen = strlen(name);
1213 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1215 if (len > MAX_IMSGSIZE)
1216 return got_error(GOT_ERR_NO_SPACE);
1218 nimsg++;
1219 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1220 err = flush_imsg(ibuf);
1221 if (err)
1222 return err;
1223 nimsg = 0;
1226 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1227 if (wbuf == NULL)
1228 return got_error_from_errno("imsg_create TREE_ENTRY");
1230 /* Keep in sync with struct got_imsg_tree_object definition! */
1231 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1232 err = got_error_from_errno("imsg_add TREE_ENTRY");
1233 ibuf_free(wbuf);
1234 return err;
1236 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1237 err = got_error_from_errno("imsg_add TREE_ENTRY");
1238 ibuf_free(wbuf);
1239 return err;
1242 if (imsg_add(wbuf, name, namelen) == -1) {
1243 err = got_error_from_errno("imsg_add TREE_ENTRY");
1244 ibuf_free(wbuf);
1245 return err;
1248 wbuf->fd = -1;
1249 imsg_close(ibuf, wbuf);
1251 totlen += len;
1254 return flush_imsg(ibuf);
1257 const struct got_error *
1258 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1260 const struct got_error *err = NULL;
1261 const size_t min_datalen =
1262 MIN(sizeof(struct got_imsg_error),
1263 sizeof(struct got_imsg_tree_object));
1264 struct got_imsg_tree_object *itree;
1265 int nentries = 0;
1267 *tree = NULL;
1268 get_more:
1269 err = read_imsg(ibuf);
1270 if (err)
1271 goto done;
1273 for (;;) {
1274 struct imsg imsg;
1275 size_t n;
1276 size_t datalen;
1277 struct got_imsg_tree_entry *ite;
1278 struct got_tree_entry *te = NULL;
1280 n = imsg_get(ibuf, &imsg);
1281 if (n == 0) {
1282 if (*tree && (*tree)->nentries != nentries)
1283 goto get_more;
1284 break;
1287 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1288 return got_error(GOT_ERR_PRIVSEP_LEN);
1290 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1292 switch (imsg.hdr.type) {
1293 case GOT_IMSG_ERROR:
1294 err = recv_imsg_error(&imsg, datalen);
1295 break;
1296 case GOT_IMSG_TREE:
1297 /* This message should only appear once. */
1298 if (*tree != NULL) {
1299 err = got_error(GOT_ERR_PRIVSEP_MSG);
1300 break;
1302 if (datalen != sizeof(*itree)) {
1303 err = got_error(GOT_ERR_PRIVSEP_LEN);
1304 break;
1306 itree = imsg.data;
1307 *tree = malloc(sizeof(**tree));
1308 if (*tree == NULL) {
1309 err = got_error_from_errno("malloc");
1310 break;
1312 (*tree)->entries = calloc(itree->nentries,
1313 sizeof(struct got_tree_entry));
1314 if ((*tree)->entries == NULL) {
1315 err = got_error_from_errno("malloc");
1316 break;
1318 (*tree)->nentries = itree->nentries;
1319 (*tree)->refcnt = 0;
1320 break;
1321 case GOT_IMSG_TREE_ENTRY:
1322 /* This message should be preceeded by GOT_IMSG_TREE. */
1323 if (*tree == NULL) {
1324 err = got_error(GOT_ERR_PRIVSEP_MSG);
1325 break;
1327 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1328 err = got_error(GOT_ERR_PRIVSEP_LEN);
1329 break;
1332 /* Remaining data contains the entry's name. */
1333 datalen -= sizeof(*ite);
1334 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1335 err = got_error(GOT_ERR_PRIVSEP_LEN);
1336 break;
1338 ite = imsg.data;
1340 if (datalen + 1 > sizeof(te->name)) {
1341 err = got_error(GOT_ERR_NO_SPACE);
1342 break;
1344 te = &(*tree)->entries[nentries];
1345 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1346 te->name[datalen] = '\0';
1348 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1349 te->mode = ite->mode;
1350 te->idx = nentries;
1351 nentries++;
1352 break;
1353 default:
1354 err = got_error(GOT_ERR_PRIVSEP_MSG);
1355 break;
1358 imsg_free(&imsg);
1360 done:
1361 if (*tree && (*tree)->nentries != nentries) {
1362 if (err == NULL)
1363 err = got_error(GOT_ERR_PRIVSEP_LEN);
1364 got_object_tree_close(*tree);
1365 *tree = NULL;
1368 return err;
1371 const struct got_error *
1372 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1373 const uint8_t *data)
1375 struct got_imsg_blob iblob;
1377 iblob.size = size;
1378 iblob.hdrlen = hdrlen;
1380 if (data) {
1381 uint8_t *buf;
1383 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1384 return got_error(GOT_ERR_NO_SPACE);
1386 buf = malloc(sizeof(iblob) + size);
1387 if (buf == NULL)
1388 return got_error_from_errno("malloc");
1390 memcpy(buf, &iblob, sizeof(iblob));
1391 memcpy(buf + sizeof(iblob), data, size);
1392 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1393 sizeof(iblob) + size) == -1) {
1394 free(buf);
1395 return got_error_from_errno("imsg_compose BLOB");
1397 free(buf);
1398 } else {
1399 /* Data has already been written to file descriptor. */
1400 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1401 sizeof(iblob)) == -1)
1402 return got_error_from_errno("imsg_compose BLOB");
1406 return flush_imsg(ibuf);
1409 const struct got_error *
1410 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1411 struct imsgbuf *ibuf)
1413 const struct got_error *err = NULL;
1414 struct imsg imsg;
1415 struct got_imsg_blob *iblob;
1416 size_t datalen;
1418 *outbuf = NULL;
1420 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1421 if (err)
1422 return err;
1424 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1426 switch (imsg.hdr.type) {
1427 case GOT_IMSG_BLOB:
1428 if (datalen < sizeof(*iblob)) {
1429 err = got_error(GOT_ERR_PRIVSEP_LEN);
1430 break;
1432 iblob = imsg.data;
1433 *size = iblob->size;
1434 *hdrlen = iblob->hdrlen;
1436 if (datalen == sizeof(*iblob)) {
1437 /* Data has been written to file descriptor. */
1438 break;
1441 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1442 err = got_error(GOT_ERR_PRIVSEP_LEN);
1443 break;
1446 *outbuf = malloc(*size);
1447 if (*outbuf == NULL) {
1448 err = got_error_from_errno("malloc");
1449 break;
1451 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1452 break;
1453 default:
1454 err = got_error(GOT_ERR_PRIVSEP_MSG);
1455 break;
1458 imsg_free(&imsg);
1460 return err;
1463 static const struct got_error *
1464 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1466 const struct got_error *err = NULL;
1467 size_t offset, remain;
1469 offset = 0;
1470 remain = tagmsg_len;
1471 while (remain > 0) {
1472 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1474 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1475 tag->tagmsg + offset, n) == -1) {
1476 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1477 break;
1480 err = flush_imsg(ibuf);
1481 if (err)
1482 break;
1484 offset += n;
1485 remain -= n;
1488 return err;
1491 const struct got_error *
1492 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1494 const struct got_error *err = NULL;
1495 struct got_imsg_tag_object *itag;
1496 uint8_t *buf;
1497 size_t len, total;
1498 size_t tag_len = strlen(tag->tag);
1499 size_t tagger_len = strlen(tag->tagger);
1500 size_t tagmsg_len = strlen(tag->tagmsg);
1502 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1504 buf = malloc(total);
1505 if (buf == NULL)
1506 return got_error_from_errno("malloc");
1508 itag = (struct got_imsg_tag_object *)buf;
1509 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1510 itag->obj_type = tag->obj_type;
1511 itag->tag_len = tag_len;
1512 itag->tagger_len = tagger_len;
1513 itag->tagger_time = tag->tagger_time;
1514 itag->tagger_gmtoff = tag->tagger_gmtoff;
1515 itag->tagmsg_len = tagmsg_len;
1517 len = sizeof(*itag);
1518 memcpy(buf + len, tag->tag, tag_len);
1519 len += tag_len;
1520 memcpy(buf + len, tag->tagger, tagger_len);
1521 len += tagger_len;
1523 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1524 err = got_error_from_errno("imsg_compose TAG");
1525 goto done;
1528 if (tagmsg_len == 0 ||
1529 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1530 err = flush_imsg(ibuf);
1531 if (err)
1532 goto done;
1534 err = send_tagmsg(ibuf, tag, tagmsg_len);
1535 done:
1536 free(buf);
1537 return err;
1540 const struct got_error *
1541 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1543 const struct got_error *err = NULL;
1544 struct imsg imsg;
1545 struct got_imsg_tag_object *itag;
1546 size_t len, datalen;
1547 const size_t min_datalen =
1548 MIN(sizeof(struct got_imsg_error),
1549 sizeof(struct got_imsg_tag_object));
1551 *tag = NULL;
1553 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1554 if (err)
1555 return err;
1557 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1558 len = 0;
1560 switch (imsg.hdr.type) {
1561 case GOT_IMSG_TAG:
1562 if (datalen < sizeof(*itag)) {
1563 err = got_error(GOT_ERR_PRIVSEP_LEN);
1564 break;
1566 itag = imsg.data;
1567 if (datalen != sizeof(*itag) + itag->tag_len +
1568 itag->tagger_len) {
1569 err = got_error(GOT_ERR_PRIVSEP_LEN);
1570 break;
1572 len += sizeof(*itag);
1574 *tag = calloc(1, sizeof(**tag));
1575 if (*tag == NULL) {
1576 err = got_error_from_errno("calloc");
1577 break;
1580 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1582 if (itag->tag_len == 0) {
1583 (*tag)->tag = strdup("");
1584 if ((*tag)->tag == NULL) {
1585 err = got_error_from_errno("strdup");
1586 break;
1588 } else {
1589 (*tag)->tag = malloc(itag->tag_len + 1);
1590 if ((*tag)->tag == NULL) {
1591 err = got_error_from_errno("malloc");
1592 break;
1594 memcpy((*tag)->tag, imsg.data + len,
1595 itag->tag_len);
1596 (*tag)->tag[itag->tag_len] = '\0';
1598 len += itag->tag_len;
1600 (*tag)->obj_type = itag->obj_type;
1601 (*tag)->tagger_time = itag->tagger_time;
1602 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1604 if (itag->tagger_len == 0) {
1605 (*tag)->tagger = strdup("");
1606 if ((*tag)->tagger == NULL) {
1607 err = got_error_from_errno("strdup");
1608 break;
1610 } else {
1611 (*tag)->tagger = malloc(itag->tagger_len + 1);
1612 if ((*tag)->tagger == NULL) {
1613 err = got_error_from_errno("malloc");
1614 break;
1616 memcpy((*tag)->tagger, imsg.data + len,
1617 itag->tagger_len);
1618 (*tag)->tagger[itag->tagger_len] = '\0';
1620 len += itag->tagger_len;
1622 if (itag->tagmsg_len == 0) {
1623 (*tag)->tagmsg = strdup("");
1624 if ((*tag)->tagmsg == NULL) {
1625 err = got_error_from_errno("strdup");
1626 break;
1628 } else {
1629 size_t offset = 0, remain = itag->tagmsg_len;
1631 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1632 if ((*tag)->tagmsg == NULL) {
1633 err = got_error_from_errno("malloc");
1634 break;
1636 while (remain > 0) {
1637 struct imsg imsg_log;
1638 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1639 remain);
1641 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1642 if (err)
1643 return err;
1645 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1646 return got_error(GOT_ERR_PRIVSEP_MSG);
1648 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1649 n);
1650 imsg_free(&imsg_log);
1651 offset += n;
1652 remain -= n;
1654 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1657 break;
1658 default:
1659 err = got_error(GOT_ERR_PRIVSEP_MSG);
1660 break;
1663 imsg_free(&imsg);
1665 return err;
1668 const struct got_error *
1669 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1670 struct got_packidx *packidx)
1672 const struct got_error *err = NULL;
1673 struct got_imsg_packidx ipackidx;
1674 struct got_imsg_pack ipack;
1675 int fd;
1677 ipackidx.len = packidx->len;
1678 fd = dup(packidx->fd);
1679 if (fd == -1)
1680 return got_error_from_errno("dup");
1682 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1683 sizeof(ipackidx)) == -1) {
1684 err = got_error_from_errno("imsg_compose PACKIDX");
1685 close(fd);
1686 return err;
1689 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1690 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1691 return got_error(GOT_ERR_NO_SPACE);
1692 ipack.filesize = pack->filesize;
1694 fd = dup(pack->fd);
1695 if (fd == -1)
1696 return got_error_from_errno("dup");
1698 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1699 == -1) {
1700 err = got_error_from_errno("imsg_compose PACK");
1701 close(fd);
1702 return err;
1705 return flush_imsg(ibuf);
1708 const struct got_error *
1709 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1710 struct got_object_id *id)
1712 struct got_imsg_packed_object iobj;
1714 iobj.idx = idx;
1715 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1717 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1718 &iobj, sizeof(iobj)) == -1)
1719 return got_error_from_errno("imsg_compose "
1720 "PACKED_OBJECT_REQUEST");
1722 return flush_imsg(ibuf);
1725 const struct got_error *
1726 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1728 const struct got_error *err = NULL;
1730 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1731 NULL, 0) == -1) {
1732 err = got_error_from_errno("imsg_compose "
1733 "GITCONFIG_PARSE_REQUEST");
1734 close(fd);
1735 return err;
1738 return flush_imsg(ibuf);
1741 const struct got_error *
1742 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1744 if (imsg_compose(ibuf,
1745 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1746 NULL, 0) == -1)
1747 return got_error_from_errno("imsg_compose "
1748 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1750 return flush_imsg(ibuf);
1753 const struct got_error *
1754 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1756 if (imsg_compose(ibuf,
1757 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1758 return got_error_from_errno("imsg_compose "
1759 "GITCONFIG_AUTHOR_NAME_REQUEST");
1761 return flush_imsg(ibuf);
1764 const struct got_error *
1765 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1767 if (imsg_compose(ibuf,
1768 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1769 return got_error_from_errno("imsg_compose "
1770 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1772 return flush_imsg(ibuf);
1775 const struct got_error *
1776 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1778 if (imsg_compose(ibuf,
1779 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1780 return got_error_from_errno("imsg_compose "
1781 "GITCONFIG_REMOTE_REQUEST");
1783 return flush_imsg(ibuf);
1786 const struct got_error *
1787 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1789 if (imsg_compose(ibuf,
1790 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1791 return got_error_from_errno("imsg_compose "
1792 "GITCONFIG_OWNER_REQUEST");
1794 return flush_imsg(ibuf);
1797 const struct got_error *
1798 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1800 size_t len = value ? strlen(value) + 1 : 0;
1802 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1803 value, len) == -1)
1804 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1806 return flush_imsg(ibuf);
1809 const struct got_error *
1810 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1812 const struct got_error *err = NULL;
1813 struct imsg imsg;
1814 size_t datalen;
1815 const size_t min_datalen = 0;
1817 *str = NULL;
1819 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1820 if (err)
1821 return err;
1822 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1824 switch (imsg.hdr.type) {
1825 case GOT_IMSG_GITCONFIG_STR_VAL:
1826 if (datalen == 0)
1827 break;
1828 *str = malloc(datalen);
1829 if (*str == NULL) {
1830 err = got_error_from_errno("malloc");
1831 break;
1833 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1834 err = got_error(GOT_ERR_NO_SPACE);
1835 break;
1836 default:
1837 err = got_error(GOT_ERR_PRIVSEP_MSG);
1838 break;
1841 imsg_free(&imsg);
1842 return err;
1845 const struct got_error *
1846 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1848 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1849 &value, sizeof(value)) == -1)
1850 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1852 return flush_imsg(ibuf);
1855 const struct got_error *
1856 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1858 const struct got_error *err = NULL;
1859 struct imsg imsg;
1860 size_t datalen;
1861 const size_t min_datalen =
1862 MIN(sizeof(struct got_imsg_error), sizeof(int));
1864 *val = 0;
1866 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1867 if (err)
1868 return err;
1869 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1871 switch (imsg.hdr.type) {
1872 case GOT_IMSG_GITCONFIG_INT_VAL:
1873 if (datalen != sizeof(*val)) {
1874 err = got_error(GOT_ERR_PRIVSEP_LEN);
1875 break;
1877 memcpy(val, imsg.data, sizeof(*val));
1878 break;
1879 default:
1880 err = got_error(GOT_ERR_PRIVSEP_MSG);
1881 break;
1884 imsg_free(&imsg);
1885 return err;
1888 const struct got_error *
1889 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1890 struct got_remote_repo *remotes, int nremotes)
1892 const struct got_error *err = NULL;
1893 struct got_imsg_remotes iremotes;
1894 int i;
1896 iremotes.nremotes = nremotes;
1897 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1898 &iremotes, sizeof(iremotes)) == -1)
1899 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1901 err = flush_imsg(ibuf);
1902 imsg_clear(ibuf);
1903 if (err)
1904 return err;
1906 for (i = 0; i < nremotes; i++) {
1907 struct got_imsg_remote iremote;
1908 size_t len = sizeof(iremote);
1909 struct ibuf *wbuf;
1911 iremote.name_len = strlen(remotes[i].name);
1912 len += iremote.name_len;
1913 iremote.url_len = strlen(remotes[i].url);
1914 len += iremote.url_len;
1916 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1917 if (wbuf == NULL)
1918 return got_error_from_errno(
1919 "imsg_create GITCONFIG_REMOTE");
1921 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1922 err = got_error_from_errno(
1923 "imsg_add GIITCONFIG_REMOTE");
1924 ibuf_free(wbuf);
1925 return err;
1928 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1929 err = got_error_from_errno(
1930 "imsg_add GIITCONFIG_REMOTE");
1931 ibuf_free(wbuf);
1932 return err;
1934 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1935 err = got_error_from_errno(
1936 "imsg_add GIITCONFIG_REMOTE");
1937 ibuf_free(wbuf);
1938 return err;
1941 wbuf->fd = -1;
1942 imsg_close(ibuf, wbuf);
1943 err = flush_imsg(ibuf);
1944 if (err)
1945 return err;
1948 return NULL;
1951 const struct got_error *
1952 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1953 int *nremotes, struct imsgbuf *ibuf)
1955 const struct got_error *err = NULL;
1956 struct imsg imsg;
1957 size_t datalen;
1958 struct got_imsg_remotes iremotes;
1959 struct got_imsg_remote iremote;
1961 *remotes = NULL;
1962 *nremotes = 0;
1963 iremotes.nremotes = 0;
1965 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1966 if (err)
1967 return err;
1968 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1970 switch (imsg.hdr.type) {
1971 case GOT_IMSG_GITCONFIG_REMOTES:
1972 if (datalen != sizeof(iremotes)) {
1973 err = got_error(GOT_ERR_PRIVSEP_LEN);
1974 break;
1976 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1977 if (iremotes.nremotes == 0) {
1978 imsg_free(&imsg);
1979 return NULL;
1981 break;
1982 default:
1983 imsg_free(&imsg);
1984 return got_error(GOT_ERR_PRIVSEP_MSG);
1987 imsg_free(&imsg);
1989 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1990 if (*remotes == NULL)
1991 return got_error_from_errno("recallocarray");
1993 while (*nremotes < iremotes.nremotes) {
1994 struct got_remote_repo *remote;
1996 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1997 if (err)
1998 break;
1999 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2001 switch (imsg.hdr.type) {
2002 case GOT_IMSG_GITCONFIG_REMOTE:
2003 remote = &(*remotes)[*nremotes];
2004 if (datalen < sizeof(iremote)) {
2005 err = got_error(GOT_ERR_PRIVSEP_LEN);
2006 break;
2008 memcpy(&iremote, imsg.data, sizeof(iremote));
2009 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2010 (sizeof(iremote) + iremote.name_len +
2011 iremote.url_len) > datalen) {
2012 err = got_error(GOT_ERR_PRIVSEP_LEN);
2013 break;
2015 remote->name = strndup(imsg.data + sizeof(iremote),
2016 iremote.name_len);
2017 if (remote->name == NULL) {
2018 err = got_error_from_errno("strndup");
2019 break;
2021 remote->url = strndup(imsg.data + sizeof(iremote) +
2022 iremote.name_len, iremote.url_len);
2023 if (remote->url == NULL) {
2024 err = got_error_from_errno("strndup");
2025 free(remote->name);
2026 break;
2028 (*nremotes)++;
2029 break;
2030 default:
2031 err = got_error(GOT_ERR_PRIVSEP_MSG);
2032 break;
2035 imsg_free(&imsg);
2036 if (err)
2037 break;
2040 if (err) {
2041 int i;
2042 for (i = 0; i < *nremotes; i++) {
2043 free((*remotes)[i].name);
2044 free((*remotes)[i].url);
2046 free(*remotes);
2047 *remotes = NULL;
2048 *nremotes = 0;
2050 return err;
2053 const struct got_error *
2054 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2055 struct got_object_id *id, int idx, const char *path)
2057 const struct got_error *err = NULL;
2058 struct ibuf *wbuf;
2059 size_t path_len = strlen(path) + 1;
2061 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2062 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2063 if (wbuf == NULL)
2064 return got_error_from_errno(
2065 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2066 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2067 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2068 ibuf_free(wbuf);
2069 return err;
2071 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2072 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2073 ibuf_free(wbuf);
2074 return err;
2076 if (imsg_add(wbuf, path, path_len) == -1) {
2077 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2078 ibuf_free(wbuf);
2079 return err;
2082 wbuf->fd = -1;
2083 imsg_close(ibuf, wbuf);
2085 return flush_imsg(ibuf);
2088 const struct got_error *
2089 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2090 size_t ncommits, struct imsgbuf *ibuf)
2092 const struct got_error *err;
2093 struct ibuf *wbuf;
2094 int i;
2096 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2097 sizeof(struct got_imsg_traversed_commits) +
2098 ncommits * SHA1_DIGEST_LENGTH);
2099 if (wbuf == NULL)
2100 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2102 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2103 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2104 ibuf_free(wbuf);
2105 return err;
2107 for (i = 0; i < ncommits; i++) {
2108 struct got_object_id *id = &commit_ids[i];
2109 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2110 err = got_error_from_errno(
2111 "imsg_add TRAVERSED_COMMITS");
2112 ibuf_free(wbuf);
2113 return err;
2117 wbuf->fd = -1;
2118 imsg_close(ibuf, wbuf);
2120 return flush_imsg(ibuf);
2123 const struct got_error *
2124 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2125 struct got_object_id **changed_commit_id,
2126 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2128 const struct got_error *err = NULL;
2129 struct imsg imsg;
2130 struct got_imsg_traversed_commits *icommits;
2131 size_t datalen;
2132 int i, done = 0;
2134 *changed_commit = NULL;
2135 *changed_commit_id = NULL;
2137 while (!done) {
2138 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2139 if (err)
2140 return err;
2142 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2143 switch (imsg.hdr.type) {
2144 case GOT_IMSG_TRAVERSED_COMMITS:
2145 icommits = imsg.data;
2146 if (datalen != sizeof(*icommits) +
2147 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2148 err = got_error(GOT_ERR_PRIVSEP_LEN);
2149 break;
2151 for (i = 0; i < icommits->ncommits; i++) {
2152 struct got_object_qid *qid;
2153 uint8_t *sha1 = (uint8_t *)imsg.data +
2154 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2155 err = got_object_qid_alloc_partial(&qid);
2156 if (err)
2157 break;
2158 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2159 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2161 /* The last commit may contain a change. */
2162 if (i == icommits->ncommits - 1) {
2163 *changed_commit_id =
2164 got_object_id_dup(qid->id);
2165 if (*changed_commit_id == NULL) {
2166 err = got_error_from_errno(
2167 "got_object_id_dup");
2168 break;
2172 break;
2173 case GOT_IMSG_COMMIT:
2174 if (*changed_commit_id == NULL) {
2175 err = got_error(GOT_ERR_PRIVSEP_MSG);
2176 break;
2178 err = get_commit_from_imsg(changed_commit, &imsg,
2179 datalen, ibuf);
2180 break;
2181 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2182 done = 1;
2183 break;
2184 default:
2185 err = got_error(GOT_ERR_PRIVSEP_MSG);
2186 break;
2189 imsg_free(&imsg);
2190 if (err)
2191 break;
2194 if (err)
2195 got_object_id_queue_free(commit_ids);
2196 return err;
2199 const struct got_error *
2200 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2202 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2203 NULL, 0) == -1)
2204 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2206 return flush_imsg(ibuf);
2209 const struct got_error *
2210 got_privsep_unveil_exec_helpers(void)
2212 const char *helpers[] = {
2213 GOT_PATH_PROG_READ_PACK,
2214 GOT_PATH_PROG_READ_OBJECT,
2215 GOT_PATH_PROG_READ_COMMIT,
2216 GOT_PATH_PROG_READ_TREE,
2217 GOT_PATH_PROG_READ_BLOB,
2218 GOT_PATH_PROG_READ_TAG,
2219 GOT_PATH_PROG_READ_GITCONFIG,
2220 GOT_PATH_PROG_FETCH_PACK,
2221 GOT_PATH_PROG_INDEX_PACK,
2223 int i;
2225 for (i = 0; i < nitems(helpers); i++) {
2226 if (unveil(helpers[i], "x") == 0)
2227 continue;
2228 return got_error_from_errno2("unveil", helpers[i]);
2231 return NULL;
2234 void
2235 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2237 if (close(imsg_fds[0]) != 0) {
2238 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2239 _exit(1);
2242 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2243 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2244 _exit(1);
2246 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2247 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2248 _exit(1);
2251 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2252 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2253 strerror(errno));
2254 _exit(1);