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/wait.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdint.h>
31 #include <poll.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <time.h>
38 #include "got_object.h"
39 #include "got_error.h"
40 #include "got_path.h"
41 #include "got_repository.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_inflate.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
51 #ifndef MIN
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #endif
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static const struct got_error *
60 poll_fd(int fd, int events, int timeout)
61 {
62 struct pollfd pfd[1];
63 struct timespec ts;
64 sigset_t sigset;
65 int n;
67 pfd[0].fd = fd;
68 pfd[0].events = events;
70 ts.tv_sec = timeout;
71 ts.tv_nsec = 0;
73 if (sigemptyset(&sigset) == -1)
74 return got_error_from_errno("sigemptyset");
75 if (sigaddset(&sigset, SIGWINCH) == -1)
76 return got_error_from_errno("sigaddset");
78 n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
79 if (n == -1)
80 return got_error_from_errno("ppoll");
81 if (n == 0)
82 return got_error(GOT_ERR_TIMEOUT);
83 if (pfd[0].revents & (POLLERR | POLLNVAL))
84 return got_error_from_errno("poll error");
85 if (pfd[0].revents & (events | POLLHUP))
86 return NULL;
88 return got_error(GOT_ERR_INTERRUPT);
89 }
91 static const struct got_error *
92 read_imsg(struct imsgbuf *ibuf)
93 {
94 const struct got_error *err;
95 size_t n;
97 /*
98 * There is no imsg API function to tell us whether the
99 * read buffer still contains pending data :-(
100 */
101 if (ibuf->r.wpos < IMSG_HEADER_SIZE) {
102 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
103 if (err)
104 return err;
107 n = imsg_read(ibuf);
108 if (n == -1) {
109 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
110 return got_error(GOT_ERR_PRIVSEP_NO_FD);
111 return got_error(GOT_ERR_PRIVSEP_READ);
113 if (n == 0)
114 return got_error(GOT_ERR_PRIVSEP_PIPE);
116 return NULL;
119 const struct got_error *
120 got_privsep_wait_for_child(pid_t pid)
122 int child_status;
124 if (waitpid(pid, &child_status, 0) == -1)
125 return got_error_from_errno("waitpid");
127 if (!WIFEXITED(child_status))
128 return got_error(GOT_ERR_PRIVSEP_DIED);
130 if (WEXITSTATUS(child_status) != 0)
131 return got_error(GOT_ERR_PRIVSEP_EXIT);
133 return NULL;
136 static const struct got_error *
137 recv_imsg_error(struct imsg *imsg, size_t datalen)
139 struct got_imsg_error *ierr;
141 if (datalen != sizeof(*ierr))
142 return got_error(GOT_ERR_PRIVSEP_LEN);
144 ierr = imsg->data;
145 if (ierr->code == GOT_ERR_ERRNO) {
146 static struct got_error serr;
147 serr.code = GOT_ERR_ERRNO;
148 serr.msg = strerror(ierr->errno_code);
149 return &serr;
152 return got_error(ierr->code);
155 const struct got_error *
156 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
157 size_t min_datalen)
159 const struct got_error *err;
160 ssize_t n;
162 n = imsg_get(ibuf, imsg);
163 if (n == -1)
164 return got_error_from_errno("imsg_get");
166 while (n == 0) {
167 err = read_imsg(ibuf);
168 if (err)
169 return err;
170 n = imsg_get(ibuf, imsg);
171 if (n == -1)
172 return got_error_from_errno("imsg_get");
175 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
176 return got_error(GOT_ERR_PRIVSEP_LEN);
178 if (imsg->hdr.type == GOT_IMSG_ERROR) {
179 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
180 return recv_imsg_error(imsg, datalen);
183 return NULL;
186 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
187 void
188 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
190 const struct got_error *poll_err;
191 struct got_imsg_error ierr;
192 int ret;
194 ierr.code = err->code;
195 if (err->code == GOT_ERR_ERRNO)
196 ierr.errno_code = errno;
197 else
198 ierr.errno_code = 0;
199 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
200 if (ret == -1) {
201 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
202 getprogname(), err->code, err->msg, strerror(errno));
203 return;
206 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
207 if (poll_err) {
208 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
209 getprogname(), err->code, err->msg, poll_err->msg);
210 return;
213 ret = imsg_flush(ibuf);
214 if (ret == -1) {
215 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
216 getprogname(), err->code, err->msg, strerror(errno));
217 imsg_clear(ibuf);
218 return;
222 static const struct got_error *
223 flush_imsg(struct imsgbuf *ibuf)
225 const struct got_error *err;
227 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
228 if (err)
229 return err;
231 if (imsg_flush(ibuf) == -1) {
232 imsg_clear(ibuf);
233 return got_error_from_errno("imsg_flush");
236 return NULL;
239 const struct got_error *
240 got_privsep_flush_imsg(struct imsgbuf *ibuf)
242 return flush_imsg(ibuf);
245 const struct got_error *
246 got_privsep_send_stop(int fd)
248 const struct got_error *err = NULL;
249 struct imsgbuf ibuf;
251 imsg_init(&ibuf, fd);
253 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
254 return got_error_from_errno("imsg_compose STOP");
256 err = flush_imsg(&ibuf);
257 return err;
260 const struct got_error *
261 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd,
262 struct got_object_id *id)
264 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd,
265 id, sizeof(*id)) == -1)
266 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
268 return flush_imsg(ibuf);
271 const struct got_error *
272 got_privsep_send_raw_obj_req(struct imsgbuf *ibuf, int fd,
273 struct got_object_id *id)
275 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_REQUEST, 0, 0, fd,
276 id, sizeof(*id)) == -1)
277 return got_error_from_errno("imsg_compose RAW_OBJECT_REQUEST");
279 return flush_imsg(ibuf);
282 const struct got_error *
283 got_privsep_send_raw_obj_outfd(struct imsgbuf *ibuf, int outfd)
285 const struct got_error *err = NULL;
287 if (imsg_compose(ibuf, GOT_IMSG_RAW_OBJECT_OUTFD, 0, 0, outfd, NULL, 0)
288 == -1) {
289 err = got_error_from_errno("imsg_compose RAW_OBJECT_OUTFD");
290 close(outfd);
291 return err;
294 return flush_imsg(ibuf);
297 const struct got_error *
298 got_privsep_send_raw_obj(struct imsgbuf *ibuf, off_t size, size_t hdrlen,
299 uint8_t *data)
301 const struct got_error *err = NULL;
302 struct got_imsg_raw_obj iobj;
303 size_t len = sizeof(iobj);
304 struct ibuf *wbuf;
306 iobj.hdrlen = hdrlen;
307 iobj.size = size;
309 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
310 len += (size_t)size + hdrlen;
312 wbuf = imsg_create(ibuf, GOT_IMSG_RAW_OBJECT, 0, 0, len);
313 if (wbuf == NULL) {
314 err = got_error_from_errno("imsg_create RAW_OBJECT");
315 return err;
318 if (imsg_add(wbuf, &iobj, sizeof(iobj)) == -1)
319 return got_error_from_errno("imsg_add RAW_OBJECT");
321 if (data && size + hdrlen <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
322 if (imsg_add(wbuf, data, size + hdrlen) == -1)
323 return got_error_from_errno("imsg_add RAW_OBJECT");
326 wbuf->fd = -1;
327 imsg_close(ibuf, wbuf);
329 return flush_imsg(ibuf);
332 const struct got_error *
333 got_privsep_recv_raw_obj(uint8_t **outbuf, off_t *size, size_t *hdrlen,
334 struct imsgbuf *ibuf)
336 const struct got_error *err = NULL;
337 struct imsg imsg;
338 struct got_imsg_raw_obj *iobj;
339 size_t datalen;
341 *outbuf = NULL;
343 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
344 if (err)
345 return err;
347 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
349 switch (imsg.hdr.type) {
350 case GOT_IMSG_RAW_OBJECT:
351 if (datalen < sizeof(*iobj)) {
352 err = got_error(GOT_ERR_PRIVSEP_LEN);
353 break;
355 iobj = imsg.data;
356 *size = iobj->size;
357 *hdrlen = iobj->hdrlen;
359 if (datalen == sizeof(*iobj)) {
360 /* Data has been written to file descriptor. */
361 break;
364 if (*size + *hdrlen > GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX) {
365 err = got_error(GOT_ERR_PRIVSEP_LEN);
366 break;
369 *outbuf = malloc(*size + *hdrlen);
370 if (*outbuf == NULL) {
371 err = got_error_from_errno("malloc");
372 break;
374 memcpy(*outbuf, imsg.data + sizeof(*iobj), *size + *hdrlen);
375 break;
376 default:
377 err = got_error(GOT_ERR_PRIVSEP_MSG);
378 break;
381 imsg_free(&imsg);
383 return err;
386 const struct got_error *
387 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
388 struct got_object_id *id, int pack_idx)
390 const struct got_error *err = NULL;
391 struct got_imsg_packed_object iobj;
392 void *data;
393 size_t len;
395 if (pack_idx != -1) { /* commit is packed */
396 iobj.idx = pack_idx;
397 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
398 data = &iobj;
399 len = sizeof(iobj);
400 } else {
401 data = id;
402 len = sizeof(*id);
405 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, data, len)
406 == -1) {
407 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
408 close(fd);
409 return err;
412 return flush_imsg(ibuf);
415 const struct got_error *
416 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
417 struct got_object_id *id, int pack_idx)
419 struct ibuf *wbuf;
420 size_t len;
422 if (pack_idx != -1)
423 len = sizeof(struct got_imsg_packed_object);
424 else
425 len = sizeof(*id);
427 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
428 if (wbuf == NULL)
429 return got_error_from_errno("imsg_create TREE_REQUEST");
431 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
432 return got_error_from_errno("imsg_add TREE_REQUEST");
434 if (pack_idx != -1) { /* tree is packed */
435 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1)
436 return got_error_from_errno("imsg_add TREE_REQUEST");
439 wbuf->fd = fd;
440 imsg_close(ibuf, wbuf);
442 return flush_imsg(ibuf);
445 const struct got_error *
446 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
447 struct got_object_id *id, int pack_idx)
449 struct got_imsg_packed_object iobj;
450 void *data;
451 size_t len;
453 if (pack_idx != -1) { /* tag is packed */
454 iobj.idx = pack_idx;
455 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
456 data = &iobj;
457 len = sizeof(iobj);
458 } else {
459 data = id;
460 len = sizeof(*id);
463 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, data, len)
464 == -1)
465 return got_error_from_errno("imsg_compose TAG_REQUEST");
467 return flush_imsg(ibuf);
470 const struct got_error *
471 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
472 struct got_object_id *id, int pack_idx)
474 const struct got_error *err = NULL;
475 struct got_imsg_packed_object iobj;
476 void *data;
477 size_t len;
479 if (pack_idx != -1) { /* blob is packed */
480 iobj.idx = pack_idx;
481 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
482 data = &iobj;
483 len = sizeof(iobj);
484 } else {
485 data = id;
486 len = sizeof(*id);
489 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, data, len)
490 == -1) {
491 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
492 close(infd);
493 return err;
496 return flush_imsg(ibuf);
499 const struct got_error *
500 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
502 const struct got_error *err = NULL;
504 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
505 == -1) {
506 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
507 close(outfd);
508 return err;
511 return flush_imsg(ibuf);
514 static const struct got_error *
515 send_fd(struct imsgbuf *ibuf, int imsg_code, int fd)
517 const struct got_error *err = NULL;
519 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, NULL, 0) == -1) {
520 err = got_error_from_errno("imsg_compose TMPFD");
521 close(fd);
522 return err;
525 return flush_imsg(ibuf);
528 const struct got_error *
529 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
531 return send_fd(ibuf, GOT_IMSG_TMPFD, fd);
534 const struct got_error *
535 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
537 struct got_imsg_object iobj;
539 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
540 iobj.type = obj->type;
541 iobj.flags = obj->flags;
542 iobj.hdrlen = obj->hdrlen;
543 iobj.size = obj->size;
544 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
545 iobj.pack_offset = obj->pack_offset;
546 iobj.pack_idx = obj->pack_idx;
549 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
550 == -1)
551 return got_error_from_errno("imsg_compose OBJECT");
553 return flush_imsg(ibuf);
556 const struct got_error *
557 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
558 struct got_pathlist_head *have_refs, int fetch_all_branches,
559 struct got_pathlist_head *wanted_branches,
560 struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
562 const struct got_error *err = NULL;
563 struct ibuf *wbuf;
564 size_t len;
565 struct got_pathlist_entry *pe;
566 struct got_imsg_fetch_request fetchreq;
568 memset(&fetchreq, 0, sizeof(fetchreq));
569 fetchreq.fetch_all_branches = fetch_all_branches;
570 fetchreq.list_refs_only = list_refs_only;
571 fetchreq.verbosity = verbosity;
572 TAILQ_FOREACH(pe, have_refs, entry)
573 fetchreq.n_have_refs++;
574 TAILQ_FOREACH(pe, wanted_branches, entry)
575 fetchreq.n_wanted_branches++;
576 TAILQ_FOREACH(pe, wanted_refs, entry)
577 fetchreq.n_wanted_refs++;
578 len = sizeof(struct got_imsg_fetch_request);
579 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
580 close(fd);
581 return got_error(GOT_ERR_NO_SPACE);
584 if (imsg_compose(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, fd,
585 &fetchreq, sizeof(fetchreq)) == -1)
586 return got_error_from_errno(
587 "imsg_compose FETCH_SERVER_PROGRESS");
589 err = flush_imsg(ibuf);
590 if (err) {
591 close(fd);
592 return err;
594 fd = -1;
596 TAILQ_FOREACH(pe, have_refs, entry) {
597 const char *name = pe->path;
598 size_t name_len = pe->path_len;
599 struct got_object_id *id = pe->data;
601 len = sizeof(struct got_imsg_fetch_have_ref) + name_len;
602 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_HAVE_REF, 0, 0, len);
603 if (wbuf == NULL)
604 return got_error_from_errno("imsg_create FETCH_HAVE_REF");
606 /* Keep in sync with struct got_imsg_fetch_have_ref! */
607 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
608 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
609 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
610 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
611 if (imsg_add(wbuf, name, name_len) == -1)
612 return got_error_from_errno("imsg_add FETCH_HAVE_REF");
614 wbuf->fd = -1;
615 imsg_close(ibuf, wbuf);
616 err = flush_imsg(ibuf);
617 if (err)
618 return err;
621 TAILQ_FOREACH(pe, wanted_branches, entry) {
622 const char *name = pe->path;
623 size_t name_len = pe->path_len;
625 len = sizeof(struct got_imsg_fetch_wanted_branch) + name_len;
626 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_BRANCH, 0, 0,
627 len);
628 if (wbuf == NULL)
629 return got_error_from_errno(
630 "imsg_create FETCH_WANTED_BRANCH");
632 /* Keep in sync with struct got_imsg_fetch_wanted_branch! */
633 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
634 return got_error_from_errno(
635 "imsg_add FETCH_WANTED_BRANCH");
636 if (imsg_add(wbuf, name, name_len) == -1)
637 return got_error_from_errno(
638 "imsg_add FETCH_WANTED_BRANCH");
640 wbuf->fd = -1;
641 imsg_close(ibuf, wbuf);
642 err = flush_imsg(ibuf);
643 if (err)
644 return err;
647 TAILQ_FOREACH(pe, wanted_refs, entry) {
648 const char *name = pe->path;
649 size_t name_len = pe->path_len;
651 len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
652 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
653 len);
654 if (wbuf == NULL)
655 return got_error_from_errno(
656 "imsg_create FETCH_WANTED_REF");
658 /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
659 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
660 return got_error_from_errno(
661 "imsg_add FETCH_WANTED_REF");
662 if (imsg_add(wbuf, name, name_len) == -1)
663 return got_error_from_errno(
664 "imsg_add FETCH_WANTED_REF");
666 wbuf->fd = -1;
667 imsg_close(ibuf, wbuf);
668 err = flush_imsg(ibuf);
669 if (err)
670 return err;
674 return NULL;
678 const struct got_error *
679 got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int fd)
681 return send_fd(ibuf, GOT_IMSG_FETCH_OUTFD, fd);
684 const struct got_error *
685 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
686 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
687 off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
689 const struct got_error *err = NULL;
690 struct imsg imsg;
691 size_t datalen;
692 struct got_imsg_fetch_symrefs *isymrefs = NULL;
693 size_t n, remain;
694 off_t off;
695 int i;
697 *done = 0;
698 *id = NULL;
699 *refname = NULL;
700 *server_progress = NULL;
701 *packfile_size = 0;
702 memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
704 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
705 if (err)
706 return err;
708 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
709 switch (imsg.hdr.type) {
710 case GOT_IMSG_ERROR:
711 if (datalen < sizeof(struct got_imsg_error)) {
712 err = got_error(GOT_ERR_PRIVSEP_LEN);
713 break;
715 err = recv_imsg_error(&imsg, datalen);
716 break;
717 case GOT_IMSG_FETCH_SYMREFS:
718 if (datalen < sizeof(*isymrefs)) {
719 err = got_error(GOT_ERR_PRIVSEP_LEN);
720 break;
722 if (isymrefs != NULL) {
723 err = got_error(GOT_ERR_PRIVSEP_MSG);
724 break;
726 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
727 off = sizeof(*isymrefs);
728 remain = datalen - off;
729 for (n = 0; n < isymrefs->nsymrefs; n++) {
730 struct got_imsg_fetch_symref *s;
731 char *name, *target;
732 if (remain < sizeof(struct got_imsg_fetch_symref)) {
733 err = got_error(GOT_ERR_PRIVSEP_LEN);
734 goto done;
736 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
737 off += sizeof(*s);
738 remain -= sizeof(*s);
739 if (remain < s->name_len) {
740 err = got_error(GOT_ERR_PRIVSEP_LEN);
741 goto done;
743 name = strndup(imsg.data + off, s->name_len);
744 if (name == NULL) {
745 err = got_error_from_errno("strndup");
746 goto done;
748 off += s->name_len;
749 remain -= s->name_len;
750 if (remain < s->target_len) {
751 err = got_error(GOT_ERR_PRIVSEP_LEN);
752 free(name);
753 goto done;
755 target = strndup(imsg.data + off, s->target_len);
756 if (target == NULL) {
757 err = got_error_from_errno("strndup");
758 free(name);
759 goto done;
761 off += s->target_len;
762 remain -= s->target_len;
763 err = got_pathlist_append(symrefs, name, target);
764 if (err) {
765 free(name);
766 free(target);
767 goto done;
770 break;
771 case GOT_IMSG_FETCH_REF:
772 if (datalen <= SHA1_DIGEST_LENGTH) {
773 err = got_error(GOT_ERR_PRIVSEP_MSG);
774 break;
776 *id = malloc(sizeof(**id));
777 if (*id == NULL) {
778 err = got_error_from_errno("malloc");
779 break;
781 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
782 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
783 datalen - SHA1_DIGEST_LENGTH);
784 if (*refname == NULL) {
785 err = got_error_from_errno("strndup");
786 break;
788 break;
789 case GOT_IMSG_FETCH_SERVER_PROGRESS:
790 if (datalen == 0) {
791 err = got_error(GOT_ERR_PRIVSEP_LEN);
792 break;
794 *server_progress = strndup(imsg.data, datalen);
795 if (*server_progress == NULL) {
796 err = got_error_from_errno("strndup");
797 break;
799 for (i = 0; i < datalen; i++) {
800 if (!isprint((unsigned char)(*server_progress)[i]) &&
801 !isspace((unsigned char)(*server_progress)[i])) {
802 err = got_error(GOT_ERR_PRIVSEP_MSG);
803 free(*server_progress);
804 *server_progress = NULL;
805 goto done;
808 break;
809 case GOT_IMSG_FETCH_DOWNLOAD_PROGRESS:
810 if (datalen < sizeof(*packfile_size)) {
811 err = got_error(GOT_ERR_PRIVSEP_MSG);
812 break;
814 memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
815 break;
816 case GOT_IMSG_FETCH_DONE:
817 if (datalen != SHA1_DIGEST_LENGTH) {
818 err = got_error(GOT_ERR_PRIVSEP_MSG);
819 break;
821 memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
822 *done = 1;
823 break;
824 default:
825 err = got_error(GOT_ERR_PRIVSEP_MSG);
826 break;
828 done:
829 if (err) {
830 free(*id);
831 *id = NULL;
832 free(*refname);
833 *refname = NULL;
835 imsg_free(&imsg);
836 return err;
839 static const struct got_error *
840 send_send_ref(const char *name, size_t name_len, struct got_object_id *id,
841 int delete, struct imsgbuf *ibuf)
843 size_t len;
844 struct ibuf *wbuf;
846 len = sizeof(struct got_imsg_send_ref) + name_len;
847 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF, 0, 0, len);
848 if (wbuf == NULL)
849 return got_error_from_errno("imsg_create SEND_REF");
851 /* Keep in sync with struct got_imsg_send_ref! */
852 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1)
853 return got_error_from_errno("imsg_add SEND_REF");
854 if (imsg_add(wbuf, &delete, sizeof(delete)) == -1)
855 return got_error_from_errno("imsg_add SEND_REF");
856 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
857 return got_error_from_errno("imsg_add SEND_REF");
858 if (imsg_add(wbuf, name, name_len) == -1)
859 return got_error_from_errno("imsg_add SEND_REF");
861 wbuf->fd = -1;
862 imsg_close(ibuf, wbuf);
863 return flush_imsg(ibuf);
866 const struct got_error *
867 got_privsep_send_send_req(struct imsgbuf *ibuf, int fd,
868 struct got_pathlist_head *have_refs,
869 struct got_pathlist_head *delete_refs,
870 int verbosity)
872 const struct got_error *err = NULL;
873 struct got_pathlist_entry *pe;
874 struct got_imsg_send_request sendreq;
875 struct got_object_id zero_id;
877 memset(&zero_id, 0, sizeof(zero_id));
878 memset(&sendreq, 0, sizeof(sendreq));
879 sendreq.verbosity = verbosity;
880 TAILQ_FOREACH(pe, have_refs, entry)
881 sendreq.nrefs++;
882 TAILQ_FOREACH(pe, delete_refs, entry)
883 sendreq.nrefs++;
884 if (imsg_compose(ibuf, GOT_IMSG_SEND_REQUEST, 0, 0, fd,
885 &sendreq, sizeof(sendreq)) == -1) {
886 err = got_error_from_errno(
887 "imsg_compose FETCH_SERVER_PROGRESS");
888 goto done;
891 err = flush_imsg(ibuf);
892 if (err)
893 goto done;
894 fd = -1;
896 TAILQ_FOREACH(pe, have_refs, entry) {
897 const char *name = pe->path;
898 size_t name_len = pe->path_len;
899 struct got_object_id *id = pe->data;
900 err = send_send_ref(name, name_len, id, 0, ibuf);
901 if (err)
902 goto done;
905 TAILQ_FOREACH(pe, delete_refs, entry) {
906 const char *name = pe->path;
907 size_t name_len = pe->path_len;
908 err = send_send_ref(name, name_len, &zero_id, 1, ibuf);
909 if (err)
910 goto done;
912 done:
913 if (fd != -1 && close(fd) == -1 && err == NULL)
914 err = got_error_from_errno("close");
915 return err;
919 const struct got_error *
920 got_privsep_recv_send_remote_refs(struct got_pathlist_head *remote_refs,
921 struct imsgbuf *ibuf)
923 const struct got_error *err = NULL;
924 struct imsg imsg;
925 size_t datalen;
926 int done = 0;
927 struct got_imsg_send_remote_ref iremote_ref;
928 struct got_object_id *id = NULL;
929 char *refname = NULL;
930 struct got_pathlist_entry *new;
932 while (!done) {
933 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
934 if (err)
935 return err;
936 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
937 switch (imsg.hdr.type) {
938 case GOT_IMSG_ERROR:
939 if (datalen < sizeof(struct got_imsg_error)) {
940 err = got_error(GOT_ERR_PRIVSEP_LEN);
941 goto done;
943 err = recv_imsg_error(&imsg, datalen);
944 goto done;
945 case GOT_IMSG_SEND_REMOTE_REF:
946 if (datalen < sizeof(iremote_ref)) {
947 err = got_error(GOT_ERR_PRIVSEP_MSG);
948 goto done;
950 memcpy(&iremote_ref, imsg.data, sizeof(iremote_ref));
951 if (datalen != sizeof(iremote_ref) +
952 iremote_ref.name_len) {
953 err = got_error(GOT_ERR_PRIVSEP_MSG);
954 goto done;
956 id = malloc(sizeof(*id));
957 if (id == NULL) {
958 err = got_error_from_errno("malloc");
959 goto done;
961 memcpy(id->sha1, iremote_ref.id, SHA1_DIGEST_LENGTH);
962 refname = strndup(imsg.data + sizeof(iremote_ref),
963 datalen - sizeof(iremote_ref));
964 if (refname == NULL) {
965 err = got_error_from_errno("strndup");
966 goto done;
968 err = got_pathlist_insert(&new, remote_refs,
969 refname, id);
970 if (err)
971 goto done;
972 if (new == NULL) { /* duplicate which wasn't inserted */
973 free(id);
974 free(refname);
976 id = NULL;
977 refname = NULL;
978 break;
979 case GOT_IMSG_SEND_PACK_REQUEST:
980 if (datalen != 0) {
981 err = got_error(GOT_ERR_PRIVSEP_MSG);
982 goto done;
984 /* got-send-pack is now waiting for a pack file. */
985 done = 1;
986 break;
987 default:
988 err = got_error(GOT_ERR_PRIVSEP_MSG);
989 break;
992 done:
993 free(id);
994 free(refname);
995 imsg_free(&imsg);
996 return err;
999 const struct got_error *
1000 got_privsep_send_packfd(struct imsgbuf *ibuf, int fd)
1002 return send_fd(ibuf, GOT_IMSG_SEND_PACKFD, fd);
1005 const struct got_error *
1006 got_privsep_recv_send_progress(int *done, off_t *bytes_sent,
1007 int *success, char **refname, struct imsgbuf *ibuf)
1009 const struct got_error *err = NULL;
1010 struct imsg imsg;
1011 size_t datalen;
1012 struct got_imsg_send_ref_status iref_status;
1014 /* Do not reset the current value of 'bytes_sent', it accumulates. */
1015 *done = 0;
1016 *success = 0;
1017 *refname = NULL;
1019 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1020 if (err)
1021 return err;
1023 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1024 switch (imsg.hdr.type) {
1025 case GOT_IMSG_ERROR:
1026 if (datalen < sizeof(struct got_imsg_error)) {
1027 err = got_error(GOT_ERR_PRIVSEP_LEN);
1028 break;
1030 err = recv_imsg_error(&imsg, datalen);
1031 break;
1032 case GOT_IMSG_SEND_UPLOAD_PROGRESS:
1033 if (datalen < sizeof(*bytes_sent)) {
1034 err = got_error(GOT_ERR_PRIVSEP_MSG);
1035 break;
1037 memcpy(bytes_sent, imsg.data, sizeof(*bytes_sent));
1038 break;
1039 case GOT_IMSG_SEND_REF_STATUS:
1040 if (datalen < sizeof(iref_status)) {
1041 err = got_error(GOT_ERR_PRIVSEP_MSG);
1042 break;
1044 memcpy(&iref_status, imsg.data, sizeof(iref_status));
1045 if (datalen != sizeof(iref_status) + iref_status.name_len) {
1046 err = got_error(GOT_ERR_PRIVSEP_MSG);
1047 break;
1049 *success = iref_status.success;
1050 *refname = strndup(imsg.data + sizeof(iref_status),
1051 iref_status.name_len);
1052 break;
1053 case GOT_IMSG_SEND_DONE:
1054 if (datalen != 0) {
1055 err = got_error(GOT_ERR_PRIVSEP_MSG);
1056 break;
1058 *done = 1;
1059 break;
1060 default:
1061 err = got_error(GOT_ERR_PRIVSEP_MSG);
1062 break;
1065 imsg_free(&imsg);
1066 return err;
1069 const struct got_error *
1070 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_sha1,
1071 int fd)
1073 const struct got_error *err = NULL;
1075 /* Keep in sync with struct got_imsg_index_pack_request */
1076 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
1077 pack_sha1, SHA1_DIGEST_LENGTH) == -1) {
1078 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
1079 close(fd);
1080 return err;
1082 return flush_imsg(ibuf);
1085 const struct got_error *
1086 got_privsep_send_index_pack_outfd(struct imsgbuf *ibuf, int fd)
1088 return send_fd(ibuf, GOT_IMSG_IDXPACK_OUTFD, fd);
1091 const struct got_error *
1092 got_privsep_recv_index_progress(int *done, int *nobj_total,
1093 int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
1094 struct imsgbuf *ibuf)
1096 const struct got_error *err = NULL;
1097 struct imsg imsg;
1098 struct got_imsg_index_pack_progress *iprogress;
1099 size_t datalen;
1101 *done = 0;
1102 *nobj_total = 0;
1103 *nobj_indexed = 0;
1104 *nobj_resolved = 0;
1106 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1107 if (err)
1108 return err;
1110 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1111 switch (imsg.hdr.type) {
1112 case GOT_IMSG_ERROR:
1113 if (datalen < sizeof(struct got_imsg_error)) {
1114 err = got_error(GOT_ERR_PRIVSEP_LEN);
1115 break;
1117 err = recv_imsg_error(&imsg, datalen);
1118 break;
1119 case GOT_IMSG_IDXPACK_PROGRESS:
1120 if (datalen < sizeof(*iprogress)) {
1121 err = got_error(GOT_ERR_PRIVSEP_LEN);
1122 break;
1124 iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
1125 *nobj_total = iprogress->nobj_total;
1126 *nobj_indexed = iprogress->nobj_indexed;
1127 *nobj_loose = iprogress->nobj_loose;
1128 *nobj_resolved = iprogress->nobj_resolved;
1129 break;
1130 case GOT_IMSG_IDXPACK_DONE:
1131 if (datalen != 0) {
1132 err = got_error(GOT_ERR_PRIVSEP_LEN);
1133 break;
1135 *done = 1;
1136 break;
1137 default:
1138 err = got_error(GOT_ERR_PRIVSEP_MSG);
1139 break;
1142 imsg_free(&imsg);
1143 return err;
1146 const struct got_error *
1147 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
1148 struct imsgbuf *ibuf)
1150 struct got_imsg_object *iobj;
1151 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1153 if (datalen != sizeof(*iobj))
1154 return got_error(GOT_ERR_PRIVSEP_LEN);
1155 iobj = imsg->data;
1157 *obj = calloc(1, sizeof(**obj));
1158 if (*obj == NULL)
1159 return got_error_from_errno("calloc");
1161 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
1162 (*obj)->type = iobj->type;
1163 (*obj)->flags = iobj->flags;
1164 (*obj)->hdrlen = iobj->hdrlen;
1165 (*obj)->size = iobj->size;
1166 /* path_packfile is handled by caller */
1167 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
1168 (*obj)->pack_offset = iobj->pack_offset;
1169 (*obj)->pack_idx = iobj->pack_idx;
1171 STAILQ_INIT(&(*obj)->deltas.entries);
1172 return NULL;
1175 const struct got_error *
1176 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
1178 const struct got_error *err = NULL;
1179 struct imsg imsg;
1180 const size_t min_datalen =
1181 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
1183 *obj = NULL;
1185 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1186 if (err)
1187 return err;
1189 switch (imsg.hdr.type) {
1190 case GOT_IMSG_OBJECT:
1191 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
1192 break;
1193 default:
1194 err = got_error(GOT_ERR_PRIVSEP_MSG);
1195 break;
1198 imsg_free(&imsg);
1200 return err;
1203 static const struct got_error *
1204 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
1205 size_t logmsg_len)
1207 const struct got_error *err = NULL;
1208 size_t offset, remain;
1210 offset = 0;
1211 remain = logmsg_len;
1212 while (remain > 0) {
1213 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1215 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
1216 commit->logmsg + offset, n) == -1) {
1217 err = got_error_from_errno("imsg_compose "
1218 "COMMIT_LOGMSG");
1219 break;
1222 err = flush_imsg(ibuf);
1223 if (err)
1224 break;
1226 offset += n;
1227 remain -= n;
1230 return err;
1233 const struct got_error *
1234 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
1236 const struct got_error *err = NULL;
1237 struct got_imsg_commit_object *icommit;
1238 uint8_t *buf;
1239 size_t len, total;
1240 struct got_object_qid *qid;
1241 size_t author_len = strlen(commit->author);
1242 size_t committer_len = strlen(commit->committer);
1243 size_t logmsg_len = strlen(commit->logmsg);
1245 total = sizeof(*icommit) + author_len + committer_len +
1246 commit->nparents * SHA1_DIGEST_LENGTH;
1248 buf = malloc(total);
1249 if (buf == NULL)
1250 return got_error_from_errno("malloc");
1252 icommit = (struct got_imsg_commit_object *)buf;
1253 memcpy(icommit->tree_id, commit->tree_id->sha1,
1254 sizeof(icommit->tree_id));
1255 icommit->author_len = author_len;
1256 icommit->author_time = commit->author_time;
1257 icommit->author_gmtoff = commit->author_gmtoff;
1258 icommit->committer_len = committer_len;
1259 icommit->committer_time = commit->committer_time;
1260 icommit->committer_gmtoff = commit->committer_gmtoff;
1261 icommit->logmsg_len = logmsg_len;
1262 icommit->nparents = commit->nparents;
1264 len = sizeof(*icommit);
1265 memcpy(buf + len, commit->author, author_len);
1266 len += author_len;
1267 memcpy(buf + len, commit->committer, committer_len);
1268 len += committer_len;
1269 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
1270 memcpy(buf + len, &qid->id, SHA1_DIGEST_LENGTH);
1271 len += SHA1_DIGEST_LENGTH;
1274 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
1275 err = got_error_from_errno("imsg_compose COMMIT");
1276 goto done;
1279 if (logmsg_len == 0 ||
1280 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1281 err = flush_imsg(ibuf);
1282 if (err)
1283 goto done;
1285 err = send_commit_logmsg(ibuf, commit, logmsg_len);
1286 done:
1287 free(buf);
1288 return err;
1291 static const struct got_error *
1292 get_commit_from_imsg(struct got_commit_object **commit,
1293 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
1295 const struct got_error *err = NULL;
1296 struct got_imsg_commit_object *icommit;
1297 size_t len = 0;
1298 int i;
1300 if (datalen < sizeof(*icommit))
1301 return got_error(GOT_ERR_PRIVSEP_LEN);
1303 icommit = imsg->data;
1304 if (datalen != sizeof(*icommit) + icommit->author_len +
1305 icommit->committer_len +
1306 icommit->nparents * SHA1_DIGEST_LENGTH)
1307 return got_error(GOT_ERR_PRIVSEP_LEN);
1309 if (icommit->nparents < 0)
1310 return got_error(GOT_ERR_PRIVSEP_LEN);
1312 len += sizeof(*icommit);
1314 *commit = got_object_commit_alloc_partial();
1315 if (*commit == NULL)
1316 return got_error_from_errno(
1317 "got_object_commit_alloc_partial");
1319 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
1320 SHA1_DIGEST_LENGTH);
1321 (*commit)->author_time = icommit->author_time;
1322 (*commit)->author_gmtoff = icommit->author_gmtoff;
1323 (*commit)->committer_time = icommit->committer_time;
1324 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
1326 if (icommit->author_len == 0) {
1327 (*commit)->author = strdup("");
1328 if ((*commit)->author == NULL) {
1329 err = got_error_from_errno("strdup");
1330 goto done;
1332 } else {
1333 (*commit)->author = malloc(icommit->author_len + 1);
1334 if ((*commit)->author == NULL) {
1335 err = got_error_from_errno("malloc");
1336 goto done;
1338 memcpy((*commit)->author, imsg->data + len,
1339 icommit->author_len);
1340 (*commit)->author[icommit->author_len] = '\0';
1342 len += icommit->author_len;
1344 if (icommit->committer_len == 0) {
1345 (*commit)->committer = strdup("");
1346 if ((*commit)->committer == NULL) {
1347 err = got_error_from_errno("strdup");
1348 goto done;
1350 } else {
1351 (*commit)->committer =
1352 malloc(icommit->committer_len + 1);
1353 if ((*commit)->committer == NULL) {
1354 err = got_error_from_errno("malloc");
1355 goto done;
1357 memcpy((*commit)->committer, imsg->data + len,
1358 icommit->committer_len);
1359 (*commit)->committer[icommit->committer_len] = '\0';
1361 len += icommit->committer_len;
1363 if (icommit->logmsg_len == 0) {
1364 (*commit)->logmsg = strdup("");
1365 if ((*commit)->logmsg == NULL) {
1366 err = got_error_from_errno("strdup");
1367 goto done;
1369 } else {
1370 size_t offset = 0, remain = icommit->logmsg_len;
1372 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1373 if ((*commit)->logmsg == NULL) {
1374 err = got_error_from_errno("malloc");
1375 goto done;
1377 while (remain > 0) {
1378 struct imsg imsg_log;
1379 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1380 remain);
1382 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1383 if (err)
1384 goto done;
1386 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1387 err = got_error(GOT_ERR_PRIVSEP_MSG);
1388 goto done;
1391 memcpy((*commit)->logmsg + offset,
1392 imsg_log.data, n);
1393 imsg_free(&imsg_log);
1394 offset += n;
1395 remain -= n;
1397 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1400 for (i = 0; i < icommit->nparents; i++) {
1401 struct got_object_qid *qid;
1403 err = got_object_qid_alloc_partial(&qid);
1404 if (err)
1405 break;
1406 memcpy(&qid->id, imsg->data + len +
1407 i * SHA1_DIGEST_LENGTH, sizeof(qid->id));
1408 STAILQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1409 (*commit)->nparents++;
1411 done:
1412 if (err) {
1413 got_object_commit_close(*commit);
1414 *commit = NULL;
1416 return err;
1419 const struct got_error *
1420 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1422 const struct got_error *err = NULL;
1423 struct imsg imsg;
1424 size_t datalen;
1425 const size_t min_datalen =
1426 MIN(sizeof(struct got_imsg_error),
1427 sizeof(struct got_imsg_commit_object));
1429 *commit = NULL;
1431 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1432 if (err)
1433 return err;
1435 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1437 switch (imsg.hdr.type) {
1438 case GOT_IMSG_COMMIT:
1439 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1440 break;
1441 default:
1442 err = got_error(GOT_ERR_PRIVSEP_MSG);
1443 break;
1446 imsg_free(&imsg);
1448 return err;
1451 static const struct got_error *
1452 send_tree_entries(struct imsgbuf *ibuf, struct got_parsed_tree_entry *entries,
1453 int idx0, int idxN, size_t len)
1455 struct ibuf *wbuf;
1456 struct got_imsg_tree_entries ientries;
1457 int i;
1459 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRIES, 0, 0, len);
1460 if (wbuf == NULL)
1461 return got_error_from_errno("imsg_create TREE_ENTRY");
1463 ientries.nentries = idxN - idx0 + 1;
1464 if (imsg_add(wbuf, &ientries, sizeof(ientries)) == -1)
1465 return got_error_from_errno("imsg_add TREE_ENTRY");
1467 for (i = idx0; i <= idxN; i++) {
1468 struct got_parsed_tree_entry *pte = &entries[i];
1470 /* Keep in sync with struct got_imsg_tree_object definition! */
1471 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1)
1472 return got_error_from_errno("imsg_add TREE_ENTRY");
1473 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1)
1474 return got_error_from_errno("imsg_add TREE_ENTRY");
1475 if (imsg_add(wbuf, &pte->namelen, sizeof(pte->namelen)) == -1)
1476 return got_error_from_errno("imsg_add TREE_ENTRY");
1478 /* Remaining bytes are the entry's name. */
1479 if (imsg_add(wbuf, pte->name, pte->namelen) == -1)
1480 return got_error_from_errno("imsg_add TREE_ENTRY");
1483 wbuf->fd = -1;
1484 imsg_close(ibuf, wbuf);
1485 return NULL;
1488 const struct got_error *
1489 got_privsep_send_tree(struct imsgbuf *ibuf,
1490 struct got_parsed_tree_entry *entries, int nentries)
1492 const struct got_error *err = NULL;
1493 struct got_imsg_tree_object itree;
1494 size_t entries_len;
1495 int i, j;
1497 itree.nentries = nentries;
1498 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1499 == -1)
1500 return got_error_from_errno("imsg_compose TREE");
1502 entries_len = sizeof(struct got_imsg_tree_entries);
1503 i = 0;
1504 for (j = 0; j < nentries; j++) {
1505 struct got_parsed_tree_entry *pte = &entries[j];
1506 size_t len = sizeof(*pte) + pte->namelen;
1508 if (j > 0 &&
1509 entries_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1510 err = send_tree_entries(ibuf, entries,
1511 i, j - 1, entries_len);
1512 if (err)
1513 return err;
1514 i = j;
1515 entries_len = sizeof(struct got_imsg_tree_entries);
1518 entries_len += len;
1521 if (j > 0) {
1522 err = send_tree_entries(ibuf, entries, i, j - 1, entries_len);
1523 if (err)
1524 return err;
1527 return flush_imsg(ibuf);
1530 const struct got_error *
1531 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1533 const struct got_error *err = NULL;
1534 const size_t min_datalen =
1535 MIN(sizeof(struct got_imsg_error),
1536 sizeof(struct got_imsg_tree_object));
1537 struct got_imsg_tree_object *itree;
1538 size_t i;
1539 int nentries = 0;
1541 *tree = NULL;
1543 err = read_imsg(ibuf);
1544 if (err)
1545 goto done;
1547 for (;;) {
1548 struct imsg imsg;
1549 size_t n;
1550 size_t datalen;
1551 struct got_imsg_tree_entries *ientries;
1552 struct got_tree_entry *te = NULL;
1553 size_t te_offset;
1555 n = imsg_get(ibuf, &imsg);
1556 if (n == 0) {
1557 if ((*tree)) {
1558 if (nentries < (*tree)->nentries) {
1559 err = read_imsg(ibuf);
1560 if (err)
1561 break;
1562 continue;
1563 } else
1564 break;
1565 } else {
1566 err = got_error(GOT_ERR_PRIVSEP_MSG);
1567 break;
1571 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen) {
1572 imsg_free(&imsg);
1573 err = got_error(GOT_ERR_PRIVSEP_LEN);
1574 break;
1577 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1579 switch (imsg.hdr.type) {
1580 case GOT_IMSG_ERROR:
1581 err = recv_imsg_error(&imsg, datalen);
1582 break;
1583 case GOT_IMSG_TREE:
1584 /* This message should only appear once. */
1585 if (*tree != NULL) {
1586 err = got_error(GOT_ERR_PRIVSEP_MSG);
1587 break;
1589 if (datalen != sizeof(*itree)) {
1590 err = got_error(GOT_ERR_PRIVSEP_LEN);
1591 break;
1593 itree = imsg.data;
1594 if (itree->nentries < 0) {
1595 err = got_error(GOT_ERR_PRIVSEP_LEN);
1596 break;
1598 *tree = malloc(sizeof(**tree));
1599 if (*tree == NULL) {
1600 err = got_error_from_errno("malloc");
1601 break;
1603 (*tree)->entries = calloc(itree->nentries,
1604 sizeof(struct got_tree_entry));
1605 if ((*tree)->entries == NULL) {
1606 err = got_error_from_errno("malloc");
1607 free(*tree);
1608 *tree = NULL;
1609 break;
1611 (*tree)->nentries = itree->nentries;
1612 (*tree)->refcnt = 0;
1613 break;
1614 case GOT_IMSG_TREE_ENTRIES:
1615 /* This message should be preceeded by GOT_IMSG_TREE. */
1616 if (*tree == NULL) {
1617 err = got_error(GOT_ERR_PRIVSEP_MSG);
1618 break;
1620 if (datalen <= sizeof(*ientries) ||
1621 datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1622 err = got_error(GOT_ERR_PRIVSEP_LEN);
1623 break;
1626 ientries = imsg.data;
1627 if (ientries->nentries > INT_MAX) {
1628 err = got_error_msg(GOT_ERR_NO_SPACE,
1629 "too many tree entries");
1630 break;
1632 te_offset = sizeof(*ientries);
1633 for (i = 0; i < ientries->nentries; i++) {
1634 struct got_imsg_tree_entry ite;
1635 const char *te_name;
1636 uint8_t *buf = imsg.data + te_offset;
1638 if (te_offset >= datalen) {
1639 err = got_error(GOT_ERR_PRIVSEP_LEN);
1640 break;
1643 /* Might not be aligned, size is ~32 bytes. */
1644 memcpy(&ite, buf, sizeof(ite));
1646 if (ite.namelen >= sizeof(te->name)) {
1647 err = got_error(GOT_ERR_PRIVSEP_LEN);
1648 break;
1650 if (te_offset + sizeof(ite) + ite.namelen >
1651 datalen) {
1652 err = got_error(GOT_ERR_PRIVSEP_LEN);
1653 break;
1655 if (nentries >= (*tree)->nentries) {
1656 err = got_error(GOT_ERR_PRIVSEP_LEN);
1657 break;
1659 te = &(*tree)->entries[nentries];
1660 te_name = buf + sizeof(ite);
1661 memcpy(te->name, te_name, ite.namelen);
1662 te->name[ite.namelen] = '\0';
1663 memcpy(te->id.sha1, ite.id, SHA1_DIGEST_LENGTH);
1664 te->mode = ite.mode;
1665 te->idx = nentries;
1666 nentries++;
1668 te_offset += sizeof(ite) + ite.namelen;
1670 break;
1671 default:
1672 err = got_error(GOT_ERR_PRIVSEP_MSG);
1673 break;
1676 imsg_free(&imsg);
1677 if (err)
1678 break;
1680 done:
1681 if (*tree && (*tree)->nentries != nentries) {
1682 if (err == NULL)
1683 err = got_error(GOT_ERR_PRIVSEP_LEN);
1684 got_object_tree_close(*tree);
1685 *tree = NULL;
1688 return err;
1691 const struct got_error *
1692 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1693 const uint8_t *data)
1695 struct got_imsg_blob iblob;
1697 iblob.size = size;
1698 iblob.hdrlen = hdrlen;
1700 if (data) {
1701 uint8_t *buf;
1703 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1704 return got_error(GOT_ERR_NO_SPACE);
1706 buf = malloc(sizeof(iblob) + size);
1707 if (buf == NULL)
1708 return got_error_from_errno("malloc");
1710 memcpy(buf, &iblob, sizeof(iblob));
1711 memcpy(buf + sizeof(iblob), data, size);
1712 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1713 sizeof(iblob) + size) == -1) {
1714 free(buf);
1715 return got_error_from_errno("imsg_compose BLOB");
1717 free(buf);
1718 } else {
1719 /* Data has already been written to file descriptor. */
1720 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1721 sizeof(iblob)) == -1)
1722 return got_error_from_errno("imsg_compose BLOB");
1726 return flush_imsg(ibuf);
1729 const struct got_error *
1730 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1731 struct imsgbuf *ibuf)
1733 const struct got_error *err = NULL;
1734 struct imsg imsg;
1735 struct got_imsg_blob *iblob;
1736 size_t datalen;
1738 *outbuf = NULL;
1740 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1741 if (err)
1742 return err;
1744 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1746 switch (imsg.hdr.type) {
1747 case GOT_IMSG_BLOB:
1748 if (datalen < sizeof(*iblob)) {
1749 err = got_error(GOT_ERR_PRIVSEP_LEN);
1750 break;
1752 iblob = imsg.data;
1753 *size = iblob->size;
1754 *hdrlen = iblob->hdrlen;
1756 if (datalen == sizeof(*iblob)) {
1757 /* Data has been written to file descriptor. */
1758 break;
1761 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1762 err = got_error(GOT_ERR_PRIVSEP_LEN);
1763 break;
1766 *outbuf = malloc(*size);
1767 if (*outbuf == NULL) {
1768 err = got_error_from_errno("malloc");
1769 break;
1771 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1772 break;
1773 default:
1774 err = got_error(GOT_ERR_PRIVSEP_MSG);
1775 break;
1778 imsg_free(&imsg);
1780 return err;
1783 static const struct got_error *
1784 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1786 const struct got_error *err = NULL;
1787 size_t offset, remain;
1789 offset = 0;
1790 remain = tagmsg_len;
1791 while (remain > 0) {
1792 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1794 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1795 tag->tagmsg + offset, n) == -1) {
1796 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1797 break;
1800 err = flush_imsg(ibuf);
1801 if (err)
1802 break;
1804 offset += n;
1805 remain -= n;
1808 return err;
1811 const struct got_error *
1812 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1814 const struct got_error *err = NULL;
1815 struct got_imsg_tag_object *itag;
1816 uint8_t *buf;
1817 size_t len, total;
1818 size_t tag_len = strlen(tag->tag);
1819 size_t tagger_len = strlen(tag->tagger);
1820 size_t tagmsg_len = strlen(tag->tagmsg);
1822 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1824 buf = malloc(total);
1825 if (buf == NULL)
1826 return got_error_from_errno("malloc");
1828 itag = (struct got_imsg_tag_object *)buf;
1829 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1830 itag->obj_type = tag->obj_type;
1831 itag->tag_len = tag_len;
1832 itag->tagger_len = tagger_len;
1833 itag->tagger_time = tag->tagger_time;
1834 itag->tagger_gmtoff = tag->tagger_gmtoff;
1835 itag->tagmsg_len = tagmsg_len;
1837 len = sizeof(*itag);
1838 memcpy(buf + len, tag->tag, tag_len);
1839 len += tag_len;
1840 memcpy(buf + len, tag->tagger, tagger_len);
1841 len += tagger_len;
1843 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1844 err = got_error_from_errno("imsg_compose TAG");
1845 goto done;
1848 if (tagmsg_len == 0 ||
1849 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1850 err = flush_imsg(ibuf);
1851 if (err)
1852 goto done;
1854 err = send_tagmsg(ibuf, tag, tagmsg_len);
1855 done:
1856 free(buf);
1857 return err;
1860 const struct got_error *
1861 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1863 const struct got_error *err = NULL;
1864 struct imsg imsg;
1865 struct got_imsg_tag_object *itag;
1866 size_t len, datalen;
1867 const size_t min_datalen =
1868 MIN(sizeof(struct got_imsg_error),
1869 sizeof(struct got_imsg_tag_object));
1871 *tag = NULL;
1873 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1874 if (err)
1875 return err;
1877 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1878 len = 0;
1880 switch (imsg.hdr.type) {
1881 case GOT_IMSG_TAG:
1882 if (datalen < sizeof(*itag)) {
1883 err = got_error(GOT_ERR_PRIVSEP_LEN);
1884 break;
1886 itag = imsg.data;
1887 if (datalen != sizeof(*itag) + itag->tag_len +
1888 itag->tagger_len) {
1889 err = got_error(GOT_ERR_PRIVSEP_LEN);
1890 break;
1892 len += sizeof(*itag);
1894 *tag = calloc(1, sizeof(**tag));
1895 if (*tag == NULL) {
1896 err = got_error_from_errno("calloc");
1897 break;
1900 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1902 if (itag->tag_len == 0) {
1903 (*tag)->tag = strdup("");
1904 if ((*tag)->tag == NULL) {
1905 err = got_error_from_errno("strdup");
1906 break;
1908 } else {
1909 (*tag)->tag = malloc(itag->tag_len + 1);
1910 if ((*tag)->tag == NULL) {
1911 err = got_error_from_errno("malloc");
1912 break;
1914 memcpy((*tag)->tag, imsg.data + len,
1915 itag->tag_len);
1916 (*tag)->tag[itag->tag_len] = '\0';
1918 len += itag->tag_len;
1920 (*tag)->obj_type = itag->obj_type;
1921 (*tag)->tagger_time = itag->tagger_time;
1922 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1924 if (itag->tagger_len == 0) {
1925 (*tag)->tagger = strdup("");
1926 if ((*tag)->tagger == NULL) {
1927 err = got_error_from_errno("strdup");
1928 break;
1930 } else {
1931 (*tag)->tagger = malloc(itag->tagger_len + 1);
1932 if ((*tag)->tagger == NULL) {
1933 err = got_error_from_errno("malloc");
1934 break;
1936 memcpy((*tag)->tagger, imsg.data + len,
1937 itag->tagger_len);
1938 (*tag)->tagger[itag->tagger_len] = '\0';
1940 len += itag->tagger_len;
1942 if (itag->tagmsg_len == 0) {
1943 (*tag)->tagmsg = strdup("");
1944 if ((*tag)->tagmsg == NULL) {
1945 err = got_error_from_errno("strdup");
1946 break;
1948 } else {
1949 size_t offset = 0, remain = itag->tagmsg_len;
1951 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1952 if ((*tag)->tagmsg == NULL) {
1953 err = got_error_from_errno("malloc");
1954 break;
1956 while (remain > 0) {
1957 struct imsg imsg_log;
1958 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1959 remain);
1961 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1962 if (err)
1963 return err;
1965 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1966 return got_error(GOT_ERR_PRIVSEP_MSG);
1968 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1969 n);
1970 imsg_free(&imsg_log);
1971 offset += n;
1972 remain -= n;
1974 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1977 break;
1978 default:
1979 err = got_error(GOT_ERR_PRIVSEP_MSG);
1980 break;
1983 imsg_free(&imsg);
1985 return err;
1988 const struct got_error *
1989 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1990 struct got_packidx *packidx)
1992 const struct got_error *err = NULL;
1993 struct got_imsg_packidx ipackidx;
1994 struct got_imsg_pack ipack;
1995 int fd;
1997 ipackidx.len = packidx->len;
1998 ipackidx.packfile_size = pack->filesize;
1999 fd = dup(packidx->fd);
2000 if (fd == -1)
2001 return got_error_from_errno("dup");
2003 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
2004 sizeof(ipackidx)) == -1) {
2005 err = got_error_from_errno("imsg_compose PACKIDX");
2006 close(fd);
2007 return err;
2010 if (strlcpy(ipack.path_packfile, pack->path_packfile,
2011 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
2012 return got_error(GOT_ERR_NO_SPACE);
2013 ipack.filesize = pack->filesize;
2015 fd = dup(pack->fd);
2016 if (fd == -1)
2017 return got_error_from_errno("dup");
2019 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
2020 == -1) {
2021 err = got_error_from_errno("imsg_compose PACK");
2022 close(fd);
2023 return err;
2026 return flush_imsg(ibuf);
2029 const struct got_error *
2030 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
2031 struct got_object_id *id)
2033 struct got_imsg_packed_object iobj;
2035 iobj.idx = idx;
2036 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2038 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
2039 &iobj, sizeof(iobj)) == -1)
2040 return got_error_from_errno("imsg_compose "
2041 "PACKED_OBJECT_REQUEST");
2043 return flush_imsg(ibuf);
2046 const struct got_error *
2047 got_privsep_send_packed_raw_obj_req(struct imsgbuf *ibuf, int idx,
2048 struct got_object_id *id)
2050 struct got_imsg_packed_object iobj;
2052 iobj.idx = idx;
2053 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
2055 if (imsg_compose(ibuf, GOT_IMSG_PACKED_RAW_OBJECT_REQUEST, 0, 0, -1,
2056 &iobj, sizeof(iobj)) == -1)
2057 return got_error_from_errno("imsg_compose "
2058 "PACKED_OBJECT_REQUEST");
2060 return flush_imsg(ibuf);
2063 const struct got_error *
2064 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
2066 const struct got_error *err = NULL;
2068 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
2069 NULL, 0) == -1) {
2070 err = got_error_from_errno("imsg_compose "
2071 "GITCONFIG_PARSE_REQUEST");
2072 close(fd);
2073 return err;
2076 return flush_imsg(ibuf);
2079 const struct got_error *
2080 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
2082 if (imsg_compose(ibuf,
2083 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
2084 NULL, 0) == -1)
2085 return got_error_from_errno("imsg_compose "
2086 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
2088 return flush_imsg(ibuf);
2091 const struct got_error *
2092 got_privsep_send_gitconfig_repository_extensions_req(struct imsgbuf *ibuf)
2094 if (imsg_compose(ibuf,
2095 GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST, 0, 0, -1,
2096 NULL, 0) == -1)
2097 return got_error_from_errno("imsg_compose "
2098 "GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST");
2100 return flush_imsg(ibuf);
2104 const struct got_error *
2105 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
2107 if (imsg_compose(ibuf,
2108 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
2109 return got_error_from_errno("imsg_compose "
2110 "GITCONFIG_AUTHOR_NAME_REQUEST");
2112 return flush_imsg(ibuf);
2115 const struct got_error *
2116 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
2118 if (imsg_compose(ibuf,
2119 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
2120 return got_error_from_errno("imsg_compose "
2121 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
2123 return flush_imsg(ibuf);
2126 const struct got_error *
2127 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
2129 if (imsg_compose(ibuf,
2130 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2131 return got_error_from_errno("imsg_compose "
2132 "GITCONFIG_REMOTE_REQUEST");
2134 return flush_imsg(ibuf);
2137 const struct got_error *
2138 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
2140 if (imsg_compose(ibuf,
2141 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
2142 return got_error_from_errno("imsg_compose "
2143 "GITCONFIG_OWNER_REQUEST");
2145 return flush_imsg(ibuf);
2148 const struct got_error *
2149 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
2151 const struct got_error *err = NULL;
2152 struct imsg imsg;
2153 size_t datalen;
2154 const size_t min_datalen = 0;
2156 *str = NULL;
2158 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2159 if (err)
2160 return err;
2161 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2163 switch (imsg.hdr.type) {
2164 case GOT_IMSG_GITCONFIG_STR_VAL:
2165 if (datalen == 0)
2166 break;
2167 /* datalen does not include terminating \0 */
2168 *str = malloc(datalen + 1);
2169 if (*str == NULL) {
2170 err = got_error_from_errno("malloc");
2171 break;
2173 memcpy(*str, imsg.data, datalen);
2174 (*str)[datalen] = '\0';
2175 break;
2176 default:
2177 err = got_error(GOT_ERR_PRIVSEP_MSG);
2178 break;
2181 imsg_free(&imsg);
2182 return err;
2185 const struct got_error *
2186 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
2188 const struct got_error *err = NULL;
2189 struct imsg imsg;
2190 size_t datalen;
2191 const size_t min_datalen =
2192 MIN(sizeof(struct got_imsg_error), sizeof(int));
2194 *val = 0;
2196 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2197 if (err)
2198 return err;
2199 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2201 switch (imsg.hdr.type) {
2202 case GOT_IMSG_GITCONFIG_INT_VAL:
2203 if (datalen != sizeof(*val)) {
2204 err = got_error(GOT_ERR_PRIVSEP_LEN);
2205 break;
2207 memcpy(val, imsg.data, sizeof(*val));
2208 break;
2209 default:
2210 err = got_error(GOT_ERR_PRIVSEP_MSG);
2211 break;
2214 imsg_free(&imsg);
2215 return err;
2218 static void
2219 free_remote_data(struct got_remote_repo *remote)
2221 int i;
2223 free(remote->name);
2224 free(remote->fetch_url);
2225 free(remote->send_url);
2226 for (i = 0; i < remote->nfetch_branches; i++)
2227 free(remote->fetch_branches[i]);
2228 free(remote->fetch_branches);
2229 for (i = 0; i < remote->nsend_branches; i++)
2230 free(remote->send_branches[i]);
2231 free(remote->send_branches);
2232 for (i = 0; i < remote->nfetch_refs; i++)
2233 free(remote->fetch_refs[i]);
2234 free(remote->fetch_refs);
2237 const struct got_error *
2238 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
2239 int *nremotes, struct imsgbuf *ibuf)
2241 const struct got_error *err = NULL;
2242 struct imsg imsg;
2243 size_t datalen;
2244 struct got_imsg_remotes iremotes;
2245 struct got_imsg_remote iremote;
2247 *remotes = NULL;
2248 *nremotes = 0;
2249 iremotes.nremotes = 0;
2251 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
2252 if (err)
2253 return err;
2254 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2256 switch (imsg.hdr.type) {
2257 case GOT_IMSG_GITCONFIG_REMOTES:
2258 if (datalen != sizeof(iremotes)) {
2259 err = got_error(GOT_ERR_PRIVSEP_LEN);
2260 break;
2262 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2263 if (iremotes.nremotes == 0) {
2264 imsg_free(&imsg);
2265 return NULL;
2267 break;
2268 default:
2269 imsg_free(&imsg);
2270 return got_error(GOT_ERR_PRIVSEP_MSG);
2273 imsg_free(&imsg);
2275 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2276 if (*remotes == NULL)
2277 return got_error_from_errno("recallocarray");
2279 while (*nremotes < iremotes.nremotes) {
2280 struct got_remote_repo *remote;
2282 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
2283 if (err)
2284 break;
2285 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2287 switch (imsg.hdr.type) {
2288 case GOT_IMSG_GITCONFIG_REMOTE:
2289 remote = &(*remotes)[*nremotes];
2290 memset(remote, 0, sizeof(*remote));
2291 if (datalen < sizeof(iremote)) {
2292 err = got_error(GOT_ERR_PRIVSEP_LEN);
2293 break;
2295 memcpy(&iremote, imsg.data, sizeof(iremote));
2296 if (iremote.name_len == 0 ||
2297 iremote.fetch_url_len == 0 ||
2298 iremote.send_url_len == 0 ||
2299 (sizeof(iremote) + iremote.name_len +
2300 iremote.fetch_url_len + iremote.send_url_len) > datalen) {
2301 err = got_error(GOT_ERR_PRIVSEP_LEN);
2302 break;
2304 remote->name = strndup(imsg.data + sizeof(iremote),
2305 iremote.name_len);
2306 if (remote->name == NULL) {
2307 err = got_error_from_errno("strndup");
2308 break;
2310 remote->fetch_url = strndup(imsg.data + sizeof(iremote) +
2311 iremote.name_len, iremote.fetch_url_len);
2312 if (remote->fetch_url == NULL) {
2313 err = got_error_from_errno("strndup");
2314 free_remote_data(remote);
2315 break;
2317 remote->send_url = strndup(imsg.data + sizeof(iremote) +
2318 iremote.name_len + iremote.fetch_url_len,
2319 iremote.send_url_len);
2320 if (remote->send_url == NULL) {
2321 err = got_error_from_errno("strndup");
2322 free_remote_data(remote);
2323 break;
2325 remote->mirror_references = iremote.mirror_references;
2326 remote->fetch_all_branches = iremote.fetch_all_branches;
2327 remote->nfetch_branches = 0;
2328 remote->fetch_branches = NULL;
2329 remote->nsend_branches = 0;
2330 remote->send_branches = NULL;
2331 remote->nfetch_refs = 0;
2332 remote->fetch_refs = NULL;
2333 (*nremotes)++;
2334 break;
2335 default:
2336 err = got_error(GOT_ERR_PRIVSEP_MSG);
2337 break;
2340 imsg_free(&imsg);
2341 if (err)
2342 break;
2345 if (err) {
2346 int i;
2347 for (i = 0; i < *nremotes; i++)
2348 free_remote_data(&(*remotes)[i]);
2349 free(*remotes);
2350 *remotes = NULL;
2351 *nremotes = 0;
2353 return err;
2356 const struct got_error *
2357 got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
2359 const struct got_error *err = NULL;
2361 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
2362 NULL, 0) == -1) {
2363 err = got_error_from_errno("imsg_compose "
2364 "GOTCONFIG_PARSE_REQUEST");
2365 close(fd);
2366 return err;
2369 return flush_imsg(ibuf);
2372 const struct got_error *
2373 got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
2375 if (imsg_compose(ibuf,
2376 GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
2377 return got_error_from_errno("imsg_compose "
2378 "GOTCONFIG_AUTHOR_REQUEST");
2380 return flush_imsg(ibuf);
2383 const struct got_error *
2384 got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
2386 if (imsg_compose(ibuf,
2387 GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
2388 return got_error_from_errno("imsg_compose "
2389 "GOTCONFIG_REMOTE_REQUEST");
2391 return flush_imsg(ibuf);
2394 const struct got_error *
2395 got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
2397 const struct got_error *err = NULL;
2398 struct imsg imsg;
2399 size_t datalen;
2400 const size_t min_datalen = 0;
2402 *str = NULL;
2404 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2405 if (err)
2406 return err;
2407 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2409 switch (imsg.hdr.type) {
2410 case GOT_IMSG_ERROR:
2411 if (datalen < sizeof(struct got_imsg_error)) {
2412 err = got_error(GOT_ERR_PRIVSEP_LEN);
2413 break;
2415 err = recv_imsg_error(&imsg, datalen);
2416 break;
2417 case GOT_IMSG_GOTCONFIG_STR_VAL:
2418 if (datalen == 0)
2419 break;
2420 /* datalen does not include terminating \0 */
2421 *str = malloc(datalen + 1);
2422 if (*str == NULL) {
2423 err = got_error_from_errno("malloc");
2424 break;
2426 memcpy(*str, imsg.data, datalen);
2427 (*str)[datalen] = '\0';
2428 break;
2429 default:
2430 err = got_error(GOT_ERR_PRIVSEP_MSG);
2431 break;
2434 imsg_free(&imsg);
2435 return err;
2438 const struct got_error *
2439 got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
2440 int *nremotes, struct imsgbuf *ibuf)
2442 const struct got_error *err = NULL;
2443 struct imsg imsg;
2444 size_t datalen;
2445 struct got_imsg_remotes iremotes;
2446 struct got_imsg_remote iremote;
2447 const size_t min_datalen =
2448 MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
2450 *remotes = NULL;
2451 *nremotes = 0;
2452 iremotes.nremotes = 0;
2454 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2455 if (err)
2456 return err;
2457 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2459 switch (imsg.hdr.type) {
2460 case GOT_IMSG_ERROR:
2461 if (datalen < sizeof(struct got_imsg_error)) {
2462 err = got_error(GOT_ERR_PRIVSEP_LEN);
2463 break;
2465 err = recv_imsg_error(&imsg, datalen);
2466 break;
2467 case GOT_IMSG_GOTCONFIG_REMOTES:
2468 if (datalen != sizeof(iremotes)) {
2469 err = got_error(GOT_ERR_PRIVSEP_LEN);
2470 break;
2472 memcpy(&iremotes, imsg.data, sizeof(iremotes));
2473 if (iremotes.nremotes == 0) {
2474 imsg_free(&imsg);
2475 return NULL;
2477 break;
2478 default:
2479 imsg_free(&imsg);
2480 return got_error(GOT_ERR_PRIVSEP_MSG);
2483 imsg_free(&imsg);
2485 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
2486 if (*remotes == NULL)
2487 return got_error_from_errno("recallocarray");
2489 while (*nremotes < iremotes.nremotes) {
2490 struct got_remote_repo *remote;
2491 const size_t min_datalen =
2492 MIN(sizeof(struct got_imsg_error), sizeof(iremote));
2493 int i;
2495 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
2496 if (err)
2497 break;
2498 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2500 switch (imsg.hdr.type) {
2501 case GOT_IMSG_ERROR:
2502 if (datalen < sizeof(struct got_imsg_error)) {
2503 err = got_error(GOT_ERR_PRIVSEP_LEN);
2504 break;
2506 err = recv_imsg_error(&imsg, datalen);
2507 break;
2508 case GOT_IMSG_GOTCONFIG_REMOTE:
2509 remote = &(*remotes)[*nremotes];
2510 memset(remote, 0, sizeof(*remote));
2511 if (datalen < sizeof(iremote)) {
2512 err = got_error(GOT_ERR_PRIVSEP_LEN);
2513 break;
2515 memcpy(&iremote, imsg.data, sizeof(iremote));
2516 if (iremote.name_len == 0 ||
2517 (iremote.fetch_url_len == 0 &&
2518 iremote.send_url_len == 0) ||
2519 (sizeof(iremote) + iremote.name_len +
2520 iremote.fetch_url_len + iremote.send_url_len) >
2521 datalen) {
2522 err = got_error(GOT_ERR_PRIVSEP_LEN);
2523 break;
2525 remote->name = strndup(imsg.data + sizeof(iremote),
2526 iremote.name_len);
2527 if (remote->name == NULL) {
2528 err = got_error_from_errno("strndup");
2529 break;
2531 remote->fetch_url = strndup(imsg.data +
2532 sizeof(iremote) + iremote.name_len,
2533 iremote.fetch_url_len);
2534 if (remote->fetch_url == NULL) {
2535 err = got_error_from_errno("strndup");
2536 free_remote_data(remote);
2537 break;
2539 remote->send_url = strndup(imsg.data +
2540 sizeof(iremote) + iremote.name_len +
2541 iremote.fetch_url_len, iremote.send_url_len);
2542 if (remote->send_url == NULL) {
2543 err = got_error_from_errno("strndup");
2544 free_remote_data(remote);
2545 break;
2547 remote->mirror_references = iremote.mirror_references;
2548 remote->fetch_all_branches = iremote.fetch_all_branches;
2549 if (iremote.nfetch_branches > 0) {
2550 remote->fetch_branches = recallocarray(NULL, 0,
2551 iremote.nfetch_branches, sizeof(char *));
2552 if (remote->fetch_branches == NULL) {
2553 err = got_error_from_errno("calloc");
2554 free_remote_data(remote);
2555 break;
2558 remote->nfetch_branches = 0;
2559 for (i = 0; i < iremote.nfetch_branches; i++) {
2560 char *branch;
2561 err = got_privsep_recv_gotconfig_str(&branch,
2562 ibuf);
2563 if (err) {
2564 free_remote_data(remote);
2565 goto done;
2567 remote->fetch_branches[i] = branch;
2568 remote->nfetch_branches++;
2570 if (iremote.nsend_branches > 0) {
2571 remote->send_branches = recallocarray(NULL, 0,
2572 iremote.nsend_branches, sizeof(char *));
2573 if (remote->send_branches == NULL) {
2574 err = got_error_from_errno("calloc");
2575 free_remote_data(remote);
2576 break;
2579 remote->nsend_branches = 0;
2580 for (i = 0; i < iremote.nsend_branches; i++) {
2581 char *branch;
2582 err = got_privsep_recv_gotconfig_str(&branch,
2583 ibuf);
2584 if (err) {
2585 free_remote_data(remote);
2586 goto done;
2588 remote->send_branches[i] = branch;
2589 remote->nsend_branches++;
2591 if (iremote.nfetch_refs > 0) {
2592 remote->fetch_refs = recallocarray(NULL, 0,
2593 iremote.nfetch_refs, sizeof(char *));
2594 if (remote->fetch_refs == NULL) {
2595 err = got_error_from_errno("calloc");
2596 free_remote_data(remote);
2597 break;
2600 remote->nfetch_refs = 0;
2601 for (i = 0; i < iremote.nfetch_refs; i++) {
2602 char *ref;
2603 err = got_privsep_recv_gotconfig_str(&ref,
2604 ibuf);
2605 if (err) {
2606 free_remote_data(remote);
2607 goto done;
2609 remote->fetch_refs[i] = ref;
2610 remote->nfetch_refs++;
2612 (*nremotes)++;
2613 break;
2614 default:
2615 err = got_error(GOT_ERR_PRIVSEP_MSG);
2616 break;
2619 imsg_free(&imsg);
2620 if (err)
2621 break;
2623 done:
2624 if (err) {
2625 int i;
2626 for (i = 0; i < *nremotes; i++)
2627 free_remote_data(&(*remotes)[i]);
2628 free(*remotes);
2629 *remotes = NULL;
2630 *nremotes = 0;
2632 return err;
2635 const struct got_error *
2636 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
2637 struct got_object_id *id, int idx, const char *path)
2639 struct ibuf *wbuf;
2640 size_t path_len = strlen(path) + 1;
2642 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
2643 sizeof(struct got_imsg_commit_traversal_request) + path_len);
2644 if (wbuf == NULL)
2645 return got_error_from_errno(
2646 "imsg_create COMMIT_TRAVERSAL_REQUEST");
2647 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
2648 return got_error_from_errno("imsg_add "
2649 "COMMIT_TRAVERSAL_REQUEST");
2650 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1)
2651 return got_error_from_errno("imsg_add "
2652 "COMMIT_TRAVERSAL_REQUEST");
2653 if (imsg_add(wbuf, path, path_len) == -1)
2654 return got_error_from_errno("imsg_add "
2655 "COMMIT_TRAVERSAL_REQUEST");
2657 wbuf->fd = -1;
2658 imsg_close(ibuf, wbuf);
2660 return flush_imsg(ibuf);
2663 const struct got_error *
2664 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2665 struct got_object_id **changed_commit_id,
2666 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2668 const struct got_error *err = NULL;
2669 struct imsg imsg;
2670 struct got_imsg_traversed_commits *icommits;
2671 size_t datalen;
2672 int i, done = 0;
2674 *changed_commit = NULL;
2675 *changed_commit_id = NULL;
2677 while (!done) {
2678 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2679 if (err)
2680 return err;
2682 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2683 switch (imsg.hdr.type) {
2684 case GOT_IMSG_TRAVERSED_COMMITS:
2685 icommits = imsg.data;
2686 if (datalen != sizeof(*icommits) +
2687 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2688 err = got_error(GOT_ERR_PRIVSEP_LEN);
2689 break;
2691 for (i = 0; i < icommits->ncommits; i++) {
2692 struct got_object_qid *qid;
2693 uint8_t *sha1 = (uint8_t *)imsg.data +
2694 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2695 err = got_object_qid_alloc_partial(&qid);
2696 if (err)
2697 break;
2698 memcpy(qid->id.sha1, sha1, SHA1_DIGEST_LENGTH);
2699 STAILQ_INSERT_TAIL(commit_ids, qid, entry);
2701 /* The last commit may contain a change. */
2702 if (i == icommits->ncommits - 1) {
2703 *changed_commit_id =
2704 got_object_id_dup(&qid->id);
2705 if (*changed_commit_id == NULL) {
2706 err = got_error_from_errno(
2707 "got_object_id_dup");
2708 break;
2712 break;
2713 case GOT_IMSG_COMMIT:
2714 if (*changed_commit_id == NULL) {
2715 err = got_error(GOT_ERR_PRIVSEP_MSG);
2716 break;
2718 err = get_commit_from_imsg(changed_commit, &imsg,
2719 datalen, ibuf);
2720 break;
2721 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2722 done = 1;
2723 break;
2724 default:
2725 err = got_error(GOT_ERR_PRIVSEP_MSG);
2726 break;
2729 imsg_free(&imsg);
2730 if (err)
2731 break;
2734 if (err)
2735 got_object_id_queue_free(commit_ids);
2736 return err;
2739 const struct got_error *
2740 got_privsep_send_raw_delta_req(struct imsgbuf *ibuf, int idx,
2741 struct got_object_id *id)
2743 struct got_imsg_raw_delta_request dreq;
2745 dreq.idx = idx;
2746 memcpy(dreq.id, id->sha1, SHA1_DIGEST_LENGTH);
2748 if (imsg_compose(ibuf, GOT_IMSG_RAW_DELTA_REQUEST, 0, 0, -1,
2749 &dreq, sizeof(dreq)) == -1)
2750 return got_error_from_errno("imsg_compose RAW_DELTA_REQUEST");
2752 return flush_imsg(ibuf);
2755 const struct got_error *
2756 got_privsep_send_raw_delta_outfd(struct imsgbuf *ibuf, int fd)
2758 return send_fd(ibuf, GOT_IMSG_RAW_DELTA_OUTFD, fd);
2761 const struct got_error *
2762 got_privsep_send_raw_delta(struct imsgbuf *ibuf, uint64_t base_size,
2763 uint64_t result_size, off_t delta_size, off_t delta_compressed_size,
2764 off_t delta_offset, off_t delta_out_offset, struct got_object_id *base_id)
2766 struct got_imsg_raw_delta idelta;
2767 int ret;
2769 idelta.base_size = base_size;
2770 idelta.result_size = result_size;
2771 idelta.delta_size = delta_size;
2772 idelta.delta_compressed_size = delta_compressed_size;
2773 idelta.delta_offset = delta_offset;
2774 idelta.delta_out_offset = delta_out_offset;
2775 memcpy(idelta.base_id, base_id->sha1, SHA1_DIGEST_LENGTH);
2777 ret = imsg_compose(ibuf, GOT_IMSG_RAW_DELTA, 0, 0, -1,
2778 &idelta, sizeof(idelta));
2779 if (ret == -1)
2780 return got_error_from_errno("imsg_compose RAW_DELTA");
2782 return flush_imsg(ibuf);
2785 const struct got_error *
2786 got_privsep_recv_raw_delta(uint64_t *base_size, uint64_t *result_size,
2787 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
2788 off_t *delta_out_offset, struct got_object_id **base_id, struct imsgbuf *ibuf)
2790 const struct got_error *err = NULL;
2791 struct imsg imsg;
2792 struct got_imsg_raw_delta *delta;
2793 size_t datalen;
2795 *base_size = 0;
2796 *result_size = 0;
2797 *delta_size = 0;
2798 *delta_compressed_size = 0;
2799 *delta_offset = 0;
2800 *delta_out_offset = 0;
2801 *base_id = NULL;
2803 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2804 if (err)
2805 return err;
2807 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2809 switch (imsg.hdr.type) {
2810 case GOT_IMSG_RAW_DELTA:
2811 if (datalen != sizeof(*delta)) {
2812 err = got_error(GOT_ERR_PRIVSEP_LEN);
2813 break;
2815 delta = imsg.data;
2816 *base_size = delta->base_size;
2817 *result_size = delta->result_size;
2818 *delta_size = delta->delta_size;
2819 *delta_compressed_size = delta->delta_compressed_size;
2820 *delta_offset = delta->delta_offset;
2821 *delta_out_offset = delta->delta_out_offset;
2822 *base_id = calloc(1, sizeof(**base_id));
2823 if (*base_id == NULL) {
2824 err = got_error_from_errno("malloc");
2825 break;
2827 memcpy((*base_id)->sha1, delta->base_id, SHA1_DIGEST_LENGTH);
2828 break;
2829 default:
2830 err = got_error(GOT_ERR_PRIVSEP_MSG);
2831 break;
2834 imsg_free(&imsg);
2836 if (err) {
2837 free(*base_id);
2838 *base_id = NULL;
2840 return err;
2843 const struct got_error *
2844 got_privsep_send_object_idlist(struct imsgbuf *ibuf,
2845 struct got_object_id **ids, size_t nids)
2847 const struct got_error *err = NULL;
2848 struct got_imsg_object_idlist idlist;
2849 struct ibuf *wbuf;
2850 size_t i;
2852 if (nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS)
2853 return got_error(GOT_ERR_NO_SPACE);
2855 wbuf = imsg_create(ibuf, GOT_IMSG_OBJ_ID_LIST, 0, 0,
2856 sizeof(idlist) + nids * sizeof(**ids));
2857 if (wbuf == NULL) {
2858 err = got_error_from_errno("imsg_create OBJ_ID_LIST");
2859 return err;
2862 idlist.nids = nids;
2863 if (imsg_add(wbuf, &idlist, sizeof(idlist)) == -1)
2864 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2866 for (i = 0; i < nids; i++) {
2867 struct got_object_id *id = ids[i];
2868 if (imsg_add(wbuf, id, sizeof(*id)) == -1)
2869 return got_error_from_errno("imsg_add OBJ_ID_LIST");
2872 wbuf->fd = -1;
2873 imsg_close(ibuf, wbuf);
2875 return flush_imsg(ibuf);
2878 const struct got_error *
2879 got_privsep_send_object_idlist_done(struct imsgbuf *ibuf)
2881 if (imsg_compose(ibuf, GOT_IMSG_OBJ_ID_LIST_DONE, 0, 0, -1, NULL, 0)
2882 == -1)
2883 return got_error_from_errno("imsg_compose OBJ_ID_LIST_DONE");
2885 return flush_imsg(ibuf);
2888 const struct got_error *
2889 got_privsep_recv_object_idlist(int *done, struct got_object_id **ids,
2890 size_t *nids, struct imsgbuf *ibuf)
2892 const struct got_error *err = NULL;
2893 struct imsg imsg;
2894 struct got_imsg_object_idlist *idlist;
2895 size_t datalen;
2897 *ids = NULL;
2898 *done = 0;
2899 *nids = 0;
2901 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2902 if (err)
2903 return err;
2905 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2906 switch (imsg.hdr.type) {
2907 case GOT_IMSG_OBJ_ID_LIST:
2908 if (datalen < sizeof(*idlist)) {
2909 err = got_error(GOT_ERR_PRIVSEP_LEN);
2910 break;
2912 idlist = imsg.data;
2913 if (idlist->nids > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
2914 err = got_error(GOT_ERR_PRIVSEP_LEN);
2915 break;
2917 *nids = idlist->nids;
2918 *ids = calloc(*nids, sizeof(**ids));
2919 if (*ids == NULL) {
2920 err = got_error_from_errno("calloc");
2921 break;
2923 memcpy(*ids, (uint8_t *)imsg.data + sizeof(idlist),
2924 *nids * sizeof(**ids));
2925 break;
2926 case GOT_IMSG_OBJ_ID_LIST_DONE:
2927 *done = 1;
2928 break;
2929 default:
2930 err = got_error(GOT_ERR_PRIVSEP_MSG);
2931 break;
2934 imsg_free(&imsg);
2936 return err;
2939 const struct got_error *
2940 got_privsep_send_delta_reuse_req(struct imsgbuf *ibuf)
2942 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_REQUEST, 0, 0, -1, NULL, 0)
2943 == -1)
2944 return got_error_from_errno("imsg_compose DELTA_REUSE_REQUEST");
2946 return flush_imsg(ibuf);
2949 const struct got_error *
2950 got_privsep_send_reused_deltas(struct imsgbuf *ibuf,
2951 struct got_imsg_reused_delta *deltas, size_t ndeltas)
2953 const struct got_error *err = NULL;
2954 struct ibuf *wbuf;
2955 struct got_imsg_reused_deltas ideltas;
2956 size_t i;
2958 if (ndeltas > GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS)
2959 return got_error(GOT_ERR_NO_SPACE);
2961 wbuf = imsg_create(ibuf, GOT_IMSG_REUSED_DELTAS, 0, 0,
2962 sizeof(ideltas) + ndeltas * sizeof(*deltas));
2963 if (wbuf == NULL) {
2964 err = got_error_from_errno("imsg_create REUSED_DELTAS");
2965 return err;
2968 ideltas.ndeltas = ndeltas;
2969 if (imsg_add(wbuf, &ideltas, sizeof(ideltas)) == -1)
2970 return got_error_from_errno("imsg_add REUSED_DELTAS");
2972 for (i = 0; i < ndeltas; i++) {
2973 struct got_imsg_reused_delta *delta = &deltas[i];
2974 if (imsg_add(wbuf, delta, sizeof(*delta)) == -1)
2975 return got_error_from_errno("imsg_add REUSED_DELTAS");
2978 wbuf->fd = -1;
2979 imsg_close(ibuf, wbuf);
2981 return flush_imsg(ibuf);
2984 const struct got_error *
2985 got_privsep_send_reused_deltas_done(struct imsgbuf *ibuf)
2987 if (imsg_compose(ibuf, GOT_IMSG_DELTA_REUSE_DONE, 0, 0, -1, NULL, 0)
2988 == -1)
2989 return got_error_from_errno("imsg_compose DELTA_REUSE_DONE");
2991 return flush_imsg(ibuf);
2994 const struct got_error *
2995 got_privsep_recv_reused_deltas(int *done, struct got_imsg_reused_delta *deltas,
2996 size_t *ndeltas, struct imsgbuf *ibuf)
2998 const struct got_error *err = NULL;
2999 struct imsg imsg;
3000 struct got_imsg_reused_deltas *ideltas;
3001 size_t datalen;
3003 *done = 0;
3004 *ndeltas = 0;
3006 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
3007 if (err)
3008 return err;
3010 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
3011 switch (imsg.hdr.type) {
3012 case GOT_IMSG_REUSED_DELTAS:
3013 if (datalen < sizeof(*ideltas)) {
3014 err = got_error(GOT_ERR_PRIVSEP_LEN);
3015 break;
3017 ideltas = imsg.data;
3018 if (ideltas->ndeltas > GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
3019 err = got_error(GOT_ERR_PRIVSEP_LEN);
3020 break;
3022 *ndeltas = ideltas->ndeltas;
3023 memcpy(deltas, (uint8_t *)imsg.data + sizeof(ideltas),
3024 *ndeltas * sizeof(*deltas));
3025 break;
3026 case GOT_IMSG_DELTA_REUSE_DONE:
3027 *done = 1;
3028 break;
3029 default:
3030 err = got_error(GOT_ERR_PRIVSEP_MSG);
3031 break;
3034 imsg_free(&imsg);
3036 return err;
3039 const struct got_error *
3040 got_privsep_unveil_exec_helpers(void)
3042 const char *helpers[] = {
3043 GOT_PATH_PROG_READ_PACK,
3044 GOT_PATH_PROG_READ_OBJECT,
3045 GOT_PATH_PROG_READ_COMMIT,
3046 GOT_PATH_PROG_READ_TREE,
3047 GOT_PATH_PROG_READ_BLOB,
3048 GOT_PATH_PROG_READ_TAG,
3049 GOT_PATH_PROG_READ_GITCONFIG,
3050 GOT_PATH_PROG_READ_GOTCONFIG,
3051 GOT_PATH_PROG_READ_PATCH,
3052 GOT_PATH_PROG_FETCH_PACK,
3053 GOT_PATH_PROG_INDEX_PACK,
3054 GOT_PATH_PROG_SEND_PACK,
3056 size_t i;
3058 for (i = 0; i < nitems(helpers); i++) {
3059 if (unveil(helpers[i], "x") == 0)
3060 continue;
3061 return got_error_from_errno2("unveil", helpers[i]);
3064 return NULL;
3067 void
3068 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
3070 if (close(imsg_fds[0]) == -1) {
3071 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3072 _exit(1);
3075 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
3076 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
3077 _exit(1);
3080 closefrom(GOT_IMSG_FD_CHILD + 1);
3082 if (execl(path, path, repo_path, (char *)NULL) == -1) {
3083 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
3084 strerror(errno));
3085 _exit(1);