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;
641 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
642 if (err)
643 return err;
645 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
646 switch (imsg.hdr.type) {
647 case GOT_IMSG_ERROR:
648 if (datalen < sizeof(struct got_imsg_error)) {
649 err = got_error(GOT_ERR_PRIVSEP_LEN);
650 break;
652 err = recv_imsg_error(&imsg, datalen);
653 break;
654 case GOT_IMSG_FETCH_SYMREFS:
655 if (datalen < sizeof(*isymrefs)) {
656 err = got_error(GOT_ERR_PRIVSEP_LEN);
657 break;
659 if (isymrefs != NULL) {
660 err = got_error(GOT_ERR_PRIVSEP_MSG);
661 break;
663 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
664 off = sizeof(*isymrefs);
665 remain = datalen - off;
666 for (n = 0; n < isymrefs->nsymrefs; n++) {
667 struct got_imsg_fetch_symref *s;
668 char *name, *target;
669 if (remain < sizeof(struct got_imsg_fetch_symref)) {
670 err = got_error(GOT_ERR_PRIVSEP_LEN);
671 goto done;
673 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
674 off += sizeof(*s);
675 remain -= sizeof(*s);
676 if (remain < s->name_len) {
677 err = got_error(GOT_ERR_PRIVSEP_LEN);
678 goto done;
680 name = strndup(imsg.data + off, s->name_len);
681 if (name == NULL) {
682 err = got_error_from_errno("strndup");
683 goto done;
685 off += s->name_len;
686 remain -= s->name_len;
687 if (remain < s->target_len) {
688 err = got_error(GOT_ERR_PRIVSEP_LEN);
689 free(name);
690 goto done;
692 target = strndup(imsg.data + off, s->target_len);
693 if (target == NULL) {
694 err = got_error_from_errno("strndup");
695 free(name);
696 goto done;
698 off += s->target_len;
699 remain -= s->target_len;
700 err = got_pathlist_append(symrefs, name, target);
701 if (err) {
702 free(name);
703 free(target);
704 goto done;
707 break;
708 case GOT_IMSG_FETCH_REF:
709 if (datalen <= SHA1_DIGEST_LENGTH) {
710 err = got_error(GOT_ERR_PRIVSEP_MSG);
711 break;
713 *id = malloc(sizeof(**id));
714 if (*id == NULL) {
715 err = got_error_from_errno("malloc");
716 break;
718 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
719 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
720 datalen - SHA1_DIGEST_LENGTH);
721 if (*refname == NULL) {
722 err = got_error_from_errno("strndup");
723 break;
725 break;
726 case GOT_IMSG_FETCH_SERVER_PROGRESS:
727 if (datalen == 0) {
728 err = got_error(GOT_ERR_PRIVSEP_LEN);
729 break;
731 *server_progress = strndup(imsg.data, datalen);
732 if (*server_progress == NULL) {
733 err = got_error_from_errno("strndup");
734 break;
736 for (i = 0; i < datalen; i++) {
737 if (!isprint((unsigned char)(*server_progress)[i]) &&
738 !isspace((unsigned char)(*server_progress)[i])) {
739 err = got_error(GOT_ERR_PRIVSEP_MSG);
740 free(*server_progress);
741 *server_progress = NULL;
742 goto done;
745 break;
746 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
747 if (datalen < sizeof(*packfile_size)) {
748 err = got_error(GOT_ERR_PRIVSEP_MSG);
749 break;
751 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
752 break;
753 case GOT_IMSG_FETCH_DONE:
754 *id = malloc(sizeof(**id));
755 if (*id == NULL) {
756 err = got_error_from_errno("malloc");
757 break;
759 if (datalen != SHA1_DIGEST_LENGTH) {
760 err = got_error(GOT_ERR_PRIVSEP_MSG);
761 break;
763 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
764 *done = 1;
765 break;
766 default:
767 err = got_error(GOT_ERR_PRIVSEP_MSG);
768 break;
770 done:
771 if (err) {
772 free(*id);
773 *id = NULL;
774 free(*refname);
775 *refname = NULL;
777 imsg_free(&imsg);
778 return err;
781 const struct got_error *
782 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
783 int fd)
785 const struct got_error *err = NULL;
787 /* Keep in sync with struct got_imsg_index_pack_request */
788 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
789 pack_hash, SHA1_DIGEST_LENGTH) == -1) {
790 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
791 close(fd);
792 return err;
794 return flush_imsg(ibuf);
797 const struct got_error *
798 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
800 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
803 const struct got_error *
804 got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
805 int nobj_indexed, int nobj_loose, int nobj_resolved)
807 struct got_imsg_index_pack_progress iprogress;
809 iprogress.nobj_total = nobj_total;
810 iprogress.nobj_indexed = nobj_indexed;
811 iprogress.nobj_loose = nobj_loose;
812 iprogress.nobj_resolved = nobj_resolved;
814 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
815 &iprogress, sizeof(iprogress)) == -1)
816 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
818 return flush_imsg(ibuf);
821 const struct got_error *
822 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
824 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
825 return got_error_from_errno("imsg_compose FETCH");
826 return flush_imsg(ibuf);
829 const struct got_error *
830 got_privsep_recv_index_progress(int *done, int *nobj_total,
831 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
832 struct imsgbuf *ibuf)
834 const struct got_error *err = NULL;
835 struct imsg imsg;
836 struct got_imsg_index_pack_progress *iprogress;
837 size_t datalen;
839 *done = 0;
840 *nobj_total = 0;
841 *nobj_indexed = 0;
842 *nobj_resolved = 0;
844 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
845 if (err)
846 return err;
848 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
849 switch (imsg.hdr.type) {
850 case GOT_IMSG_ERROR:
851 if (datalen < sizeof(struct got_imsg_error)) {
852 err = got_error(GOT_ERR_PRIVSEP_LEN);
853 break;
855 err = recv_imsg_error(&imsg, datalen);
856 break;
857 case GOT_IMSG_IDXPACK_PROGRESS:
858 if (datalen < sizeof(*iprogress)) {
859 err = got_error(GOT_ERR_PRIVSEP_LEN);
860 break;
862 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
863 *nobj_total = iprogress->nobj_total;
864 *nobj_indexed = iprogress->nobj_indexed;
865 *nobj_loose = iprogress->nobj_loose;
866 *nobj_resolved = iprogress->nobj_resolved;
867 break;
868 case GOT_IMSG_IDXPACK_DONE:
869 if (datalen != 0) {
870 err = got_error(GOT_ERR_PRIVSEP_LEN);
871 break;
873 *done = 1;
874 break;
875 default:
876 err = got_error(GOT_ERR_PRIVSEP_MSG);
877 break;
880 imsg_free(&imsg);
881 return err;
884 const struct got_error *
885 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
886 struct imsgbuf *ibuf)
888 const struct got_error *err = NULL;
889 struct got_imsg_object *iobj;
890 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
892 if (datalen != sizeof(*iobj))
893 return got_error(GOT_ERR_PRIVSEP_LEN);
894 iobj = imsg->data;
896 *obj = calloc(1, sizeof(**obj));
897 if (*obj == NULL)
898 return got_error_from_errno("calloc");
900 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
901 (*obj)->type = iobj->type;
902 (*obj)->flags = iobj->flags;
903 (*obj)->hdrlen = iobj->hdrlen;
904 (*obj)->size = iobj->size;
905 /* path_packfile is handled by caller */
906 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
907 (*obj)->pack_offset = iobj->pack_offset;
908 (*obj)->pack_idx = iobj->pack_idx;
911 return err;
914 const struct got_error *
915 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
917 const struct got_error *err = NULL;
918 struct imsg imsg;
919 const size_t min_datalen =
920 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
922 *obj = NULL;
924 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
925 if (err)
926 return err;
928 switch (imsg.hdr.type) {
929 case GOT_IMSG_OBJECT:
930 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
931 break;
932 default:
933 err = got_error(GOT_ERR_PRIVSEP_MSG);
934 break;
937 imsg_free(&imsg);
939 return err;
942 static const struct got_error *
943 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
944 size_t logmsg_len)
946 const struct got_error *err = NULL;
947 size_t offset, remain;
949 offset = 0;
950 remain = logmsg_len;
951 while (remain > 0) {
952 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
954 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
955 commit->logmsg + offset, n) == -1) {
956 err = got_error_from_errno("imsg_compose "
957 "COMMIT_LOGMSG");
958 break;
961 err = flush_imsg(ibuf);
962 if (err)
963 break;
965 offset += n;
966 remain -= n;
969 return err;
972 const struct got_error *
973 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
975 const struct got_error *err = NULL;
976 struct got_imsg_commit_object *icommit;
977 uint8_t *buf;
978 size_t len, total;
979 struct got_object_qid *qid;
980 size_t author_len = strlen(commit->author);
981 size_t committer_len = strlen(commit->committer);
982 size_t logmsg_len = strlen(commit->logmsg);
984 total = sizeof(*icommit) + author_len + committer_len +
985 commit->nparents * SHA1_DIGEST_LENGTH;
987 buf = malloc(total);
988 if (buf == NULL)
989 return got_error_from_errno("malloc");
991 icommit = (struct got_imsg_commit_object *)buf;
992 memcpy(icommit->tree_id, commit->tree_id->sha1,
993 sizeof(icommit->tree_id));
994 icommit->author_len = author_len;
995 icommit->author_time = commit->author_time;
996 icommit->author_gmtoff = commit->author_gmtoff;
997 icommit->committer_len = committer_len;
998 icommit->committer_time = commit->committer_time;
999 icommit->committer_gmtoff = commit->committer_gmtoff;
1000 icommit->logmsg_len = logmsg_len;
1001 icommit->nparents = commit->nparents;
1003 len = sizeof(*icommit);
1004 memcpy(buf + len, commit->author, author_len);
1005 len += author_len;
1006 memcpy(buf + len, commit->committer, committer_len);
1007 len += committer_len;
1008 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
1009 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
1010 len += SHA1_DIGEST_LENGTH;
1013 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1014 err = got_error_from_errno("imsg_compose COMMIT");
1015 goto done;
1018 if (logmsg_len == 0 ||
1019 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1020 err = flush_imsg(ibuf);
1021 if (err)
1022 goto done;
1024 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1025 done:
1026 free(buf);
1027 return err;
1030 static const struct got_error *
1031 get_commit_from_imsg(struct got_commit_object **commit,
1032 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1034 const struct got_error *err = NULL;
1035 struct got_imsg_commit_object *icommit;
1036 size_t len = 0;
1037 int i;
1039 if (datalen < sizeof(*icommit))
1040 return got_error(GOT_ERR_PRIVSEP_LEN);
1042 icommit = imsg->data;
1043 if (datalen != sizeof(*icommit) + icommit->author_len +
1044 icommit->committer_len +
1045 icommit->nparents * SHA1_DIGEST_LENGTH)
1046 return got_error(GOT_ERR_PRIVSEP_LEN);
1048 if (icommit->nparents < 0)
1049 return got_error(GOT_ERR_PRIVSEP_LEN);
1051 len += sizeof(*icommit);
1053 *commit = got_object_commit_alloc_partial();
1054 if (*commit == NULL)
1055 return got_error_from_errno(
1056 "got_object_commit_alloc_partial");
1058 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1059 SHA1_DIGEST_LENGTH);
1060 (*commit)->author_time = icommit->author_time;
1061 (*commit)->author_gmtoff = icommit->author_gmtoff;
1062 (*commit)->committer_time = icommit->committer_time;
1063 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1065 if (icommit->author_len == 0) {
1066 (*commit)->author = strdup("");
1067 if ((*commit)->author == NULL) {
1068 err = got_error_from_errno("strdup");
1069 goto done;
1071 } else {
1072 (*commit)->author = malloc(icommit->author_len + 1);
1073 if ((*commit)->author == NULL) {
1074 err = got_error_from_errno("malloc");
1075 goto done;
1077 memcpy((*commit)->author, imsg->data + len,
1078 icommit->author_len);
1079 (*commit)->author[icommit->author_len] = '\0';
1081 len += icommit->author_len;
1083 if (icommit->committer_len == 0) {
1084 (*commit)->committer = strdup("");
1085 if ((*commit)->committer == NULL) {
1086 err = got_error_from_errno("strdup");
1087 goto done;
1089 } else {
1090 (*commit)->committer =
1091 malloc(icommit->committer_len + 1);
1092 if ((*commit)->committer == NULL) {
1093 err = got_error_from_errno("malloc");
1094 goto done;
1096 memcpy((*commit)->committer, imsg->data + len,
1097 icommit->committer_len);
1098 (*commit)->committer[icommit->committer_len] = '\0';
1100 len += icommit->committer_len;
1102 if (icommit->logmsg_len == 0) {
1103 (*commit)->logmsg = strdup("");
1104 if ((*commit)->logmsg == NULL) {
1105 err = got_error_from_errno("strdup");
1106 goto done;
1108 } else {
1109 size_t offset = 0, remain = icommit->logmsg_len;
1111 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1112 if ((*commit)->logmsg == NULL) {
1113 err = got_error_from_errno("malloc");
1114 goto done;
1116 while (remain > 0) {
1117 struct imsg imsg_log;
1118 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1119 remain);
1121 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1122 if (err)
1123 goto done;
1125 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1126 err = got_error(GOT_ERR_PRIVSEP_MSG);
1127 goto done;
1130 memcpy((*commit)->logmsg + offset,
1131 imsg_log.data, n);
1132 imsg_free(&imsg_log);
1133 offset += n;
1134 remain -= n;
1136 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1139 for (i = 0; i < icommit->nparents; i++) {
1140 struct got_object_qid *qid;
1142 err = got_object_qid_alloc_partial(&qid);
1143 if (err)
1144 break;
1145 memcpy(qid->id, imsg->data + len +
1146 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1147 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1148 (*commit)->nparents++;
1150 done:
1151 if (err) {
1152 got_object_commit_close(*commit);
1153 *commit = NULL;
1155 return err;
1158 const struct got_error *
1159 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1161 const struct got_error *err = NULL;
1162 struct imsg imsg;
1163 size_t datalen;
1164 const size_t min_datalen =
1165 MIN(sizeof(struct got_imsg_error),
1166 sizeof(struct got_imsg_commit_object));
1168 *commit = NULL;
1170 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1171 if (err)
1172 return err;
1174 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1176 switch (imsg.hdr.type) {
1177 case GOT_IMSG_COMMIT:
1178 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1179 break;
1180 default:
1181 err = got_error(GOT_ERR_PRIVSEP_MSG);
1182 break;
1185 imsg_free(&imsg);
1187 return err;
1190 const struct got_error *
1191 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1192 int nentries)
1194 const struct got_error *err = NULL;
1195 struct got_imsg_tree_object itree;
1196 struct got_pathlist_entry *pe;
1197 size_t totlen;
1198 int nimsg; /* number of imsg queued in ibuf */
1200 itree.nentries = nentries;
1201 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1202 == -1)
1203 return got_error_from_errno("imsg_compose TREE");
1205 totlen = sizeof(itree);
1206 nimsg = 1;
1207 TAILQ_FOREACH(pe, entries, entry) {
1208 const char *name = pe->path;
1209 struct got_parsed_tree_entry *pte = pe->data;
1210 struct ibuf *wbuf;
1211 size_t namelen = strlen(name);
1212 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1214 if (len > MAX_IMSGSIZE)
1215 return got_error(GOT_ERR_NO_SPACE);
1217 nimsg++;
1218 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1219 err = flush_imsg(ibuf);
1220 if (err)
1221 return err;
1222 nimsg = 0;
1225 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1226 if (wbuf == NULL)
1227 return got_error_from_errno("imsg_create TREE_ENTRY");
1229 /* Keep in sync with struct got_imsg_tree_object definition! */
1230 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1231 err = got_error_from_errno("imsg_add TREE_ENTRY");
1232 ibuf_free(wbuf);
1233 return err;
1235 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1236 err = got_error_from_errno("imsg_add TREE_ENTRY");
1237 ibuf_free(wbuf);
1238 return err;
1241 if (imsg_add(wbuf, name, namelen) == -1) {
1242 err = got_error_from_errno("imsg_add TREE_ENTRY");
1243 ibuf_free(wbuf);
1244 return err;
1247 wbuf->fd = -1;
1248 imsg_close(ibuf, wbuf);
1250 totlen += len;
1253 return flush_imsg(ibuf);
1256 const struct got_error *
1257 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1259 const struct got_error *err = NULL;
1260 const size_t min_datalen =
1261 MIN(sizeof(struct got_imsg_error),
1262 sizeof(struct got_imsg_tree_object));
1263 struct got_imsg_tree_object *itree;
1264 int nentries = 0;
1266 *tree = NULL;
1267 get_more:
1268 err = read_imsg(ibuf);
1269 if (err)
1270 goto done;
1272 for (;;) {
1273 struct imsg imsg;
1274 size_t n;
1275 size_t datalen;
1276 struct got_imsg_tree_entry *ite;
1277 struct got_tree_entry *te = NULL;
1279 n = imsg_get(ibuf, &imsg);
1280 if (n == 0) {
1281 if (*tree && (*tree)->nentries != nentries)
1282 goto get_more;
1283 break;
1286 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1287 return got_error(GOT_ERR_PRIVSEP_LEN);
1289 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1291 switch (imsg.hdr.type) {
1292 case GOT_IMSG_ERROR:
1293 err = recv_imsg_error(&imsg, datalen);
1294 break;
1295 case GOT_IMSG_TREE:
1296 /* This message should only appear once. */
1297 if (*tree != NULL) {
1298 err = got_error(GOT_ERR_PRIVSEP_MSG);
1299 break;
1301 if (datalen != sizeof(*itree)) {
1302 err = got_error(GOT_ERR_PRIVSEP_LEN);
1303 break;
1305 itree = imsg.data;
1306 *tree = malloc(sizeof(**tree));
1307 if (*tree == NULL) {
1308 err = got_error_from_errno("malloc");
1309 break;
1311 (*tree)->entries = calloc(itree->nentries,
1312 sizeof(struct got_tree_entry));
1313 if ((*tree)->entries == NULL) {
1314 err = got_error_from_errno("malloc");
1315 break;
1317 (*tree)->nentries = itree->nentries;
1318 (*tree)->refcnt = 0;
1319 break;
1320 case GOT_IMSG_TREE_ENTRY:
1321 /* This message should be preceeded by GOT_IMSG_TREE. */
1322 if (*tree == NULL) {
1323 err = got_error(GOT_ERR_PRIVSEP_MSG);
1324 break;
1326 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1327 err = got_error(GOT_ERR_PRIVSEP_LEN);
1328 break;
1331 /* Remaining data contains the entry's name. */
1332 datalen -= sizeof(*ite);
1333 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1334 err = got_error(GOT_ERR_PRIVSEP_LEN);
1335 break;
1337 ite = imsg.data;
1339 if (datalen + 1 > sizeof(te->name)) {
1340 err = got_error(GOT_ERR_NO_SPACE);
1341 break;
1343 te = &(*tree)->entries[nentries];
1344 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1345 te->name[datalen] = '\0';
1347 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1348 te->mode = ite->mode;
1349 te->idx = nentries;
1350 nentries++;
1351 break;
1352 default:
1353 err = got_error(GOT_ERR_PRIVSEP_MSG);
1354 break;
1357 imsg_free(&imsg);
1359 done:
1360 if (*tree && (*tree)->nentries != nentries) {
1361 if (err == NULL)
1362 err = got_error(GOT_ERR_PRIVSEP_LEN);
1363 got_object_tree_close(*tree);
1364 *tree = NULL;
1367 return err;
1370 const struct got_error *
1371 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1372 const uint8_t *data)
1374 struct got_imsg_blob iblob;
1376 iblob.size = size;
1377 iblob.hdrlen = hdrlen;
1379 if (data) {
1380 uint8_t *buf;
1382 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1383 return got_error(GOT_ERR_NO_SPACE);
1385 buf = malloc(sizeof(iblob) + size);
1386 if (buf == NULL)
1387 return got_error_from_errno("malloc");
1389 memcpy(buf, &iblob, sizeof(iblob));
1390 memcpy(buf + sizeof(iblob), data, size);
1391 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1392 sizeof(iblob) + size) == -1) {
1393 free(buf);
1394 return got_error_from_errno("imsg_compose BLOB");
1396 free(buf);
1397 } else {
1398 /* Data has already been written to file descriptor. */
1399 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1400 sizeof(iblob)) == -1)
1401 return got_error_from_errno("imsg_compose BLOB");
1405 return flush_imsg(ibuf);
1408 const struct got_error *
1409 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1410 struct imsgbuf *ibuf)
1412 const struct got_error *err = NULL;
1413 struct imsg imsg;
1414 struct got_imsg_blob *iblob;
1415 size_t datalen;
1417 *outbuf = NULL;
1419 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1420 if (err)
1421 return err;
1423 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1425 switch (imsg.hdr.type) {
1426 case GOT_IMSG_BLOB:
1427 if (datalen < sizeof(*iblob)) {
1428 err = got_error(GOT_ERR_PRIVSEP_LEN);
1429 break;
1431 iblob = imsg.data;
1432 *size = iblob->size;
1433 *hdrlen = iblob->hdrlen;
1435 if (datalen == sizeof(*iblob)) {
1436 /* Data has been written to file descriptor. */
1437 break;
1440 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1441 err = got_error(GOT_ERR_PRIVSEP_LEN);
1442 break;
1445 *outbuf = malloc(*size);
1446 if (*outbuf == NULL) {
1447 err = got_error_from_errno("malloc");
1448 break;
1450 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1451 break;
1452 default:
1453 err = got_error(GOT_ERR_PRIVSEP_MSG);
1454 break;
1457 imsg_free(&imsg);
1459 return err;
1462 static const struct got_error *
1463 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1465 const struct got_error *err = NULL;
1466 size_t offset, remain;
1468 offset = 0;
1469 remain = tagmsg_len;
1470 while (remain > 0) {
1471 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1473 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1474 tag->tagmsg + offset, n) == -1) {
1475 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1476 break;
1479 err = flush_imsg(ibuf);
1480 if (err)
1481 break;
1483 offset += n;
1484 remain -= n;
1487 return err;
1490 const struct got_error *
1491 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1493 const struct got_error *err = NULL;
1494 struct got_imsg_tag_object *itag;
1495 uint8_t *buf;
1496 size_t len, total;
1497 size_t tag_len = strlen(tag->tag);
1498 size_t tagger_len = strlen(tag->tagger);
1499 size_t tagmsg_len = strlen(tag->tagmsg);
1501 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1503 buf = malloc(total);
1504 if (buf == NULL)
1505 return got_error_from_errno("malloc");
1507 itag = (struct got_imsg_tag_object *)buf;
1508 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1509 itag->obj_type = tag->obj_type;
1510 itag->tag_len = tag_len;
1511 itag->tagger_len = tagger_len;
1512 itag->tagger_time = tag->tagger_time;
1513 itag->tagger_gmtoff = tag->tagger_gmtoff;
1514 itag->tagmsg_len = tagmsg_len;
1516 len = sizeof(*itag);
1517 memcpy(buf + len, tag->tag, tag_len);
1518 len += tag_len;
1519 memcpy(buf + len, tag->tagger, tagger_len);
1520 len += tagger_len;
1522 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1523 err = got_error_from_errno("imsg_compose TAG");
1524 goto done;
1527 if (tagmsg_len == 0 ||
1528 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1529 err = flush_imsg(ibuf);
1530 if (err)
1531 goto done;
1533 err = send_tagmsg(ibuf, tag, tagmsg_len);
1534 done:
1535 free(buf);
1536 return err;
1539 const struct got_error *
1540 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1542 const struct got_error *err = NULL;
1543 struct imsg imsg;
1544 struct got_imsg_tag_object *itag;
1545 size_t len, datalen;
1546 const size_t min_datalen =
1547 MIN(sizeof(struct got_imsg_error),
1548 sizeof(struct got_imsg_tag_object));
1550 *tag = NULL;
1552 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1553 if (err)
1554 return err;
1556 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1557 len = 0;
1559 switch (imsg.hdr.type) {
1560 case GOT_IMSG_TAG:
1561 if (datalen < sizeof(*itag)) {
1562 err = got_error(GOT_ERR_PRIVSEP_LEN);
1563 break;
1565 itag = imsg.data;
1566 if (datalen != sizeof(*itag) + itag->tag_len +
1567 itag->tagger_len) {
1568 err = got_error(GOT_ERR_PRIVSEP_LEN);
1569 break;
1571 len += sizeof(*itag);
1573 *tag = calloc(1, sizeof(**tag));
1574 if (*tag == NULL) {
1575 err = got_error_from_errno("calloc");
1576 break;
1579 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1581 if (itag->tag_len == 0) {
1582 (*tag)->tag = strdup("");
1583 if ((*tag)->tag == NULL) {
1584 err = got_error_from_errno("strdup");
1585 break;
1587 } else {
1588 (*tag)->tag = malloc(itag->tag_len + 1);
1589 if ((*tag)->tag == NULL) {
1590 err = got_error_from_errno("malloc");
1591 break;
1593 memcpy((*tag)->tag, imsg.data + len,
1594 itag->tag_len);
1595 (*tag)->tag[itag->tag_len] = '\0';
1597 len += itag->tag_len;
1599 (*tag)->obj_type = itag->obj_type;
1600 (*tag)->tagger_time = itag->tagger_time;
1601 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1603 if (itag->tagger_len == 0) {
1604 (*tag)->tagger = strdup("");
1605 if ((*tag)->tagger == NULL) {
1606 err = got_error_from_errno("strdup");
1607 break;
1609 } else {
1610 (*tag)->tagger = malloc(itag->tagger_len + 1);
1611 if ((*tag)->tagger == NULL) {
1612 err = got_error_from_errno("malloc");
1613 break;
1615 memcpy((*tag)->tagger, imsg.data + len,
1616 itag->tagger_len);
1617 (*tag)->tagger[itag->tagger_len] = '\0';
1619 len += itag->tagger_len;
1621 if (itag->tagmsg_len == 0) {
1622 (*tag)->tagmsg = strdup("");
1623 if ((*tag)->tagmsg == NULL) {
1624 err = got_error_from_errno("strdup");
1625 break;
1627 } else {
1628 size_t offset = 0, remain = itag->tagmsg_len;
1630 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1631 if ((*tag)->tagmsg == NULL) {
1632 err = got_error_from_errno("malloc");
1633 break;
1635 while (remain > 0) {
1636 struct imsg imsg_log;
1637 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1638 remain);
1640 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1641 if (err)
1642 return err;
1644 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1645 return got_error(GOT_ERR_PRIVSEP_MSG);
1647 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1648 n);
1649 imsg_free(&imsg_log);
1650 offset += n;
1651 remain -= n;
1653 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1656 break;
1657 default:
1658 err = got_error(GOT_ERR_PRIVSEP_MSG);
1659 break;
1662 imsg_free(&imsg);
1664 return err;
1667 const struct got_error *
1668 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1669 struct got_packidx *packidx)
1671 const struct got_error *err = NULL;
1672 struct got_imsg_packidx ipackidx;
1673 struct got_imsg_pack ipack;
1674 int fd;
1676 ipackidx.len = packidx->len;
1677 fd = dup(packidx->fd);
1678 if (fd == -1)
1679 return got_error_from_errno("dup");
1681 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1682 sizeof(ipackidx)) == -1) {
1683 err = got_error_from_errno("imsg_compose PACKIDX");
1684 close(fd);
1685 return err;
1688 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1689 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1690 return got_error(GOT_ERR_NO_SPACE);
1691 ipack.filesize = pack->filesize;
1693 fd = dup(pack->fd);
1694 if (fd == -1)
1695 return got_error_from_errno("dup");
1697 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1698 == -1) {
1699 err = got_error_from_errno("imsg_compose PACK");
1700 close(fd);
1701 return err;
1704 return flush_imsg(ibuf);
1707 const struct got_error *
1708 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1709 struct got_object_id *id)
1711 struct got_imsg_packed_object iobj;
1713 iobj.idx = idx;
1714 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1716 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1717 &iobj, sizeof(iobj)) == -1)
1718 return got_error_from_errno("imsg_compose "
1719 "PACKED_OBJECT_REQUEST");
1721 return flush_imsg(ibuf);
1724 const struct got_error *
1725 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1727 const struct got_error *err = NULL;
1729 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1730 NULL, 0) == -1) {
1731 err = got_error_from_errno("imsg_compose "
1732 "GITCONFIG_PARSE_REQUEST");
1733 close(fd);
1734 return err;
1737 return flush_imsg(ibuf);
1740 const struct got_error *
1741 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1743 if (imsg_compose(ibuf,
1744 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1745 NULL, 0) == -1)
1746 return got_error_from_errno("imsg_compose "
1747 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1749 return flush_imsg(ibuf);
1752 const struct got_error *
1753 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1755 if (imsg_compose(ibuf,
1756 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1757 return got_error_from_errno("imsg_compose "
1758 "GITCONFIG_AUTHOR_NAME_REQUEST");
1760 return flush_imsg(ibuf);
1763 const struct got_error *
1764 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1766 if (imsg_compose(ibuf,
1767 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1768 return got_error_from_errno("imsg_compose "
1769 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1771 return flush_imsg(ibuf);
1774 const struct got_error *
1775 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1777 if (imsg_compose(ibuf,
1778 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1779 return got_error_from_errno("imsg_compose "
1780 "GITCONFIG_REMOTE_REQUEST");
1782 return flush_imsg(ibuf);
1785 const struct got_error *
1786 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1788 if (imsg_compose(ibuf,
1789 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1790 return got_error_from_errno("imsg_compose "
1791 "GITCONFIG_OWNER_REQUEST");
1793 return flush_imsg(ibuf);
1796 const struct got_error *
1797 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1799 size_t len = value ? strlen(value) + 1 : 0;
1801 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1802 value, len) == -1)
1803 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1805 return flush_imsg(ibuf);
1808 const struct got_error *
1809 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1811 const struct got_error *err = NULL;
1812 struct imsg imsg;
1813 size_t datalen;
1814 const size_t min_datalen = 0;
1816 *str = NULL;
1818 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1819 if (err)
1820 return err;
1821 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1823 switch (imsg.hdr.type) {
1824 case GOT_IMSG_GITCONFIG_STR_VAL:
1825 if (datalen == 0)
1826 break;
1827 *str = malloc(datalen);
1828 if (*str == NULL) {
1829 err = got_error_from_errno("malloc");
1830 break;
1832 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1833 err = got_error(GOT_ERR_NO_SPACE);
1834 break;
1835 default:
1836 err = got_error(GOT_ERR_PRIVSEP_MSG);
1837 break;
1840 imsg_free(&imsg);
1841 return err;
1844 const struct got_error *
1845 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1847 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1848 &value, sizeof(value)) == -1)
1849 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1851 return flush_imsg(ibuf);
1854 const struct got_error *
1855 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1857 const struct got_error *err = NULL;
1858 struct imsg imsg;
1859 size_t datalen;
1860 const size_t min_datalen =
1861 MIN(sizeof(struct got_imsg_error), sizeof(int));
1863 *val = 0;
1865 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1866 if (err)
1867 return err;
1868 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1870 switch (imsg.hdr.type) {
1871 case GOT_IMSG_GITCONFIG_INT_VAL:
1872 if (datalen != sizeof(*val)) {
1873 err = got_error(GOT_ERR_PRIVSEP_LEN);
1874 break;
1876 memcpy(val, imsg.data, sizeof(*val));
1877 break;
1878 default:
1879 err = got_error(GOT_ERR_PRIVSEP_MSG);
1880 break;
1883 imsg_free(&imsg);
1884 return err;
1887 const struct got_error *
1888 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1889 struct got_remote_repo *remotes, int nremotes)
1891 const struct got_error *err = NULL;
1892 struct got_imsg_remotes iremotes;
1893 int i;
1895 iremotes.nremotes = nremotes;
1896 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1897 &iremotes, sizeof(iremotes)) == -1)
1898 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1900 err = flush_imsg(ibuf);
1901 imsg_clear(ibuf);
1902 if (err)
1903 return err;
1905 for (i = 0; i < nremotes; i++) {
1906 struct got_imsg_remote iremote;
1907 size_t len = sizeof(iremote);
1908 struct ibuf *wbuf;
1910 iremote.name_len = strlen(remotes[i].name);
1911 len += iremote.name_len;
1912 iremote.url_len = strlen(remotes[i].url);
1913 len += iremote.url_len;
1915 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1916 if (wbuf == NULL)
1917 return got_error_from_errno(
1918 "imsg_create GITCONFIG_REMOTE");
1920 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1921 err = got_error_from_errno(
1922 "imsg_add GIITCONFIG_REMOTE");
1923 ibuf_free(wbuf);
1924 return err;
1927 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1928 err = got_error_from_errno(
1929 "imsg_add GIITCONFIG_REMOTE");
1930 ibuf_free(wbuf);
1931 return err;
1933 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1934 err = got_error_from_errno(
1935 "imsg_add GIITCONFIG_REMOTE");
1936 ibuf_free(wbuf);
1937 return err;
1940 wbuf->fd = -1;
1941 imsg_close(ibuf, wbuf);
1942 err = flush_imsg(ibuf);
1943 if (err)
1944 return err;
1947 return NULL;
1950 const struct got_error *
1951 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1952 int *nremotes, struct imsgbuf *ibuf)
1954 const struct got_error *err = NULL;
1955 struct imsg imsg;
1956 size_t datalen;
1957 struct got_imsg_remotes iremotes;
1958 struct got_imsg_remote iremote;
1960 *remotes = NULL;
1961 *nremotes = 0;
1962 iremotes.nremotes = 0;
1964 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1965 if (err)
1966 return err;
1967 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1969 switch (imsg.hdr.type) {
1970 case GOT_IMSG_GITCONFIG_REMOTES:
1971 if (datalen != sizeof(iremotes)) {
1972 err = got_error(GOT_ERR_PRIVSEP_LEN);
1973 break;
1975 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1976 if (iremotes.nremotes == 0) {
1977 imsg_free(&imsg);
1978 return NULL;
1980 break;
1981 default:
1982 imsg_free(&imsg);
1983 return got_error(GOT_ERR_PRIVSEP_MSG);
1986 imsg_free(&imsg);
1988 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1989 if (*remotes == NULL)
1990 return got_error_from_errno("recallocarray");
1992 while (*nremotes < iremotes.nremotes) {
1993 struct got_remote_repo *remote;
1995 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1996 if (err)
1997 break;
1998 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2000 switch (imsg.hdr.type) {
2001 case GOT_IMSG_GITCONFIG_REMOTE:
2002 remote = &(*remotes)[*nremotes];
2003 if (datalen < sizeof(iremote)) {
2004 err = got_error(GOT_ERR_PRIVSEP_LEN);
2005 break;
2007 memcpy(&iremote, imsg.data, sizeof(iremote));
2008 if (iremote.name_len == 0 || iremote.url_len == 0 ||
2009 (sizeof(iremote) + iremote.name_len +
2010 iremote.url_len) > datalen) {
2011 err = got_error(GOT_ERR_PRIVSEP_LEN);
2012 break;
2014 remote->name = strndup(imsg.data + sizeof(iremote),
2015 iremote.name_len);
2016 if (remote->name == NULL) {
2017 err = got_error_from_errno("strndup");
2018 break;
2020 remote->url = strndup(imsg.data + sizeof(iremote) +
2021 iremote.name_len, iremote.url_len);
2022 if (remote->url == NULL) {
2023 err = got_error_from_errno("strndup");
2024 free(remote->name);
2025 break;
2027 (*nremotes)++;
2028 break;
2029 default:
2030 err = got_error(GOT_ERR_PRIVSEP_MSG);
2031 break;
2034 imsg_free(&imsg);
2035 if (err)
2036 break;
2039 if (err) {
2040 int i;
2041 for (i = 0; i < *nremotes; i++) {
2042 free((*remotes)[i].name);
2043 free((*remotes)[i].url);
2045 free(*remotes);
2046 *remotes = NULL;
2047 *nremotes = 0;
2049 return err;
2052 const struct got_error *
2053 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2054 struct got_object_id *id, int idx, const char *path)
2056 const struct got_error *err = NULL;
2057 struct ibuf *wbuf;
2058 size_t path_len = strlen(path) + 1;
2060 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2061 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2062 if (wbuf == NULL)
2063 return got_error_from_errno(
2064 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2065 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2066 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2067 ibuf_free(wbuf);
2068 return err;
2070 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
2071 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2072 ibuf_free(wbuf);
2073 return err;
2075 if (imsg_add(wbuf, path, path_len) == -1) {
2076 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
2077 ibuf_free(wbuf);
2078 return err;
2081 wbuf->fd = -1;
2082 imsg_close(ibuf, wbuf);
2084 return flush_imsg(ibuf);
2087 const struct got_error *
2088 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
2089 size_t ncommits, struct imsgbuf *ibuf)
2091 const struct got_error *err;
2092 struct ibuf *wbuf;
2093 int i;
2095 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2096 sizeof(struct got_imsg_traversed_commits) +
2097 ncommits * SHA1_DIGEST_LENGTH);
2098 if (wbuf == NULL)
2099 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2101 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2102 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2103 ibuf_free(wbuf);
2104 return err;
2106 for (i = 0; i < ncommits; i++) {
2107 struct got_object_id *id = &commit_ids[i];
2108 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2109 err = got_error_from_errno(
2110 "imsg_add TRAVERSED_COMMITS");
2111 ibuf_free(wbuf);
2112 return err;
2116 wbuf->fd = -1;
2117 imsg_close(ibuf, wbuf);
2119 return flush_imsg(ibuf);
2122 const struct got_error *
2123 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2124 struct got_object_id **changed_commit_id,
2125 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2127 const struct got_error *err = NULL;
2128 struct imsg imsg;
2129 struct got_imsg_traversed_commits *icommits;
2130 size_t datalen;
2131 int i, done = 0;
2133 *changed_commit = NULL;
2134 *changed_commit_id = NULL;
2136 while (!done) {
2137 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2138 if (err)
2139 return err;
2141 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2142 switch (imsg.hdr.type) {
2143 case GOT_IMSG_TRAVERSED_COMMITS:
2144 icommits = imsg.data;
2145 if (datalen != sizeof(*icommits) +
2146 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2147 err = got_error(GOT_ERR_PRIVSEP_LEN);
2148 break;
2150 for (i = 0; i < icommits->ncommits; i++) {
2151 struct got_object_qid *qid;
2152 uint8_t *sha1 = (uint8_t *)imsg.data +
2153 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2154 err = got_object_qid_alloc_partial(&qid);
2155 if (err)
2156 break;
2157 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2158 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2160 /* The last commit may contain a change. */
2161 if (i == icommits->ncommits - 1) {
2162 *changed_commit_id =
2163 got_object_id_dup(qid->id);
2164 if (*changed_commit_id == NULL) {
2165 err = got_error_from_errno(
2166 "got_object_id_dup");
2167 break;
2171 break;
2172 case GOT_IMSG_COMMIT:
2173 if (*changed_commit_id == NULL) {
2174 err = got_error(GOT_ERR_PRIVSEP_MSG);
2175 break;
2177 err = get_commit_from_imsg(changed_commit, &imsg,
2178 datalen, ibuf);
2179 break;
2180 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2181 done = 1;
2182 break;
2183 default:
2184 err = got_error(GOT_ERR_PRIVSEP_MSG);
2185 break;
2188 imsg_free(&imsg);
2189 if (err)
2190 break;
2193 if (err)
2194 got_object_id_queue_free(commit_ids);
2195 return err;
2198 const struct got_error *
2199 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2201 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2202 NULL, 0) == -1)
2203 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2205 return flush_imsg(ibuf);
2208 const struct got_error *
2209 got_privsep_unveil_exec_helpers(void)
2211 const char *helpers[] = {
2212 GOT_PATH_PROG_READ_PACK,
2213 GOT_PATH_PROG_READ_OBJECT,
2214 GOT_PATH_PROG_READ_COMMIT,
2215 GOT_PATH_PROG_READ_TREE,
2216 GOT_PATH_PROG_READ_BLOB,
2217 GOT_PATH_PROG_READ_TAG,
2218 GOT_PATH_PROG_READ_GITCONFIG,
2219 GOT_PATH_PROG_FETCH_PACK,
2220 GOT_PATH_PROG_INDEX_PACK,
2222 int i;
2224 for (i = 0; i < nitems(helpers); i++) {
2225 if (unveil(helpers[i], "x") == 0)
2226 continue;
2227 return got_error_from_errno2("unveil", helpers[i]);
2230 return NULL;
2233 void
2234 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2236 if (close(imsg_fds[0]) != 0) {
2237 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2238 _exit(1);
2241 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2242 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2243 _exit(1);
2245 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2246 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2247 _exit(1);
2250 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2251 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2252 strerror(errno));
2253 _exit(1);