Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/syslimits.h>
21 #include <sys/wait.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <stdint.h>
28 #include <poll.h>
29 #include <imsg.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <time.h>
34 #include "got_object.h"
35 #include "got_error.h"
36 #include "got_path.h"
37 #include "got_repository.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_inflate.h"
42 #include "got_lib_object.h"
43 #include "got_lib_object_parse.h"
44 #include "got_lib_privsep.h"
45 #include "got_lib_pack.h"
47 #ifndef MIN
48 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 #endif
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 static const struct got_error *
56 poll_fd(int fd, int events, int timeout)
57 {
58 struct pollfd pfd[1];
59 int n;
61 pfd[0].fd = fd;
62 pfd[0].events = events;
64 n = poll(pfd, 1, timeout);
65 if (n == -1)
66 return got_error_from_errno("poll");
67 if (n == 0)
68 return got_error(GOT_ERR_TIMEOUT);
69 if (pfd[0].revents & (POLLERR | POLLNVAL))
70 return got_error_from_errno("poll error");
71 if (pfd[0].revents & (events | POLLHUP))
72 return NULL;
74 return got_error(GOT_ERR_INTERRUPT);
75 }
77 static const struct got_error *
78 read_imsg(struct imsgbuf *ibuf)
79 {
80 const struct got_error *err;
81 size_t n;
83 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
84 if (err)
85 return err;
87 n = imsg_read(ibuf);
88 if (n == -1) {
89 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
90 return got_error(GOT_ERR_PRIVSEP_NO_FD);
91 return got_error(GOT_ERR_PRIVSEP_READ);
92 }
93 if (n == 0)
94 return got_error(GOT_ERR_PRIVSEP_PIPE);
96 return NULL;
97 }
99 const struct got_error *
100 got_privsep_wait_for_child(pid_t pid)
102 int child_status;
104 if (waitpid(pid, &child_status, 0) == -1)
105 return got_error_from_errno("waitpid");
107 if (!WIFEXITED(child_status))
108 return got_error(GOT_ERR_PRIVSEP_DIED);
110 if (WEXITSTATUS(child_status) != 0)
111 return got_error(GOT_ERR_PRIVSEP_EXIT);
113 return NULL;
116 static const struct got_error *
117 recv_imsg_error(struct imsg *imsg, size_t datalen)
119 struct got_imsg_error *ierr;
121 if (datalen != sizeof(*ierr))
122 return got_error(GOT_ERR_PRIVSEP_LEN);
124 ierr = imsg->data;
125 if (ierr->code == GOT_ERR_ERRNO) {
126 static struct got_error serr;
127 serr.code = GOT_ERR_ERRNO;
128 serr.msg = strerror(ierr->errno_code);
129 return &serr;
132 return got_error(ierr->code);
135 const struct got_error *
136 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
137 size_t min_datalen)
139 const struct got_error *err;
140 ssize_t n;
142 n = imsg_get(ibuf, imsg);
143 if (n == -1)
144 return got_error_from_errno("imsg_get");
146 while (n == 0) {
147 err = read_imsg(ibuf);
148 if (err)
149 return err;
150 n = imsg_get(ibuf, imsg);
151 if (n == -1)
152 return got_error_from_errno("imsg_get");
155 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
156 return got_error(GOT_ERR_PRIVSEP_LEN);
158 if (imsg->hdr.type == GOT_IMSG_ERROR) {
159 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
160 return recv_imsg_error(imsg, datalen);
163 return NULL;
166 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
167 void
168 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
170 const struct got_error *poll_err;
171 struct got_imsg_error ierr;
172 int ret;
174 ierr.code = err->code;
175 if (err->code == GOT_ERR_ERRNO)
176 ierr.errno_code = errno;
177 else
178 ierr.errno_code = 0;
179 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
180 if (ret == -1) {
181 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
182 getprogname(), err->code, err->msg, strerror(errno));
183 return;
186 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
187 if (poll_err) {
188 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
189 getprogname(), err->code, err->msg, poll_err->msg);
190 return;
193 ret = imsg_flush(ibuf);
194 if (ret == -1) {
195 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
196 getprogname(), err->code, err->msg, strerror(errno));
197 return;
201 static const struct got_error *
202 flush_imsg(struct imsgbuf *ibuf)
204 const struct got_error *err;
206 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
207 if (err)
208 return err;
210 if (imsg_flush(ibuf) == -1)
211 return got_error_from_errno("imsg_flush");
213 return NULL;
216 const struct got_error *
217 got_privsep_send_stop(int fd)
219 const struct got_error *err = NULL;
220 struct imsgbuf ibuf;
222 imsg_init(&ibuf, fd);
224 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
225 return got_error_from_errno("imsg_compose STOP");
227 err = flush_imsg(&ibuf);
228 imsg_clear(&ibuf);
229 return err;
232 const struct got_error *
233 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
235 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
236 == -1)
237 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
239 return flush_imsg(ibuf);
242 const struct got_error *
243 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
244 struct got_object_id *id, int pack_idx)
246 const struct got_error *err = NULL;
247 struct got_imsg_packed_object iobj, *iobjp;
248 size_t len;
250 if (id) { /* commit is packed */
251 iobj.idx = pack_idx;
252 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
253 iobjp = &iobj;
254 len = sizeof(iobj);
255 } else {
256 iobjp = NULL;
257 len = 0;
260 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
261 == -1) {
262 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
263 close(fd);
264 return err;
267 return flush_imsg(ibuf);
270 const struct got_error *
271 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
272 struct got_object_id *id, int pack_idx)
274 const struct got_error *err = NULL;
275 struct ibuf *wbuf;
276 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
278 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
279 if (wbuf == NULL)
280 return got_error_from_errno("imsg_create TREE_REQUEST");
282 if (id) { /* tree is packed */
283 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
284 err = got_error_from_errno("imsg_add TREE_ENTRY");
285 ibuf_free(wbuf);
286 return err;
289 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
290 err = got_error_from_errno("imsg_add TREE_ENTRY");
291 ibuf_free(wbuf);
292 return err;
296 wbuf->fd = fd;
297 imsg_close(ibuf, wbuf);
299 return flush_imsg(ibuf);
302 const struct got_error *
303 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
304 struct got_object_id *id, int pack_idx)
306 struct got_imsg_packed_object iobj, *iobjp;
307 size_t len;
309 if (id) { /* tag is packed */
310 iobj.idx = pack_idx;
311 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
312 iobjp = &iobj;
313 len = sizeof(iobj);
314 } else {
315 iobjp = NULL;
316 len = 0;
319 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
320 == -1)
321 return got_error_from_errno("imsg_compose TAG_REQUEST");
323 return flush_imsg(ibuf);
326 const struct got_error *
327 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
328 struct got_object_id *id, int pack_idx)
330 const struct got_error *err = NULL;
331 struct got_imsg_packed_object iobj, *iobjp;
332 size_t len;
334 if (id) { /* blob is packed */
335 iobj.idx = pack_idx;
336 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
337 iobjp = &iobj;
338 len = sizeof(iobj);
339 } else {
340 iobjp = NULL;
341 len = 0;
344 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
345 == -1) {
346 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
347 close(infd);
348 return err;
351 return flush_imsg(ibuf);
354 const struct got_error *
355 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
357 const struct got_error *err = NULL;
359 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
360 == -1) {
361 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
362 close(outfd);
363 return err;
366 return flush_imsg(ibuf);
369 const struct got_error *
370 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
372 const struct got_error *err = NULL;
374 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
375 == -1) {
376 err = got_error_from_errno("imsg_compose TMPFD");
377 close(fd);
378 return err;
381 return flush_imsg(ibuf);
384 const struct got_error *
385 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
387 struct got_imsg_object iobj;
389 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
390 iobj.type = obj->type;
391 iobj.flags = obj->flags;
392 iobj.hdrlen = obj->hdrlen;
393 iobj.size = obj->size;
394 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
395 iobj.pack_offset = obj->pack_offset;
396 iobj.pack_idx = obj->pack_idx;
399 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
400 == -1)
401 return got_error_from_errno("imsg_compose OBJECT");
403 return flush_imsg(ibuf);
406 const struct got_error *
407 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
408 struct imsgbuf *ibuf)
410 const struct got_error *err = NULL;
411 struct got_imsg_object *iobj;
412 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
414 if (datalen != sizeof(*iobj))
415 return got_error(GOT_ERR_PRIVSEP_LEN);
416 iobj = imsg->data;
418 *obj = calloc(1, sizeof(**obj));
419 if (*obj == NULL)
420 return got_error_from_errno("calloc");
422 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
423 (*obj)->type = iobj->type;
424 (*obj)->flags = iobj->flags;
425 (*obj)->hdrlen = iobj->hdrlen;
426 (*obj)->size = iobj->size;
427 /* path_packfile is handled by caller */
428 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
429 (*obj)->pack_offset = iobj->pack_offset;
430 (*obj)->pack_idx = iobj->pack_idx;
433 return err;
436 const struct got_error *
437 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
439 const struct got_error *err = NULL;
440 struct imsg imsg;
441 const size_t min_datalen =
442 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
444 *obj = NULL;
446 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
447 if (err)
448 return err;
450 switch (imsg.hdr.type) {
451 case GOT_IMSG_OBJECT:
452 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
453 break;
454 default:
455 err = got_error(GOT_ERR_PRIVSEP_MSG);
456 break;
459 imsg_free(&imsg);
461 return err;
464 static const struct got_error *
465 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
466 size_t logmsg_len)
468 const struct got_error *err = NULL;
469 size_t offset, remain;
471 offset = 0;
472 remain = logmsg_len;
473 while (remain > 0) {
474 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
476 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
477 commit->logmsg + offset, n) == -1) {
478 err = got_error_from_errno("imsg_compose "
479 "COMMIT_LOGMSG");
480 break;
483 err = flush_imsg(ibuf);
484 if (err)
485 break;
487 offset += n;
488 remain -= n;
491 return err;
494 const struct got_error *
495 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
497 const struct got_error *err = NULL;
498 struct got_imsg_commit_object *icommit;
499 uint8_t *buf;
500 size_t len, total;
501 struct got_object_qid *qid;
502 size_t author_len = strlen(commit->author);
503 size_t committer_len = strlen(commit->committer);
504 size_t logmsg_len = strlen(commit->logmsg);
506 total = sizeof(*icommit) + author_len + committer_len +
507 commit->nparents * SHA1_DIGEST_LENGTH;
509 buf = malloc(total);
510 if (buf == NULL)
511 return got_error_from_errno("malloc");
513 icommit = (struct got_imsg_commit_object *)buf;
514 memcpy(icommit->tree_id, commit->tree_id->sha1,
515 sizeof(icommit->tree_id));
516 icommit->author_len = author_len;
517 icommit->author_time = commit->author_time;
518 icommit->author_gmtoff = commit->author_gmtoff;
519 icommit->committer_len = committer_len;
520 icommit->committer_time = commit->committer_time;
521 icommit->committer_gmtoff = commit->committer_gmtoff;
522 icommit->logmsg_len = logmsg_len;
523 icommit->nparents = commit->nparents;
525 len = sizeof(*icommit);
526 memcpy(buf + len, commit->author, author_len);
527 len += author_len;
528 memcpy(buf + len, commit->committer, committer_len);
529 len += committer_len;
530 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
531 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
532 len += SHA1_DIGEST_LENGTH;
535 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
536 err = got_error_from_errno("imsg_compose COMMIT");
537 goto done;
540 if (logmsg_len == 0 ||
541 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
542 err = flush_imsg(ibuf);
543 if (err)
544 goto done;
546 err = send_commit_logmsg(ibuf, commit, logmsg_len);
547 done:
548 free(buf);
549 return err;
552 const struct got_error *
553 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
555 const struct got_error *err = NULL;
556 struct imsg imsg;
557 struct got_imsg_commit_object *icommit;
558 size_t len, datalen;
559 int i;
560 const size_t min_datalen =
561 MIN(sizeof(struct got_imsg_error),
562 sizeof(struct got_imsg_commit_object));
564 *commit = NULL;
566 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
567 if (err)
568 return err;
570 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
571 len = 0;
573 switch (imsg.hdr.type) {
574 case GOT_IMSG_COMMIT:
575 if (datalen < sizeof(*icommit)) {
576 err = got_error(GOT_ERR_PRIVSEP_LEN);
577 break;
579 icommit = imsg.data;
580 if (datalen != sizeof(*icommit) + icommit->author_len +
581 icommit->committer_len +
582 icommit->nparents * SHA1_DIGEST_LENGTH) {
583 err = got_error(GOT_ERR_PRIVSEP_LEN);
584 break;
586 if (icommit->nparents < 0) {
587 err = got_error(GOT_ERR_PRIVSEP_LEN);
588 break;
590 len += sizeof(*icommit);
592 *commit = got_object_commit_alloc_partial();
593 if (*commit == NULL) {
594 err = got_error_from_errno(
595 "got_object_commit_alloc_partial");
596 break;
599 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
600 SHA1_DIGEST_LENGTH);
601 (*commit)->author_time = icommit->author_time;
602 (*commit)->author_gmtoff = icommit->author_gmtoff;
603 (*commit)->committer_time = icommit->committer_time;
604 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
606 if (icommit->author_len == 0) {
607 (*commit)->author = strdup("");
608 if ((*commit)->author == NULL) {
609 err = got_error_from_errno("strdup");
610 break;
612 } else {
613 (*commit)->author = malloc(icommit->author_len + 1);
614 if ((*commit)->author == NULL) {
615 err = got_error_from_errno("malloc");
616 break;
618 memcpy((*commit)->author, imsg.data + len,
619 icommit->author_len);
620 (*commit)->author[icommit->author_len] = '\0';
622 len += icommit->author_len;
624 if (icommit->committer_len == 0) {
625 (*commit)->committer = strdup("");
626 if ((*commit)->committer == NULL) {
627 err = got_error_from_errno("strdup");
628 break;
630 } else {
631 (*commit)->committer =
632 malloc(icommit->committer_len + 1);
633 if ((*commit)->committer == NULL) {
634 err = got_error_from_errno("malloc");
635 break;
637 memcpy((*commit)->committer, imsg.data + len,
638 icommit->committer_len);
639 (*commit)->committer[icommit->committer_len] = '\0';
641 len += icommit->committer_len;
643 if (icommit->logmsg_len == 0) {
644 (*commit)->logmsg = strdup("");
645 if ((*commit)->logmsg == NULL) {
646 err = got_error_from_errno("strdup");
647 break;
649 } else {
650 size_t offset = 0, remain = icommit->logmsg_len;
652 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
653 if ((*commit)->logmsg == NULL) {
654 err = got_error_from_errno("malloc");
655 break;
657 while (remain > 0) {
658 struct imsg imsg_log;
659 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
660 remain);
662 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
663 if (err)
664 return err;
666 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
667 return got_error(GOT_ERR_PRIVSEP_MSG);
669 memcpy((*commit)->logmsg + offset,
670 imsg_log.data, n);
671 imsg_free(&imsg_log);
672 offset += n;
673 remain -= n;
675 (*commit)->logmsg[icommit->logmsg_len] = '\0';
678 for (i = 0; i < icommit->nparents; i++) {
679 struct got_object_qid *qid;
681 err = got_object_qid_alloc_partial(&qid);
682 if (err)
683 break;
684 memcpy(qid->id, imsg.data + len +
685 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
686 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
687 (*commit)->nparents++;
689 break;
690 default:
691 err = got_error(GOT_ERR_PRIVSEP_MSG);
692 break;
695 imsg_free(&imsg);
697 return err;
700 const struct got_error *
701 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
702 int nentries)
704 const struct got_error *err = NULL;
705 struct got_imsg_tree_object itree;
706 struct got_pathlist_entry *pe;
707 size_t totlen;
708 int nimsg; /* number of imsg queued in ibuf */
710 itree.nentries = nentries;
711 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
712 == -1)
713 return got_error_from_errno("imsg_compose TREE");
715 totlen = sizeof(itree);
716 nimsg = 1;
717 TAILQ_FOREACH(pe, entries, entry) {
718 const char *name = pe->path;
719 struct got_parsed_tree_entry *pte = pe->data;
720 struct ibuf *wbuf;
721 size_t namelen = strlen(name);
722 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
724 if (len > MAX_IMSGSIZE)
725 return got_error(GOT_ERR_NO_SPACE);
727 nimsg++;
728 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
729 err = flush_imsg(ibuf);
730 if (err)
731 return err;
732 nimsg = 0;
735 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
736 if (wbuf == NULL)
737 return got_error_from_errno("imsg_create TREE_ENTRY");
739 /* Keep in sync with struct got_imsg_tree_object definition! */
740 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
741 err = got_error_from_errno("imsg_add TREE_ENTRY");
742 ibuf_free(wbuf);
743 return err;
745 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
746 err = got_error_from_errno("imsg_add TREE_ENTRY");
747 ibuf_free(wbuf);
748 return err;
751 if (imsg_add(wbuf, name, namelen) == -1) {
752 err = got_error_from_errno("imsg_add TREE_ENTRY");
753 ibuf_free(wbuf);
754 return err;
757 wbuf->fd = -1;
758 imsg_close(ibuf, wbuf);
760 totlen += len;
763 return flush_imsg(ibuf);
766 const struct got_error *
767 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
769 const struct got_error *err = NULL;
770 const size_t min_datalen =
771 MIN(sizeof(struct got_imsg_error),
772 sizeof(struct got_imsg_tree_object));
773 struct got_imsg_tree_object *itree;
774 int nentries = 0;
776 *tree = NULL;
777 get_more:
778 err = read_imsg(ibuf);
779 if (err)
780 goto done;
782 for (;;) {
783 struct imsg imsg;
784 size_t n;
785 size_t datalen;
786 struct got_imsg_tree_entry *ite;
787 struct got_tree_entry *te = NULL;
789 n = imsg_get(ibuf, &imsg);
790 if (n == 0) {
791 if (*tree && (*tree)->nentries != nentries)
792 goto get_more;
793 break;
796 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
797 return got_error(GOT_ERR_PRIVSEP_LEN);
799 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
801 switch (imsg.hdr.type) {
802 case GOT_IMSG_ERROR:
803 err = recv_imsg_error(&imsg, datalen);
804 break;
805 case GOT_IMSG_TREE:
806 /* This message should only appear once. */
807 if (*tree != NULL) {
808 err = got_error(GOT_ERR_PRIVSEP_MSG);
809 break;
811 if (datalen != sizeof(*itree)) {
812 err = got_error(GOT_ERR_PRIVSEP_LEN);
813 break;
815 itree = imsg.data;
816 *tree = malloc(sizeof(**tree));
817 if (*tree == NULL) {
818 err = got_error_from_errno("malloc");
819 break;
821 (*tree)->entries = calloc(itree->nentries,
822 sizeof(struct got_tree_entry));
823 if ((*tree)->entries == NULL) {
824 err = got_error_from_errno("malloc");
825 break;
827 (*tree)->nentries = itree->nentries;
828 (*tree)->refcnt = 0;
829 break;
830 case GOT_IMSG_TREE_ENTRY:
831 /* This message should be preceeded by GOT_IMSG_TREE. */
832 if (*tree == NULL) {
833 err = got_error(GOT_ERR_PRIVSEP_MSG);
834 break;
836 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
837 err = got_error(GOT_ERR_PRIVSEP_LEN);
838 break;
841 /* Remaining data contains the entry's name. */
842 datalen -= sizeof(*ite);
843 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
844 err = got_error(GOT_ERR_PRIVSEP_LEN);
845 break;
847 ite = imsg.data;
849 if (datalen + 1 > sizeof(te->name)) {
850 err = got_error(GOT_ERR_NO_SPACE);
851 break;
853 te = &(*tree)->entries[nentries];
854 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
855 te->name[datalen] = '\0';
857 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
858 te->mode = ite->mode;
859 te->idx = nentries;
860 nentries++;
861 break;
862 default:
863 err = got_error(GOT_ERR_PRIVSEP_MSG);
864 break;
867 imsg_free(&imsg);
869 done:
870 if (*tree && (*tree)->nentries != nentries) {
871 if (err == NULL)
872 err = got_error(GOT_ERR_PRIVSEP_LEN);
873 got_object_tree_close(*tree);
874 *tree = NULL;
877 return err;
880 const struct got_error *
881 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
882 const uint8_t *data)
884 struct got_imsg_blob iblob;
886 iblob.size = size;
887 iblob.hdrlen = hdrlen;
889 if (data) {
890 uint8_t *buf;
892 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
893 return got_error(GOT_ERR_NO_SPACE);
895 buf = malloc(sizeof(iblob) + size);
896 if (buf == NULL)
897 return got_error_from_errno("malloc");
899 memcpy(buf, &iblob, sizeof(iblob));
900 memcpy(buf + sizeof(iblob), data, size);
901 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
902 sizeof(iblob) + size) == -1) {
903 free(buf);
904 return got_error_from_errno("imsg_compose BLOB");
906 free(buf);
907 } else {
908 /* Data has already been written to file descriptor. */
909 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
910 sizeof(iblob)) == -1)
911 return got_error_from_errno("imsg_compose BLOB");
915 return flush_imsg(ibuf);
918 const struct got_error *
919 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
920 struct imsgbuf *ibuf)
922 const struct got_error *err = NULL;
923 struct imsg imsg;
924 struct got_imsg_blob *iblob;
925 size_t datalen;
927 *outbuf = NULL;
929 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
930 if (err)
931 return err;
933 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
935 switch (imsg.hdr.type) {
936 case GOT_IMSG_BLOB:
937 if (datalen < sizeof(*iblob)) {
938 err = got_error(GOT_ERR_PRIVSEP_LEN);
939 break;
941 iblob = imsg.data;
942 *size = iblob->size;
943 *hdrlen = iblob->hdrlen;
945 if (datalen == sizeof(*iblob)) {
946 /* Data has been written to file descriptor. */
947 break;
950 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
951 err = got_error(GOT_ERR_PRIVSEP_LEN);
952 break;
955 *outbuf = malloc(*size);
956 if (*outbuf == NULL) {
957 err = got_error_from_errno("malloc");
958 break;
960 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
961 break;
962 default:
963 err = got_error(GOT_ERR_PRIVSEP_MSG);
964 break;
967 imsg_free(&imsg);
969 return err;
972 static const struct got_error *
973 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
975 const struct got_error *err = NULL;
976 size_t offset, remain;
978 offset = 0;
979 remain = tagmsg_len;
980 while (remain > 0) {
981 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
983 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
984 tag->tagmsg + offset, n) == -1) {
985 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
986 break;
989 err = flush_imsg(ibuf);
990 if (err)
991 break;
993 offset += n;
994 remain -= n;
997 return err;
1000 const struct got_error *
1001 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1003 const struct got_error *err = NULL;
1004 struct got_imsg_tag_object *itag;
1005 uint8_t *buf;
1006 size_t len, total;
1007 size_t tag_len = strlen(tag->tag);
1008 size_t tagger_len = strlen(tag->tagger);
1009 size_t tagmsg_len = strlen(tag->tagmsg);
1011 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1013 buf = malloc(total);
1014 if (buf == NULL)
1015 return got_error_from_errno("malloc");
1017 itag = (struct got_imsg_tag_object *)buf;
1018 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1019 itag->obj_type = tag->obj_type;
1020 itag->tag_len = tag_len;
1021 itag->tagger_len = tagger_len;
1022 itag->tagger_time = tag->tagger_time;
1023 itag->tagger_gmtoff = tag->tagger_gmtoff;
1024 itag->tagmsg_len = tagmsg_len;
1026 len = sizeof(*itag);
1027 memcpy(buf + len, tag->tag, tag_len);
1028 len += tag_len;
1029 memcpy(buf + len, tag->tagger, tagger_len);
1030 len += tagger_len;
1032 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1033 err = got_error_from_errno("imsg_compose TAG");
1034 goto done;
1037 if (tagmsg_len == 0 ||
1038 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1039 err = flush_imsg(ibuf);
1040 if (err)
1041 goto done;
1043 err = send_tagmsg(ibuf, tag, tagmsg_len);
1044 done:
1045 free(buf);
1046 return err;
1049 const struct got_error *
1050 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1052 const struct got_error *err = NULL;
1053 struct imsg imsg;
1054 struct got_imsg_tag_object *itag;
1055 size_t len, datalen;
1056 const size_t min_datalen =
1057 MIN(sizeof(struct got_imsg_error),
1058 sizeof(struct got_imsg_tag_object));
1060 *tag = NULL;
1062 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1063 if (err)
1064 return err;
1066 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1067 len = 0;
1069 switch (imsg.hdr.type) {
1070 case GOT_IMSG_TAG:
1071 if (datalen < sizeof(*itag)) {
1072 err = got_error(GOT_ERR_PRIVSEP_LEN);
1073 break;
1075 itag = imsg.data;
1076 if (datalen != sizeof(*itag) + itag->tag_len +
1077 itag->tagger_len) {
1078 err = got_error(GOT_ERR_PRIVSEP_LEN);
1079 break;
1081 len += sizeof(*itag);
1083 *tag = calloc(1, sizeof(**tag));
1084 if (*tag == NULL) {
1085 err = got_error_from_errno("calloc");
1086 break;
1089 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1091 if (itag->tag_len == 0) {
1092 (*tag)->tag = strdup("");
1093 if ((*tag)->tag == NULL) {
1094 err = got_error_from_errno("strdup");
1095 break;
1097 } else {
1098 (*tag)->tag = malloc(itag->tag_len + 1);
1099 if ((*tag)->tag == NULL) {
1100 err = got_error_from_errno("malloc");
1101 break;
1103 memcpy((*tag)->tag, imsg.data + len,
1104 itag->tag_len);
1105 (*tag)->tag[itag->tag_len] = '\0';
1107 len += itag->tag_len;
1109 (*tag)->obj_type = itag->obj_type;
1110 (*tag)->tagger_time = itag->tagger_time;
1111 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1113 if (itag->tagger_len == 0) {
1114 (*tag)->tagger = strdup("");
1115 if ((*tag)->tagger == NULL) {
1116 err = got_error_from_errno("strdup");
1117 break;
1119 } else {
1120 (*tag)->tagger = malloc(itag->tagger_len + 1);
1121 if ((*tag)->tagger == NULL) {
1122 err = got_error_from_errno("malloc");
1123 break;
1125 memcpy((*tag)->tagger, imsg.data + len,
1126 itag->tagger_len);
1127 (*tag)->tagger[itag->tagger_len] = '\0';
1129 len += itag->tagger_len;
1131 if (itag->tagmsg_len == 0) {
1132 (*tag)->tagmsg = strdup("");
1133 if ((*tag)->tagmsg == NULL) {
1134 err = got_error_from_errno("strdup");
1135 break;
1137 } else {
1138 size_t offset = 0, remain = itag->tagmsg_len;
1140 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1141 if ((*tag)->tagmsg == NULL) {
1142 err = got_error_from_errno("malloc");
1143 break;
1145 while (remain > 0) {
1146 struct imsg imsg_log;
1147 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1148 remain);
1150 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1151 if (err)
1152 return err;
1154 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1155 return got_error(GOT_ERR_PRIVSEP_MSG);
1157 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1158 n);
1159 imsg_free(&imsg_log);
1160 offset += n;
1161 remain -= n;
1163 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1166 break;
1167 default:
1168 err = got_error(GOT_ERR_PRIVSEP_MSG);
1169 break;
1172 imsg_free(&imsg);
1174 return err;
1177 const struct got_error *
1178 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1179 struct got_packidx *packidx)
1181 const struct got_error *err = NULL;
1182 struct got_imsg_packidx ipackidx;
1183 struct got_imsg_pack ipack;
1184 int fd;
1186 ipackidx.len = packidx->len;
1187 fd = dup(packidx->fd);
1188 if (fd == -1)
1189 return got_error_from_errno("dup");
1191 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1192 sizeof(ipackidx)) == -1) {
1193 err = got_error_from_errno("imsg_compose PACKIDX");
1194 close(fd);
1195 return err;
1198 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1199 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1200 return got_error(GOT_ERR_NO_SPACE);
1201 ipack.filesize = pack->filesize;
1203 fd = dup(pack->fd);
1204 if (fd == -1)
1205 return got_error_from_errno("dup");
1207 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1208 == -1) {
1209 err = got_error_from_errno("imsg_compose PACK");
1210 close(fd);
1211 return err;
1214 return flush_imsg(ibuf);
1217 const struct got_error *
1218 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1219 struct got_object_id *id)
1221 struct got_imsg_packed_object iobj;
1223 iobj.idx = idx;
1224 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1226 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1227 &iobj, sizeof(iobj)) == -1)
1228 return got_error_from_errno("imsg_compose "
1229 "PACKED_OBJECT_REQUEST");
1231 return flush_imsg(ibuf);
1234 const struct got_error *
1235 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1237 const struct got_error *err = NULL;
1239 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1240 NULL, 0) == -1) {
1241 err = got_error_from_errno("imsg_compose "
1242 "GITCONFIG_PARSE_REQUEST");
1243 close(fd);
1244 return err;
1247 return flush_imsg(ibuf);
1250 const struct got_error *
1251 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1253 if (imsg_compose(ibuf,
1254 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1255 NULL, 0) == -1)
1256 return got_error_from_errno("imsg_compose "
1257 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1259 return flush_imsg(ibuf);
1262 const struct got_error *
1263 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1265 if (imsg_compose(ibuf,
1266 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1267 return got_error_from_errno("imsg_compose "
1268 "GITCONFIG_AUTHOR_NAME_REQUEST");
1270 return flush_imsg(ibuf);
1273 const struct got_error *
1274 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1276 if (imsg_compose(ibuf,
1277 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1278 return got_error_from_errno("imsg_compose "
1279 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1281 return flush_imsg(ibuf);
1284 const struct got_error *
1285 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1287 if (imsg_compose(ibuf,
1288 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1289 return got_error_from_errno("imsg_compose "
1290 "GITCONFIG_REMOTE_REQUEST");
1292 return flush_imsg(ibuf);
1295 const struct got_error *
1296 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1298 size_t len = value ? strlen(value) + 1 : 0;
1300 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1301 value, len) == -1)
1302 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1304 return flush_imsg(ibuf);
1307 const struct got_error *
1308 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1310 const struct got_error *err = NULL;
1311 struct imsg imsg;
1312 size_t datalen;
1313 const size_t min_datalen = 0;
1315 *str = NULL;
1317 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1318 if (err)
1319 return err;
1320 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1322 switch (imsg.hdr.type) {
1323 case GOT_IMSG_GITCONFIG_STR_VAL:
1324 if (datalen == 0)
1325 break;
1326 *str = malloc(datalen);
1327 if (*str == NULL) {
1328 err = got_error_from_errno("malloc");
1329 break;
1331 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1332 err = got_error(GOT_ERR_NO_SPACE);
1333 break;
1334 default:
1335 err = got_error(GOT_ERR_PRIVSEP_MSG);
1336 break;
1339 imsg_free(&imsg);
1340 return err;
1343 const struct got_error *
1344 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1346 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1347 &value, sizeof(value)) == -1)
1348 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1350 return flush_imsg(ibuf);
1353 const struct got_error *
1354 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1356 const struct got_error *err = NULL;
1357 struct imsg imsg;
1358 size_t datalen;
1359 const size_t min_datalen =
1360 MIN(sizeof(struct got_imsg_error), sizeof(int));
1362 *val = 0;
1364 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1365 if (err)
1366 return err;
1367 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1369 switch (imsg.hdr.type) {
1370 case GOT_IMSG_GITCONFIG_INT_VAL:
1371 if (datalen != sizeof(*val)) {
1372 err = got_error(GOT_ERR_PRIVSEP_LEN);
1373 break;
1375 memcpy(val, imsg.data, sizeof(*val));
1376 break;
1377 default:
1378 err = got_error(GOT_ERR_PRIVSEP_MSG);
1379 break;
1382 imsg_free(&imsg);
1383 return err;
1386 const struct got_error *
1387 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1388 struct got_remote_repo *remotes, int nremotes)
1390 const struct got_error *err = NULL;
1391 struct got_imsg_remotes iremotes;
1392 int i;
1394 iremotes.nremotes = nremotes;
1395 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1396 &iremotes, sizeof(iremotes)) == -1)
1397 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1399 err = flush_imsg(ibuf);
1400 imsg_clear(ibuf);
1401 if (err)
1402 return err;
1404 for (i = 0; i < nremotes; i++) {
1405 struct got_imsg_remote iremote;
1406 size_t len = sizeof(iremote);
1407 struct ibuf *wbuf;
1409 iremote.name_len = strlen(remotes[i].name);
1410 len += iremote.name_len;
1411 iremote.url_len = strlen(remotes[i].url);
1412 len += iremote.url_len;
1414 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1415 if (wbuf == NULL)
1416 return got_error_from_errno(
1417 "imsg_create GITCONFIG_REMOTE");
1419 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1420 err = got_error_from_errno(
1421 "imsg_add GIITCONFIG_REMOTE");
1422 ibuf_free(wbuf);
1423 return err;
1426 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1427 err = got_error_from_errno(
1428 "imsg_add GIITCONFIG_REMOTE");
1429 ibuf_free(wbuf);
1430 return err;
1432 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1433 err = got_error_from_errno(
1434 "imsg_add GIITCONFIG_REMOTE");
1435 ibuf_free(wbuf);
1436 return err;
1439 wbuf->fd = -1;
1440 imsg_close(ibuf, wbuf);
1441 err = flush_imsg(ibuf);
1442 if (err)
1443 return err;
1446 return NULL;
1449 const struct got_error *
1450 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1451 int *nremotes, struct imsgbuf *ibuf)
1453 const struct got_error *err = NULL;
1454 struct imsg imsg;
1455 size_t datalen;
1456 struct got_imsg_remotes iremotes;
1457 struct got_imsg_remote iremote;
1459 *remotes = NULL;
1460 *nremotes = 0;
1462 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1463 if (err)
1464 return err;
1465 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1467 switch (imsg.hdr.type) {
1468 case GOT_IMSG_GITCONFIG_REMOTES:
1469 if (datalen != sizeof(iremotes)) {
1470 err = got_error(GOT_ERR_PRIVSEP_LEN);
1471 break;
1473 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1474 if (iremotes.nremotes == 0) {
1475 imsg_free(&imsg);
1476 return NULL;
1478 break;
1479 default:
1480 err = got_error(GOT_ERR_PRIVSEP_MSG);
1481 break;
1484 imsg_free(&imsg);
1486 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1487 if (*remotes == NULL)
1488 return got_error_from_errno("recallocarray");
1490 while (*nremotes < iremotes.nremotes) {
1491 struct got_remote_repo *remote;
1493 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1494 if (err)
1495 break;
1496 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1498 switch (imsg.hdr.type) {
1499 case GOT_IMSG_GITCONFIG_REMOTE:
1500 remote = &(*remotes)[*nremotes];
1501 if (datalen < sizeof(iremote)) {
1502 err = got_error(GOT_ERR_PRIVSEP_LEN);
1503 break;
1505 memcpy(&iremote, imsg.data, sizeof(iremote));
1506 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1507 (sizeof(iremote) + iremote.name_len +
1508 iremote.url_len) > datalen) {
1509 err = got_error(GOT_ERR_PRIVSEP_LEN);
1510 break;
1512 remote->name = strndup(imsg.data + sizeof(iremote),
1513 iremote.name_len);
1514 if (remote->name == NULL) {
1515 err = got_error_from_errno("strndup");
1516 break;
1518 remote->url = strndup(imsg.data + sizeof(iremote) +
1519 iremote.name_len, iremote.url_len);
1520 if (remote->url == NULL) {
1521 err = got_error_from_errno("strndup");
1522 free(remote->name);
1523 break;
1525 (*nremotes)++;
1526 break;
1527 default:
1528 err = got_error(GOT_ERR_PRIVSEP_MSG);
1529 break;
1532 imsg_free(&imsg);
1533 if (err)
1534 break;
1537 if (err) {
1538 int i;
1539 for (i = 0; i < *nremotes; i++) {
1540 free((*remotes)[i].name);
1541 free((*remotes)[i].url);
1543 free(*remotes);
1544 *remotes = NULL;
1545 *nremotes = 0;
1547 return err;
1550 const struct got_error *
1551 got_privsep_unveil_exec_helpers(void)
1553 const char *helpers[] = {
1554 GOT_PATH_PROG_READ_PACK,
1555 GOT_PATH_PROG_READ_OBJECT,
1556 GOT_PATH_PROG_READ_COMMIT,
1557 GOT_PATH_PROG_READ_TREE,
1558 GOT_PATH_PROG_READ_BLOB,
1559 GOT_PATH_PROG_READ_TAG,
1560 GOT_PATH_PROG_READ_GITCONFIG,
1562 int i;
1564 for (i = 0; i < nitems(helpers); i++) {
1565 if (unveil(helpers[i], "x") == 0)
1566 continue;
1567 return got_error_from_errno2("unveil", helpers[i]);
1570 return NULL;
1573 void
1574 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1576 if (close(imsg_fds[0]) != 0) {
1577 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1578 _exit(1);
1581 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1582 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1583 _exit(1);
1585 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1586 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1587 _exit(1);
1590 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1591 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1592 strerror(errno));
1593 _exit(1);