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 const struct got_error *
372 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
374 const struct got_error *err = NULL;
376 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
377 == -1) {
378 err = got_error_from_errno("imsg_compose TMPFD");
379 close(fd);
380 return err;
383 return flush_imsg(ibuf);
386 const struct got_error *
387 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
389 struct got_imsg_object iobj;
391 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
392 iobj.type = obj->type;
393 iobj.flags = obj->flags;
394 iobj.hdrlen = obj->hdrlen;
395 iobj.size = obj->size;
396 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
397 iobj.pack_offset = obj->pack_offset;
398 iobj.pack_idx = obj->pack_idx;
401 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
402 == -1)
403 return got_error_from_errno("imsg_compose OBJECT");
405 return flush_imsg(ibuf);
408 const struct got_error *
409 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
410 struct got_pathlist_head *have_refs)
412 const struct got_error *err = NULL;
413 struct ibuf *wbuf;
414 size_t len, n_have_refs = 0;
415 struct got_pathlist_entry *pe;
417 len = sizeof(struct got_imsg_fetch_symrefs);
418 TAILQ_FOREACH(pe, have_refs, entry) {
419 struct got_object_id *id = pe->data;
420 len += sizeof(struct got_imsg_fetch_have_ref) +
421 pe->path_len + sizeof(id->sha1);
422 n_have_refs++;
424 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
425 close(fd);
426 return got_error(GOT_ERR_NO_SPACE);
429 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REQUEST, 0, 0, len);
430 if (wbuf == NULL) {
431 close(fd);
432 return got_error_from_errno("imsg_create FETCH_REQUEST");
435 /* Keep in sync with struct got_imsg_fetch_have_refs definition! */
436 if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
437 err = got_error_from_errno("imsg_add FETCH_REQUEST");
438 ibuf_free(wbuf);
439 close(fd);
440 return err;
443 TAILQ_FOREACH(pe, have_refs, entry) {
444 const char *name = pe->path;
445 size_t name_len = pe->path_len;
446 struct got_object_id *id = pe->data;
448 /* Keep in sync with struct got_imsg_fetch_have_ref! */
449 if (imsg_add(wbuf, id->sha1, sizeof(id->sha1)) == -1) {
450 err = got_error_from_errno("imsg_add FETCH_REQUEST");
451 ibuf_free(wbuf);
452 close(fd);
453 return err;
455 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
456 err = got_error_from_errno("imsg_add FETCH_REQUEST");
457 ibuf_free(wbuf);
458 close(fd);
459 return err;
461 if (imsg_add(wbuf, name, name_len) == -1) {
462 err = got_error_from_errno("imsg_add FETCH_REQUEST");
463 ibuf_free(wbuf);
464 close(fd);
465 return err;
469 wbuf->fd = fd;
470 imsg_close(ibuf, wbuf);
471 return flush_imsg(ibuf);
474 const struct got_error *
475 got_privsep_send_fetch_symrefs(struct imsgbuf *ibuf,
476 struct got_pathlist_head *symrefs)
478 const struct got_error *err = NULL;
479 struct ibuf *wbuf;
480 size_t len, nsymrefs = 0;
481 struct got_pathlist_entry *pe;
483 len = sizeof(struct got_imsg_fetch_symrefs);
484 TAILQ_FOREACH(pe, symrefs, entry) {
485 const char *target = pe->data;
486 len += sizeof(struct got_imsg_fetch_symref) +
487 pe->path_len + strlen(target);
488 nsymrefs++;
491 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
492 return got_error(GOT_ERR_NO_SPACE);
494 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
495 if (wbuf == NULL)
496 return got_error_from_errno("imsg_create FETCH_SYMREFS");
498 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
499 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
500 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
501 ibuf_free(wbuf);
502 return err;
505 TAILQ_FOREACH(pe, symrefs, entry) {
506 const char *name = pe->path;
507 size_t name_len = pe->path_len;
508 const char *target = pe->data;
509 size_t target_len = strlen(target);
511 /* Keep in sync with struct got_imsg_fetch_symref definition! */
512 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
513 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
514 ibuf_free(wbuf);
515 return err;
517 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
518 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
519 ibuf_free(wbuf);
520 return err;
522 if (imsg_add(wbuf, name, name_len) == -1) {
523 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
524 ibuf_free(wbuf);
525 return err;
527 if (imsg_add(wbuf, target, target_len) == -1) {
528 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
529 ibuf_free(wbuf);
530 return err;
534 wbuf->fd = -1;
535 imsg_close(ibuf, wbuf);
536 return flush_imsg(ibuf);
539 const struct got_error *
540 got_privsep_send_fetch_ref(struct imsgbuf *ibuf,
541 struct got_object_id *refid, const char *refname)
543 const struct got_error *err = NULL;
544 struct ibuf *wbuf;
545 size_t len, reflen = strlen(refname);
547 len = sizeof(struct got_imsg_fetch_ref) + reflen;
548 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
549 return got_error(GOT_ERR_NO_SPACE);
551 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
552 if (wbuf == NULL)
553 return got_error_from_errno("imsg_create FETCH_REF");
555 /* Keep in sync with struct got_imsg_fetch_ref definition! */
556 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
557 err = got_error_from_errno("imsg_add FETCH_REF");
558 ibuf_free(wbuf);
559 return err;
561 if (imsg_add(wbuf, refname, reflen) == -1) {
562 err = got_error_from_errno("imsg_add FETCH_REF");
563 ibuf_free(wbuf);
564 return err;
567 wbuf->fd = -1;
568 imsg_close(ibuf, wbuf);
569 return flush_imsg(ibuf);
572 const struct got_error *
573 got_privsep_send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg,
574 size_t msglen)
576 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
577 return got_error(GOT_ERR_NO_SPACE);
579 if (msglen == 0)
580 return NULL;
582 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
583 msg, msglen) == -1)
584 return got_error_from_errno(
585 "imsg_compose FETCH_SERVER_PROGRESS");
587 return flush_imsg(ibuf);
590 const struct got_error *
591 got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
593 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
594 hash.sha1, SHA1_DIGEST_LENGTH) == -1)
595 return got_error_from_errno("imsg_compose FETCH");
596 return flush_imsg(ibuf);
600 const struct got_error *
601 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
602 char **refname, struct got_pathlist_head *symrefs, char **server_progress,
603 struct imsgbuf *ibuf)
605 const struct got_error *err = NULL;
606 struct imsg imsg;
607 size_t datalen;
608 struct got_imsg_fetch_symrefs *isymrefs = NULL;
609 size_t n, remain;
610 off_t off;
611 int i;
613 *done = 0;
614 *id = NULL;
615 *refname = NULL;
616 *server_progress = NULL;
618 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
619 if (err)
620 return err;
622 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
623 switch (imsg.hdr.type) {
624 case GOT_IMSG_ERROR:
625 if (datalen < sizeof(struct got_imsg_error)) {
626 err = got_error(GOT_ERR_PRIVSEP_LEN);
627 break;
629 err = recv_imsg_error(&imsg, datalen);
630 break;
631 case GOT_IMSG_FETCH_SYMREFS:
632 if (datalen < sizeof(*isymrefs)) {
633 err = got_error(GOT_ERR_PRIVSEP_LEN);
634 break;
636 if (isymrefs != NULL) {
637 err = got_error(GOT_ERR_PRIVSEP_MSG);
638 break;
640 isymrefs = (struct got_imsg_fetch_symrefs *)imsg.data;
641 off = sizeof(*isymrefs);
642 remain = datalen - off;
643 for (n = 0; n < isymrefs->nsymrefs; n++) {
644 struct got_imsg_fetch_symref *s;
645 char *name, *target;
646 if (remain < sizeof(struct got_imsg_fetch_symref)) {
647 err = got_error(GOT_ERR_PRIVSEP_LEN);
648 goto done;
650 s = (struct got_imsg_fetch_symref *)(imsg.data + off);
651 off += sizeof(*s);
652 remain -= sizeof(*s);
653 if (remain < s->name_len) {
654 err = got_error(GOT_ERR_PRIVSEP_LEN);
655 goto done;
657 name = strndup(imsg.data + off, s->name_len);
658 if (name == NULL) {
659 err = got_error_from_errno("strndup");
660 goto done;
662 off += s->name_len;
663 remain -= s->name_len;
664 if (remain < s->target_len) {
665 err = got_error(GOT_ERR_PRIVSEP_LEN);
666 free(name);
667 goto done;
669 target = strndup(imsg.data + off, s->target_len);
670 if (target == NULL) {
671 err = got_error_from_errno("strndup");
672 free(name);
673 goto done;
675 off += s->target_len;
676 remain -= s->target_len;
677 err = got_pathlist_append(symrefs, name, target);
678 if (err) {
679 free(name);
680 free(target);
681 goto done;
684 break;
685 case GOT_IMSG_FETCH_REF:
686 if (datalen <= SHA1_DIGEST_LENGTH) {
687 err = got_error(GOT_ERR_PRIVSEP_MSG);
688 break;
690 *id = malloc(sizeof(**id));
691 if (*id == NULL) {
692 err = got_error_from_errno("malloc");
693 break;
695 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
696 *refname = strndup(imsg.data + SHA1_DIGEST_LENGTH,
697 datalen - SHA1_DIGEST_LENGTH);
698 if (*refname == NULL) {
699 err = got_error_from_errno("strndup");
700 break;
702 break;
703 case GOT_IMSG_FETCH_SERVER_PROGRESS:
704 if (datalen == 0) {
705 err = got_error(GOT_ERR_PRIVSEP_LEN);
706 break;
708 *server_progress = strndup(imsg.data, datalen);
709 if (*server_progress == NULL) {
710 err = got_error_from_errno("strndup");
711 break;
713 for (i = 0; i < datalen; i++) {
714 if (!isprint((unsigned char)(*server_progress)[i]) &&
715 !isspace((unsigned char)(*server_progress)[i])) {
716 err = got_error(GOT_ERR_PRIVSEP_MSG);
717 free(*server_progress);
718 *server_progress = NULL;
719 goto done;
722 break;
723 case GOT_IMSG_FETCH_DONE:
724 *id = malloc(sizeof(**id));
725 if (*id == NULL) {
726 err = got_error_from_errno("malloc");
727 break;
729 if (datalen != SHA1_DIGEST_LENGTH) {
730 err = got_error(GOT_ERR_PRIVSEP_MSG);
731 break;
733 memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
734 *done = 1;
735 break;
736 default:
737 err = got_error(GOT_ERR_PRIVSEP_MSG);
738 break;
740 done:
741 if (err) {
742 free(*id);
743 *id = NULL;
744 free(*refname);
745 *refname = NULL;
747 imsg_free(&imsg);
748 return err;
751 const struct got_error *
752 got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
754 const struct got_error *err = NULL;
756 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
757 hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
758 err = got_error_from_errno("imsg_compose INDEX_REQUEST");
759 close(fd);
760 return err;
762 return flush_imsg(ibuf);
765 const struct got_error *
766 got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
768 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
769 return got_error_from_errno("imsg_compose FETCH");
770 return flush_imsg(ibuf);
773 const struct got_error *
774 got_privsep_wait_index_pack_done(struct imsgbuf *ibuf)
776 const struct got_error *err = NULL;
777 struct imsg imsg;
779 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
780 if (err)
781 return err;
782 if (imsg.hdr.type == GOT_IMSG_IDXPACK_DONE)
783 return NULL;
784 else
785 return got_error(GOT_ERR_PRIVSEP_MSG);
786 imsg_free(&imsg);
789 const struct got_error *
790 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
791 struct imsgbuf *ibuf)
793 const struct got_error *err = NULL;
794 struct got_imsg_object *iobj;
795 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
797 if (datalen != sizeof(*iobj))
798 return got_error(GOT_ERR_PRIVSEP_LEN);
799 iobj = imsg->data;
801 *obj = calloc(1, sizeof(**obj));
802 if (*obj == NULL)
803 return got_error_from_errno("calloc");
805 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
806 (*obj)->type = iobj->type;
807 (*obj)->flags = iobj->flags;
808 (*obj)->hdrlen = iobj->hdrlen;
809 (*obj)->size = iobj->size;
810 /* path_packfile is handled by caller */
811 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
812 (*obj)->pack_offset = iobj->pack_offset;
813 (*obj)->pack_idx = iobj->pack_idx;
816 return err;
819 const struct got_error *
820 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
822 const struct got_error *err = NULL;
823 struct imsg imsg;
824 const size_t min_datalen =
825 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
827 *obj = NULL;
829 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
830 if (err)
831 return err;
833 switch (imsg.hdr.type) {
834 case GOT_IMSG_OBJECT:
835 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
836 break;
837 default:
838 err = got_error(GOT_ERR_PRIVSEP_MSG);
839 break;
842 imsg_free(&imsg);
844 return err;
847 static const struct got_error *
848 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
849 size_t logmsg_len)
851 const struct got_error *err = NULL;
852 size_t offset, remain;
854 offset = 0;
855 remain = logmsg_len;
856 while (remain > 0) {
857 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
859 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
860 commit->logmsg + offset, n) == -1) {
861 err = got_error_from_errno("imsg_compose "
862 "COMMIT_LOGMSG");
863 break;
866 err = flush_imsg(ibuf);
867 if (err)
868 break;
870 offset += n;
871 remain -= n;
874 return err;
877 const struct got_error *
878 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
880 const struct got_error *err = NULL;
881 struct got_imsg_commit_object *icommit;
882 uint8_t *buf;
883 size_t len, total;
884 struct got_object_qid *qid;
885 size_t author_len = strlen(commit->author);
886 size_t committer_len = strlen(commit->committer);
887 size_t logmsg_len = strlen(commit->logmsg);
889 total = sizeof(*icommit) + author_len + committer_len +
890 commit->nparents * SHA1_DIGEST_LENGTH;
892 buf = malloc(total);
893 if (buf == NULL)
894 return got_error_from_errno("malloc");
896 icommit = (struct got_imsg_commit_object *)buf;
897 memcpy(icommit->tree_id, commit->tree_id->sha1,
898 sizeof(icommit->tree_id));
899 icommit->author_len = author_len;
900 icommit->author_time = commit->author_time;
901 icommit->author_gmtoff = commit->author_gmtoff;
902 icommit->committer_len = committer_len;
903 icommit->committer_time = commit->committer_time;
904 icommit->committer_gmtoff = commit->committer_gmtoff;
905 icommit->logmsg_len = logmsg_len;
906 icommit->nparents = commit->nparents;
908 len = sizeof(*icommit);
909 memcpy(buf + len, commit->author, author_len);
910 len += author_len;
911 memcpy(buf + len, commit->committer, committer_len);
912 len += committer_len;
913 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
914 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
915 len += SHA1_DIGEST_LENGTH;
918 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
919 err = got_error_from_errno("imsg_compose COMMIT");
920 goto done;
923 if (logmsg_len == 0 ||
924 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
925 err = flush_imsg(ibuf);
926 if (err)
927 goto done;
929 err = send_commit_logmsg(ibuf, commit, logmsg_len);
930 done:
931 free(buf);
932 return err;
935 static const struct got_error *
936 get_commit_from_imsg(struct got_commit_object **commit,
937 struct imsg *imsg, size_t datalen, struct imsgbuf *ibuf)
939 const struct got_error *err = NULL;
940 struct got_imsg_commit_object *icommit;
941 size_t len = 0;
942 int i;
944 if (datalen < sizeof(*icommit))
945 return got_error(GOT_ERR_PRIVSEP_LEN);
947 icommit = imsg->data;
948 if (datalen != sizeof(*icommit) + icommit->author_len +
949 icommit->committer_len +
950 icommit->nparents * SHA1_DIGEST_LENGTH)
951 return got_error(GOT_ERR_PRIVSEP_LEN);
953 if (icommit->nparents < 0)
954 return got_error(GOT_ERR_PRIVSEP_LEN);
956 len += sizeof(*icommit);
958 *commit = got_object_commit_alloc_partial();
959 if (*commit == NULL)
960 return got_error_from_errno(
961 "got_object_commit_alloc_partial");
963 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
964 SHA1_DIGEST_LENGTH);
965 (*commit)->author_time = icommit->author_time;
966 (*commit)->author_gmtoff = icommit->author_gmtoff;
967 (*commit)->committer_time = icommit->committer_time;
968 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
970 if (icommit->author_len == 0) {
971 (*commit)->author = strdup("");
972 if ((*commit)->author == NULL) {
973 err = got_error_from_errno("strdup");
974 goto done;
976 } else {
977 (*commit)->author = malloc(icommit->author_len + 1);
978 if ((*commit)->author == NULL) {
979 err = got_error_from_errno("malloc");
980 goto done;
982 memcpy((*commit)->author, imsg->data + len,
983 icommit->author_len);
984 (*commit)->author[icommit->author_len] = '\0';
986 len += icommit->author_len;
988 if (icommit->committer_len == 0) {
989 (*commit)->committer = strdup("");
990 if ((*commit)->committer == NULL) {
991 err = got_error_from_errno("strdup");
992 goto done;
994 } else {
995 (*commit)->committer =
996 malloc(icommit->committer_len + 1);
997 if ((*commit)->committer == NULL) {
998 err = got_error_from_errno("malloc");
999 goto done;
1001 memcpy((*commit)->committer, imsg->data + len,
1002 icommit->committer_len);
1003 (*commit)->committer[icommit->committer_len] = '\0';
1005 len += icommit->committer_len;
1007 if (icommit->logmsg_len == 0) {
1008 (*commit)->logmsg = strdup("");
1009 if ((*commit)->logmsg == NULL) {
1010 err = got_error_from_errno("strdup");
1011 goto done;
1013 } else {
1014 size_t offset = 0, remain = icommit->logmsg_len;
1016 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
1017 if ((*commit)->logmsg == NULL) {
1018 err = got_error_from_errno("malloc");
1019 goto done;
1021 while (remain > 0) {
1022 struct imsg imsg_log;
1023 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1024 remain);
1026 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1027 if (err)
1028 goto done;
1030 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG) {
1031 err = got_error(GOT_ERR_PRIVSEP_MSG);
1032 goto done;
1035 memcpy((*commit)->logmsg + offset,
1036 imsg_log.data, n);
1037 imsg_free(&imsg_log);
1038 offset += n;
1039 remain -= n;
1041 (*commit)->logmsg[icommit->logmsg_len] = '\0';
1044 for (i = 0; i < icommit->nparents; i++) {
1045 struct got_object_qid *qid;
1047 err = got_object_qid_alloc_partial(&qid);
1048 if (err)
1049 break;
1050 memcpy(qid->id, imsg->data + len +
1051 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
1052 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
1053 (*commit)->nparents++;
1055 done:
1056 if (err) {
1057 got_object_commit_close(*commit);
1058 *commit = NULL;
1060 return err;
1063 const struct got_error *
1064 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
1066 const struct got_error *err = NULL;
1067 struct imsg imsg;
1068 size_t datalen;
1069 const size_t min_datalen =
1070 MIN(sizeof(struct got_imsg_error),
1071 sizeof(struct got_imsg_commit_object));
1073 *commit = NULL;
1075 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1076 if (err)
1077 return err;
1079 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1081 switch (imsg.hdr.type) {
1082 case GOT_IMSG_COMMIT:
1083 err = get_commit_from_imsg(commit, &imsg, datalen, ibuf);
1084 break;
1085 default:
1086 err = got_error(GOT_ERR_PRIVSEP_MSG);
1087 break;
1090 imsg_free(&imsg);
1092 return err;
1095 const struct got_error *
1096 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
1097 int nentries)
1099 const struct got_error *err = NULL;
1100 struct got_imsg_tree_object itree;
1101 struct got_pathlist_entry *pe;
1102 size_t totlen;
1103 int nimsg; /* number of imsg queued in ibuf */
1105 itree.nentries = nentries;
1106 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
1107 == -1)
1108 return got_error_from_errno("imsg_compose TREE");
1110 totlen = sizeof(itree);
1111 nimsg = 1;
1112 TAILQ_FOREACH(pe, entries, entry) {
1113 const char *name = pe->path;
1114 struct got_parsed_tree_entry *pte = pe->data;
1115 struct ibuf *wbuf;
1116 size_t namelen = strlen(name);
1117 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
1119 if (len > MAX_IMSGSIZE)
1120 return got_error(GOT_ERR_NO_SPACE);
1122 nimsg++;
1123 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
1124 err = flush_imsg(ibuf);
1125 if (err)
1126 return err;
1127 nimsg = 0;
1130 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
1131 if (wbuf == NULL)
1132 return got_error_from_errno("imsg_create TREE_ENTRY");
1134 /* Keep in sync with struct got_imsg_tree_object definition! */
1135 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
1136 err = got_error_from_errno("imsg_add TREE_ENTRY");
1137 ibuf_free(wbuf);
1138 return err;
1140 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
1141 err = got_error_from_errno("imsg_add TREE_ENTRY");
1142 ibuf_free(wbuf);
1143 return err;
1146 if (imsg_add(wbuf, name, namelen) == -1) {
1147 err = got_error_from_errno("imsg_add TREE_ENTRY");
1148 ibuf_free(wbuf);
1149 return err;
1152 wbuf->fd = -1;
1153 imsg_close(ibuf, wbuf);
1155 totlen += len;
1158 return flush_imsg(ibuf);
1161 const struct got_error *
1162 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
1164 const struct got_error *err = NULL;
1165 const size_t min_datalen =
1166 MIN(sizeof(struct got_imsg_error),
1167 sizeof(struct got_imsg_tree_object));
1168 struct got_imsg_tree_object *itree;
1169 int nentries = 0;
1171 *tree = NULL;
1172 get_more:
1173 err = read_imsg(ibuf);
1174 if (err)
1175 goto done;
1177 for (;;) {
1178 struct imsg imsg;
1179 size_t n;
1180 size_t datalen;
1181 struct got_imsg_tree_entry *ite;
1182 struct got_tree_entry *te = NULL;
1184 n = imsg_get(ibuf, &imsg);
1185 if (n == 0) {
1186 if (*tree && (*tree)->nentries != nentries)
1187 goto get_more;
1188 break;
1191 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
1192 return got_error(GOT_ERR_PRIVSEP_LEN);
1194 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1196 switch (imsg.hdr.type) {
1197 case GOT_IMSG_ERROR:
1198 err = recv_imsg_error(&imsg, datalen);
1199 break;
1200 case GOT_IMSG_TREE:
1201 /* This message should only appear once. */
1202 if (*tree != NULL) {
1203 err = got_error(GOT_ERR_PRIVSEP_MSG);
1204 break;
1206 if (datalen != sizeof(*itree)) {
1207 err = got_error(GOT_ERR_PRIVSEP_LEN);
1208 break;
1210 itree = imsg.data;
1211 *tree = malloc(sizeof(**tree));
1212 if (*tree == NULL) {
1213 err = got_error_from_errno("malloc");
1214 break;
1216 (*tree)->entries = calloc(itree->nentries,
1217 sizeof(struct got_tree_entry));
1218 if ((*tree)->entries == NULL) {
1219 err = got_error_from_errno("malloc");
1220 break;
1222 (*tree)->nentries = itree->nentries;
1223 (*tree)->refcnt = 0;
1224 break;
1225 case GOT_IMSG_TREE_ENTRY:
1226 /* This message should be preceeded by GOT_IMSG_TREE. */
1227 if (*tree == NULL) {
1228 err = got_error(GOT_ERR_PRIVSEP_MSG);
1229 break;
1231 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
1232 err = got_error(GOT_ERR_PRIVSEP_LEN);
1233 break;
1236 /* Remaining data contains the entry's name. */
1237 datalen -= sizeof(*ite);
1238 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
1239 err = got_error(GOT_ERR_PRIVSEP_LEN);
1240 break;
1242 ite = imsg.data;
1244 if (datalen + 1 > sizeof(te->name)) {
1245 err = got_error(GOT_ERR_NO_SPACE);
1246 break;
1248 te = &(*tree)->entries[nentries];
1249 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
1250 te->name[datalen] = '\0';
1252 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
1253 te->mode = ite->mode;
1254 te->idx = nentries;
1255 nentries++;
1256 break;
1257 default:
1258 err = got_error(GOT_ERR_PRIVSEP_MSG);
1259 break;
1262 imsg_free(&imsg);
1264 done:
1265 if (*tree && (*tree)->nentries != nentries) {
1266 if (err == NULL)
1267 err = got_error(GOT_ERR_PRIVSEP_LEN);
1268 got_object_tree_close(*tree);
1269 *tree = NULL;
1272 return err;
1275 const struct got_error *
1276 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
1277 const uint8_t *data)
1279 struct got_imsg_blob iblob;
1281 iblob.size = size;
1282 iblob.hdrlen = hdrlen;
1284 if (data) {
1285 uint8_t *buf;
1287 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
1288 return got_error(GOT_ERR_NO_SPACE);
1290 buf = malloc(sizeof(iblob) + size);
1291 if (buf == NULL)
1292 return got_error_from_errno("malloc");
1294 memcpy(buf, &iblob, sizeof(iblob));
1295 memcpy(buf + sizeof(iblob), data, size);
1296 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
1297 sizeof(iblob) + size) == -1) {
1298 free(buf);
1299 return got_error_from_errno("imsg_compose BLOB");
1301 free(buf);
1302 } else {
1303 /* Data has already been written to file descriptor. */
1304 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
1305 sizeof(iblob)) == -1)
1306 return got_error_from_errno("imsg_compose BLOB");
1310 return flush_imsg(ibuf);
1313 const struct got_error *
1314 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1315 struct imsgbuf *ibuf)
1317 const struct got_error *err = NULL;
1318 struct imsg imsg;
1319 struct got_imsg_blob *iblob;
1320 size_t datalen;
1322 *outbuf = NULL;
1324 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1325 if (err)
1326 return err;
1328 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1330 switch (imsg.hdr.type) {
1331 case GOT_IMSG_BLOB:
1332 if (datalen < sizeof(*iblob)) {
1333 err = got_error(GOT_ERR_PRIVSEP_LEN);
1334 break;
1336 iblob = imsg.data;
1337 *size = iblob->size;
1338 *hdrlen = iblob->hdrlen;
1340 if (datalen == sizeof(*iblob)) {
1341 /* Data has been written to file descriptor. */
1342 break;
1345 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
1346 err = got_error(GOT_ERR_PRIVSEP_LEN);
1347 break;
1350 *outbuf = malloc(*size);
1351 if (*outbuf == NULL) {
1352 err = got_error_from_errno("malloc");
1353 break;
1355 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
1356 break;
1357 default:
1358 err = got_error(GOT_ERR_PRIVSEP_MSG);
1359 break;
1362 imsg_free(&imsg);
1364 return err;
1367 static const struct got_error *
1368 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
1370 const struct got_error *err = NULL;
1371 size_t offset, remain;
1373 offset = 0;
1374 remain = tagmsg_len;
1375 while (remain > 0) {
1376 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
1378 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
1379 tag->tagmsg + offset, n) == -1) {
1380 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
1381 break;
1384 err = flush_imsg(ibuf);
1385 if (err)
1386 break;
1388 offset += n;
1389 remain -= n;
1392 return err;
1395 const struct got_error *
1396 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1398 const struct got_error *err = NULL;
1399 struct got_imsg_tag_object *itag;
1400 uint8_t *buf;
1401 size_t len, total;
1402 size_t tag_len = strlen(tag->tag);
1403 size_t tagger_len = strlen(tag->tagger);
1404 size_t tagmsg_len = strlen(tag->tagmsg);
1406 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1408 buf = malloc(total);
1409 if (buf == NULL)
1410 return got_error_from_errno("malloc");
1412 itag = (struct got_imsg_tag_object *)buf;
1413 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1414 itag->obj_type = tag->obj_type;
1415 itag->tag_len = tag_len;
1416 itag->tagger_len = tagger_len;
1417 itag->tagger_time = tag->tagger_time;
1418 itag->tagger_gmtoff = tag->tagger_gmtoff;
1419 itag->tagmsg_len = tagmsg_len;
1421 len = sizeof(*itag);
1422 memcpy(buf + len, tag->tag, tag_len);
1423 len += tag_len;
1424 memcpy(buf + len, tag->tagger, tagger_len);
1425 len += tagger_len;
1427 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1428 err = got_error_from_errno("imsg_compose TAG");
1429 goto done;
1432 if (tagmsg_len == 0 ||
1433 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1434 err = flush_imsg(ibuf);
1435 if (err)
1436 goto done;
1438 err = send_tagmsg(ibuf, tag, tagmsg_len);
1439 done:
1440 free(buf);
1441 return err;
1444 const struct got_error *
1445 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1447 const struct got_error *err = NULL;
1448 struct imsg imsg;
1449 struct got_imsg_tag_object *itag;
1450 size_t len, datalen;
1451 const size_t min_datalen =
1452 MIN(sizeof(struct got_imsg_error),
1453 sizeof(struct got_imsg_tag_object));
1455 *tag = NULL;
1457 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1458 if (err)
1459 return err;
1461 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1462 len = 0;
1464 switch (imsg.hdr.type) {
1465 case GOT_IMSG_TAG:
1466 if (datalen < sizeof(*itag)) {
1467 err = got_error(GOT_ERR_PRIVSEP_LEN);
1468 break;
1470 itag = imsg.data;
1471 if (datalen != sizeof(*itag) + itag->tag_len +
1472 itag->tagger_len) {
1473 err = got_error(GOT_ERR_PRIVSEP_LEN);
1474 break;
1476 len += sizeof(*itag);
1478 *tag = calloc(1, sizeof(**tag));
1479 if (*tag == NULL) {
1480 err = got_error_from_errno("calloc");
1481 break;
1484 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1486 if (itag->tag_len == 0) {
1487 (*tag)->tag = strdup("");
1488 if ((*tag)->tag == NULL) {
1489 err = got_error_from_errno("strdup");
1490 break;
1492 } else {
1493 (*tag)->tag = malloc(itag->tag_len + 1);
1494 if ((*tag)->tag == NULL) {
1495 err = got_error_from_errno("malloc");
1496 break;
1498 memcpy((*tag)->tag, imsg.data + len,
1499 itag->tag_len);
1500 (*tag)->tag[itag->tag_len] = '\0';
1502 len += itag->tag_len;
1504 (*tag)->obj_type = itag->obj_type;
1505 (*tag)->tagger_time = itag->tagger_time;
1506 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1508 if (itag->tagger_len == 0) {
1509 (*tag)->tagger = strdup("");
1510 if ((*tag)->tagger == NULL) {
1511 err = got_error_from_errno("strdup");
1512 break;
1514 } else {
1515 (*tag)->tagger = malloc(itag->tagger_len + 1);
1516 if ((*tag)->tagger == NULL) {
1517 err = got_error_from_errno("malloc");
1518 break;
1520 memcpy((*tag)->tagger, imsg.data + len,
1521 itag->tagger_len);
1522 (*tag)->tagger[itag->tagger_len] = '\0';
1524 len += itag->tagger_len;
1526 if (itag->tagmsg_len == 0) {
1527 (*tag)->tagmsg = strdup("");
1528 if ((*tag)->tagmsg == NULL) {
1529 err = got_error_from_errno("strdup");
1530 break;
1532 } else {
1533 size_t offset = 0, remain = itag->tagmsg_len;
1535 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1536 if ((*tag)->tagmsg == NULL) {
1537 err = got_error_from_errno("malloc");
1538 break;
1540 while (remain > 0) {
1541 struct imsg imsg_log;
1542 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1543 remain);
1545 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1546 if (err)
1547 return err;
1549 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1550 return got_error(GOT_ERR_PRIVSEP_MSG);
1552 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1553 n);
1554 imsg_free(&imsg_log);
1555 offset += n;
1556 remain -= n;
1558 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1561 break;
1562 default:
1563 err = got_error(GOT_ERR_PRIVSEP_MSG);
1564 break;
1567 imsg_free(&imsg);
1569 return err;
1572 const struct got_error *
1573 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1574 struct got_packidx *packidx)
1576 const struct got_error *err = NULL;
1577 struct got_imsg_packidx ipackidx;
1578 struct got_imsg_pack ipack;
1579 int fd;
1581 ipackidx.len = packidx->len;
1582 fd = dup(packidx->fd);
1583 if (fd == -1)
1584 return got_error_from_errno("dup");
1586 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1587 sizeof(ipackidx)) == -1) {
1588 err = got_error_from_errno("imsg_compose PACKIDX");
1589 close(fd);
1590 return err;
1593 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1594 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1595 return got_error(GOT_ERR_NO_SPACE);
1596 ipack.filesize = pack->filesize;
1598 fd = dup(pack->fd);
1599 if (fd == -1)
1600 return got_error_from_errno("dup");
1602 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1603 == -1) {
1604 err = got_error_from_errno("imsg_compose PACK");
1605 close(fd);
1606 return err;
1609 return flush_imsg(ibuf);
1612 const struct got_error *
1613 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1614 struct got_object_id *id)
1616 struct got_imsg_packed_object iobj;
1618 iobj.idx = idx;
1619 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1621 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1622 &iobj, sizeof(iobj)) == -1)
1623 return got_error_from_errno("imsg_compose "
1624 "PACKED_OBJECT_REQUEST");
1626 return flush_imsg(ibuf);
1629 const struct got_error *
1630 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1632 const struct got_error *err = NULL;
1634 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1635 NULL, 0) == -1) {
1636 err = got_error_from_errno("imsg_compose "
1637 "GITCONFIG_PARSE_REQUEST");
1638 close(fd);
1639 return err;
1642 return flush_imsg(ibuf);
1645 const struct got_error *
1646 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1648 if (imsg_compose(ibuf,
1649 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1650 NULL, 0) == -1)
1651 return got_error_from_errno("imsg_compose "
1652 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1654 return flush_imsg(ibuf);
1657 const struct got_error *
1658 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1660 if (imsg_compose(ibuf,
1661 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1662 return got_error_from_errno("imsg_compose "
1663 "GITCONFIG_AUTHOR_NAME_REQUEST");
1665 return flush_imsg(ibuf);
1668 const struct got_error *
1669 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1671 if (imsg_compose(ibuf,
1672 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1673 return got_error_from_errno("imsg_compose "
1674 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1676 return flush_imsg(ibuf);
1679 const struct got_error *
1680 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1682 if (imsg_compose(ibuf,
1683 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1684 return got_error_from_errno("imsg_compose "
1685 "GITCONFIG_REMOTE_REQUEST");
1687 return flush_imsg(ibuf);
1690 const struct got_error *
1691 got_privsep_send_gitconfig_owner_req(struct imsgbuf *ibuf)
1693 if (imsg_compose(ibuf,
1694 GOT_IMSG_GITCONFIG_OWNER_REQUEST, 0, 0, -1, NULL, 0) == -1)
1695 return got_error_from_errno("imsg_compose "
1696 "GITCONFIG_OWNER_REQUEST");
1698 return flush_imsg(ibuf);
1701 const struct got_error *
1702 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1704 size_t len = value ? strlen(value) + 1 : 0;
1706 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1707 value, len) == -1)
1708 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1710 return flush_imsg(ibuf);
1713 const struct got_error *
1714 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1716 const struct got_error *err = NULL;
1717 struct imsg imsg;
1718 size_t datalen;
1719 const size_t min_datalen = 0;
1721 *str = NULL;
1723 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1724 if (err)
1725 return err;
1726 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1728 switch (imsg.hdr.type) {
1729 case GOT_IMSG_GITCONFIG_STR_VAL:
1730 if (datalen == 0)
1731 break;
1732 *str = malloc(datalen);
1733 if (*str == NULL) {
1734 err = got_error_from_errno("malloc");
1735 break;
1737 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1738 err = got_error(GOT_ERR_NO_SPACE);
1739 break;
1740 default:
1741 err = got_error(GOT_ERR_PRIVSEP_MSG);
1742 break;
1745 imsg_free(&imsg);
1746 return err;
1749 const struct got_error *
1750 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1752 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1753 &value, sizeof(value)) == -1)
1754 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1756 return flush_imsg(ibuf);
1759 const struct got_error *
1760 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1762 const struct got_error *err = NULL;
1763 struct imsg imsg;
1764 size_t datalen;
1765 const size_t min_datalen =
1766 MIN(sizeof(struct got_imsg_error), sizeof(int));
1768 *val = 0;
1770 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1771 if (err)
1772 return err;
1773 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1775 switch (imsg.hdr.type) {
1776 case GOT_IMSG_GITCONFIG_INT_VAL:
1777 if (datalen != sizeof(*val)) {
1778 err = got_error(GOT_ERR_PRIVSEP_LEN);
1779 break;
1781 memcpy(val, imsg.data, sizeof(*val));
1782 break;
1783 default:
1784 err = got_error(GOT_ERR_PRIVSEP_MSG);
1785 break;
1788 imsg_free(&imsg);
1789 return err;
1792 const struct got_error *
1793 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1794 struct got_remote_repo *remotes, int nremotes)
1796 const struct got_error *err = NULL;
1797 struct got_imsg_remotes iremotes;
1798 int i;
1800 iremotes.nremotes = nremotes;
1801 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1802 &iremotes, sizeof(iremotes)) == -1)
1803 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1805 err = flush_imsg(ibuf);
1806 imsg_clear(ibuf);
1807 if (err)
1808 return err;
1810 for (i = 0; i < nremotes; i++) {
1811 struct got_imsg_remote iremote;
1812 size_t len = sizeof(iremote);
1813 struct ibuf *wbuf;
1815 iremote.name_len = strlen(remotes[i].name);
1816 len += iremote.name_len;
1817 iremote.url_len = strlen(remotes[i].url);
1818 len += iremote.url_len;
1820 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1821 if (wbuf == NULL)
1822 return got_error_from_errno(
1823 "imsg_create GITCONFIG_REMOTE");
1825 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1826 err = got_error_from_errno(
1827 "imsg_add GIITCONFIG_REMOTE");
1828 ibuf_free(wbuf);
1829 return err;
1832 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1833 err = got_error_from_errno(
1834 "imsg_add GIITCONFIG_REMOTE");
1835 ibuf_free(wbuf);
1836 return err;
1838 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1839 err = got_error_from_errno(
1840 "imsg_add GIITCONFIG_REMOTE");
1841 ibuf_free(wbuf);
1842 return err;
1845 wbuf->fd = -1;
1846 imsg_close(ibuf, wbuf);
1847 err = flush_imsg(ibuf);
1848 if (err)
1849 return err;
1852 return NULL;
1855 const struct got_error *
1856 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1857 int *nremotes, struct imsgbuf *ibuf)
1859 const struct got_error *err = NULL;
1860 struct imsg imsg;
1861 size_t datalen;
1862 struct got_imsg_remotes iremotes;
1863 struct got_imsg_remote iremote;
1865 *remotes = NULL;
1866 *nremotes = 0;
1867 iremotes.nremotes = 0;
1869 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1870 if (err)
1871 return err;
1872 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1874 switch (imsg.hdr.type) {
1875 case GOT_IMSG_GITCONFIG_REMOTES:
1876 if (datalen != sizeof(iremotes)) {
1877 err = got_error(GOT_ERR_PRIVSEP_LEN);
1878 break;
1880 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1881 if (iremotes.nremotes == 0) {
1882 imsg_free(&imsg);
1883 return NULL;
1885 break;
1886 default:
1887 imsg_free(&imsg);
1888 return got_error(GOT_ERR_PRIVSEP_MSG);
1891 imsg_free(&imsg);
1893 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1894 if (*remotes == NULL)
1895 return got_error_from_errno("recallocarray");
1897 while (*nremotes < iremotes.nremotes) {
1898 struct got_remote_repo *remote;
1900 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1901 if (err)
1902 break;
1903 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1905 switch (imsg.hdr.type) {
1906 case GOT_IMSG_GITCONFIG_REMOTE:
1907 remote = &(*remotes)[*nremotes];
1908 if (datalen < sizeof(iremote)) {
1909 err = got_error(GOT_ERR_PRIVSEP_LEN);
1910 break;
1912 memcpy(&iremote, imsg.data, sizeof(iremote));
1913 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1914 (sizeof(iremote) + iremote.name_len +
1915 iremote.url_len) > datalen) {
1916 err = got_error(GOT_ERR_PRIVSEP_LEN);
1917 break;
1919 remote->name = strndup(imsg.data + sizeof(iremote),
1920 iremote.name_len);
1921 if (remote->name == NULL) {
1922 err = got_error_from_errno("strndup");
1923 break;
1925 remote->url = strndup(imsg.data + sizeof(iremote) +
1926 iremote.name_len, iremote.url_len);
1927 if (remote->url == NULL) {
1928 err = got_error_from_errno("strndup");
1929 free(remote->name);
1930 break;
1932 (*nremotes)++;
1933 break;
1934 default:
1935 err = got_error(GOT_ERR_PRIVSEP_MSG);
1936 break;
1939 imsg_free(&imsg);
1940 if (err)
1941 break;
1944 if (err) {
1945 int i;
1946 for (i = 0; i < *nremotes; i++) {
1947 free((*remotes)[i].name);
1948 free((*remotes)[i].url);
1950 free(*remotes);
1951 *remotes = NULL;
1952 *nremotes = 0;
1954 return err;
1957 const struct got_error *
1958 got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
1959 struct got_object_id *id, int idx, const char *path)
1961 const struct got_error *err = NULL;
1962 struct ibuf *wbuf;
1963 size_t path_len = strlen(path) + 1;
1965 wbuf = imsg_create(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_REQUEST, 0, 0,
1966 sizeof(struct got_imsg_commit_traversal_request) + path_len);
1967 if (wbuf == NULL)
1968 return got_error_from_errno(
1969 "imsg_create COMMIT_TRAVERSAL_REQUEST");
1970 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
1971 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1972 ibuf_free(wbuf);
1973 return err;
1975 if (imsg_add(wbuf, &idx, sizeof(idx)) == -1) {
1976 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1977 ibuf_free(wbuf);
1978 return err;
1980 if (imsg_add(wbuf, path, path_len) == -1) {
1981 err = got_error_from_errno("imsg_add COMMIT_TRAVERSAL_REQUEST");
1982 ibuf_free(wbuf);
1983 return err;
1986 wbuf->fd = -1;
1987 imsg_close(ibuf, wbuf);
1989 return flush_imsg(ibuf);
1992 const struct got_error *
1993 got_privsep_send_traversed_commits(struct got_object_id *commit_ids,
1994 size_t ncommits, struct imsgbuf *ibuf)
1996 const struct got_error *err;
1997 struct ibuf *wbuf;
1998 int i;
2000 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
2001 sizeof(struct got_imsg_traversed_commits) +
2002 ncommits * SHA1_DIGEST_LENGTH);
2003 if (wbuf == NULL)
2004 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
2006 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
2007 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
2008 ibuf_free(wbuf);
2009 return err;
2011 for (i = 0; i < ncommits; i++) {
2012 struct got_object_id *id = &commit_ids[i];
2013 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
2014 err = got_error_from_errno(
2015 "imsg_add TRAVERSED_COMMITS");
2016 ibuf_free(wbuf);
2017 return err;
2021 wbuf->fd = -1;
2022 imsg_close(ibuf, wbuf);
2024 return flush_imsg(ibuf);
2027 const struct got_error *
2028 got_privsep_recv_traversed_commits(struct got_commit_object **changed_commit,
2029 struct got_object_id **changed_commit_id,
2030 struct got_object_id_queue *commit_ids, struct imsgbuf *ibuf)
2032 const struct got_error *err = NULL;
2033 struct imsg imsg;
2034 struct got_imsg_traversed_commits *icommits;
2035 size_t datalen;
2036 int i, done = 0;
2038 *changed_commit = NULL;
2039 *changed_commit_id = NULL;
2041 while (!done) {
2042 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
2043 if (err)
2044 return err;
2046 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
2047 switch (imsg.hdr.type) {
2048 case GOT_IMSG_TRAVERSED_COMMITS:
2049 icommits = imsg.data;
2050 if (datalen != sizeof(*icommits) +
2051 icommits->ncommits * SHA1_DIGEST_LENGTH) {
2052 err = got_error(GOT_ERR_PRIVSEP_LEN);
2053 break;
2055 for (i = 0; i < icommits->ncommits; i++) {
2056 struct got_object_qid *qid;
2057 uint8_t *sha1 = (uint8_t *)imsg.data +
2058 sizeof(*icommits) + i * SHA1_DIGEST_LENGTH;
2059 err = got_object_qid_alloc_partial(&qid);
2060 if (err)
2061 break;
2062 memcpy(qid->id->sha1, sha1, SHA1_DIGEST_LENGTH);
2063 SIMPLEQ_INSERT_TAIL(commit_ids, qid, entry);
2065 /* The last commit may contain a change. */
2066 if (i == icommits->ncommits - 1) {
2067 *changed_commit_id =
2068 got_object_id_dup(qid->id);
2069 if (*changed_commit_id == NULL) {
2070 err = got_error_from_errno(
2071 "got_object_id_dup");
2072 break;
2076 break;
2077 case GOT_IMSG_COMMIT:
2078 if (*changed_commit_id == NULL) {
2079 err = got_error(GOT_ERR_PRIVSEP_MSG);
2080 break;
2082 err = get_commit_from_imsg(changed_commit, &imsg,
2083 datalen, ibuf);
2084 break;
2085 case GOT_IMSG_COMMIT_TRAVERSAL_DONE:
2086 done = 1;
2087 break;
2088 default:
2089 err = got_error(GOT_ERR_PRIVSEP_MSG);
2090 break;
2093 imsg_free(&imsg);
2094 if (err)
2095 break;
2098 if (err)
2099 got_object_id_queue_free(commit_ids);
2100 return err;
2103 const struct got_error *
2104 got_privsep_send_commit_traversal_done(struct imsgbuf *ibuf)
2106 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
2107 NULL, 0) == -1)
2108 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
2110 return flush_imsg(ibuf);
2113 const struct got_error *
2114 got_privsep_unveil_exec_helpers(void)
2116 const char *helpers[] = {
2117 GOT_PATH_PROG_READ_PACK,
2118 GOT_PATH_PROG_READ_OBJECT,
2119 GOT_PATH_PROG_READ_COMMIT,
2120 GOT_PATH_PROG_READ_TREE,
2121 GOT_PATH_PROG_READ_BLOB,
2122 GOT_PATH_PROG_READ_TAG,
2123 GOT_PATH_PROG_READ_GITCONFIG,
2125 int i;
2127 for (i = 0; i < nitems(helpers); i++) {
2128 if (unveil(helpers[i], "x") == 0)
2129 continue;
2130 return got_error_from_errno2("unveil", helpers[i]);
2133 return NULL;
2136 void
2137 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
2139 if (close(imsg_fds[0]) != 0) {
2140 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2141 _exit(1);
2144 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
2145 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2146 _exit(1);
2148 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
2149 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
2150 _exit(1);
2153 if (execl(path, path, repo_path, (char *)NULL) == -1) {
2154 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
2155 strerror(errno));
2156 _exit(1);