Blob


1 /*
2 * Copyright (c) 2018, 2019 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);
153 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
154 return got_error(GOT_ERR_PRIVSEP_LEN);
156 if (imsg->hdr.type == GOT_IMSG_ERROR) {
157 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
158 return recv_imsg_error(imsg, datalen);
161 return NULL;
164 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
165 void
166 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
168 const struct got_error *poll_err;
169 struct got_imsg_error ierr;
170 int ret;
172 ierr.code = err->code;
173 if (err->code == GOT_ERR_ERRNO)
174 ierr.errno_code = errno;
175 else
176 ierr.errno_code = 0;
177 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
178 if (ret == -1) {
179 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
180 getprogname(), err->code, err->msg, strerror(errno));
181 return;
184 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
185 if (poll_err) {
186 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
187 getprogname(), err->code, err->msg, poll_err->msg);
188 return;
191 ret = imsg_flush(ibuf);
192 if (ret == -1) {
193 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
194 getprogname(), err->code, err->msg, strerror(errno));
195 return;
199 static const struct got_error *
200 flush_imsg(struct imsgbuf *ibuf)
202 const struct got_error *err;
204 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
205 if (err)
206 return err;
208 if (imsg_flush(ibuf) == -1)
209 return got_error_from_errno("imsg_flush");
211 return NULL;
214 const struct got_error *
215 got_privsep_send_stop(int fd)
217 const struct got_error *err = NULL;
218 struct imsgbuf ibuf;
220 imsg_init(&ibuf, fd);
222 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
223 return got_error_from_errno("imsg_compose STOP");
225 err = flush_imsg(&ibuf);
226 imsg_clear(&ibuf);
227 return err;
230 const struct got_error *
231 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd)
233 if (imsg_compose(ibuf, GOT_IMSG_OBJECT_REQUEST, 0, 0, fd, NULL, 0)
234 == -1)
235 return got_error_from_errno("imsg_compose OBJECT_REQUEST");
237 return flush_imsg(ibuf);
240 const struct got_error *
241 got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
242 struct got_object_id *id, int pack_idx)
244 const struct got_error *err = NULL;
245 struct got_imsg_packed_object iobj, *iobjp;
246 size_t len;
248 if (id) { /* commit is packed */
249 iobj.idx = pack_idx;
250 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
251 iobjp = &iobj;
252 len = sizeof(iobj);
253 } else {
254 iobjp = NULL;
255 len = 0;
258 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
259 == -1) {
260 err = got_error_from_errno("imsg_compose COMMIT_REQUEST");
261 close(fd);
262 return err;
265 return flush_imsg(ibuf);
268 const struct got_error *
269 got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
270 struct got_object_id *id, int pack_idx)
272 const struct got_error *err = NULL;
273 struct ibuf *wbuf;
274 size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
276 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
277 if (wbuf == NULL)
278 return got_error_from_errno("imsg_create TREE_REQUEST");
280 if (id) { /* tree is packed */
281 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
282 err = got_error_from_errno("imsg_add TREE_ENTRY");
283 ibuf_free(wbuf);
284 return err;
287 if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
288 err = got_error_from_errno("imsg_add TREE_ENTRY");
289 ibuf_free(wbuf);
290 return err;
294 wbuf->fd = fd;
295 imsg_close(ibuf, wbuf);
297 return flush_imsg(ibuf);
300 const struct got_error *
301 got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
302 struct got_object_id *id, int pack_idx)
304 struct got_imsg_packed_object iobj, *iobjp;
305 size_t len;
307 if (id) { /* tag is packed */
308 iobj.idx = pack_idx;
309 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
310 iobjp = &iobj;
311 len = sizeof(iobj);
312 } else {
313 iobjp = NULL;
314 len = 0;
317 if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
318 == -1)
319 return got_error_from_errno("imsg_compose TAG_REQUEST");
321 return flush_imsg(ibuf);
324 const struct got_error *
325 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd,
326 struct got_object_id *id, int pack_idx)
328 const struct got_error *err = NULL;
329 struct got_imsg_packed_object iobj, *iobjp;
330 size_t len;
332 if (id) { /* blob is packed */
333 iobj.idx = pack_idx;
334 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
335 iobjp = &iobj;
336 len = sizeof(iobj);
337 } else {
338 iobjp = NULL;
339 len = 0;
342 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, iobjp, len)
343 == -1) {
344 err = got_error_from_errno("imsg_compose BLOB_REQUEST");
345 close(infd);
346 return err;
349 return flush_imsg(ibuf);
352 const struct got_error *
353 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
355 const struct got_error *err = NULL;
357 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
358 == -1) {
359 err = got_error_from_errno("imsg_compose BLOB_OUTFD");
360 close(outfd);
361 return err;
364 return flush_imsg(ibuf);
367 const struct got_error *
368 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
370 const struct got_error *err = NULL;
372 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
373 == -1) {
374 err = got_error_from_errno("imsg_compose TMPFD");
375 close(fd);
376 return err;
379 return flush_imsg(ibuf);
382 const struct got_error *
383 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
385 struct got_imsg_object iobj;
387 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
388 iobj.type = obj->type;
389 iobj.flags = obj->flags;
390 iobj.hdrlen = obj->hdrlen;
391 iobj.size = obj->size;
392 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
393 iobj.pack_offset = obj->pack_offset;
394 iobj.pack_idx = obj->pack_idx;
397 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
398 == -1)
399 return got_error_from_errno("imsg_compose OBJECT");
401 return flush_imsg(ibuf);
404 const struct got_error *
405 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
406 struct imsgbuf *ibuf)
408 const struct got_error *err = NULL;
409 struct got_imsg_object *iobj;
410 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
412 if (datalen != sizeof(*iobj))
413 return got_error(GOT_ERR_PRIVSEP_LEN);
414 iobj = imsg->data;
416 *obj = calloc(1, sizeof(**obj));
417 if (*obj == NULL)
418 return got_error_from_errno("calloc");
420 memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
421 (*obj)->type = iobj->type;
422 (*obj)->flags = iobj->flags;
423 (*obj)->hdrlen = iobj->hdrlen;
424 (*obj)->size = iobj->size;
425 /* path_packfile is handled by caller */
426 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
427 (*obj)->pack_offset = iobj->pack_offset;
428 (*obj)->pack_idx = iobj->pack_idx;
431 return err;
434 const struct got_error *
435 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
437 const struct got_error *err = NULL;
438 struct imsg imsg;
439 const size_t min_datalen =
440 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
442 *obj = NULL;
444 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
445 if (err)
446 return err;
448 switch (imsg.hdr.type) {
449 case GOT_IMSG_OBJECT:
450 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
451 break;
452 default:
453 err = got_error(GOT_ERR_PRIVSEP_MSG);
454 break;
457 imsg_free(&imsg);
459 return err;
462 static const struct got_error *
463 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
464 size_t logmsg_len)
466 const struct got_error *err = NULL;
467 size_t offset, remain;
469 offset = 0;
470 remain = logmsg_len;
471 while (remain > 0) {
472 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
474 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
475 commit->logmsg + offset, n) == -1) {
476 err = got_error_from_errno("imsg_compose "
477 "COMMIT_LOGMSG");
478 break;
481 err = flush_imsg(ibuf);
482 if (err)
483 break;
485 offset += n;
486 remain -= n;
489 return err;
492 const struct got_error *
493 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
495 const struct got_error *err = NULL;
496 struct got_imsg_commit_object *icommit;
497 uint8_t *buf;
498 size_t len, total;
499 struct got_object_qid *qid;
500 size_t author_len = strlen(commit->author);
501 size_t committer_len = strlen(commit->committer);
502 size_t logmsg_len = strlen(commit->logmsg);
504 total = sizeof(*icommit) + author_len + committer_len +
505 commit->nparents * SHA1_DIGEST_LENGTH;
507 buf = malloc(total);
508 if (buf == NULL)
509 return got_error_from_errno("malloc");
511 icommit = (struct got_imsg_commit_object *)buf;
512 memcpy(icommit->tree_id, commit->tree_id->sha1,
513 sizeof(icommit->tree_id));
514 icommit->author_len = author_len;
515 icommit->author_time = commit->author_time;
516 icommit->author_gmtoff = commit->author_gmtoff;
517 icommit->committer_len = committer_len;
518 icommit->committer_time = commit->committer_time;
519 icommit->committer_gmtoff = commit->committer_gmtoff;
520 icommit->logmsg_len = logmsg_len;
521 icommit->nparents = commit->nparents;
523 len = sizeof(*icommit);
524 memcpy(buf + len, commit->author, author_len);
525 len += author_len;
526 memcpy(buf + len, commit->committer, committer_len);
527 len += committer_len;
528 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
529 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
530 len += SHA1_DIGEST_LENGTH;
533 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
534 err = got_error_from_errno("imsg_compose COMMIT");
535 goto done;
538 if (logmsg_len == 0 ||
539 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
540 err = flush_imsg(ibuf);
541 if (err)
542 goto done;
544 err = send_commit_logmsg(ibuf, commit, logmsg_len);
545 done:
546 free(buf);
547 return err;
550 const struct got_error *
551 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
553 const struct got_error *err = NULL;
554 struct imsg imsg;
555 struct got_imsg_commit_object *icommit;
556 size_t len, datalen;
557 int i;
558 const size_t min_datalen =
559 MIN(sizeof(struct got_imsg_error),
560 sizeof(struct got_imsg_commit_object));
562 *commit = NULL;
564 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
565 if (err)
566 return err;
568 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
569 len = 0;
571 switch (imsg.hdr.type) {
572 case GOT_IMSG_COMMIT:
573 if (datalen < sizeof(*icommit)) {
574 err = got_error(GOT_ERR_PRIVSEP_LEN);
575 break;
577 icommit = imsg.data;
578 if (datalen != sizeof(*icommit) + icommit->author_len +
579 icommit->committer_len +
580 icommit->nparents * SHA1_DIGEST_LENGTH) {
581 err = got_error(GOT_ERR_PRIVSEP_LEN);
582 break;
584 if (icommit->nparents < 0) {
585 err = got_error(GOT_ERR_PRIVSEP_LEN);
586 break;
588 len += sizeof(*icommit);
590 *commit = got_object_commit_alloc_partial();
591 if (*commit == NULL) {
592 err = got_error_from_errno(
593 "got_object_commit_alloc_partial");
594 break;
597 memcpy((*commit)->tree_id->sha1, icommit->tree_id,
598 SHA1_DIGEST_LENGTH);
599 (*commit)->author_time = icommit->author_time;
600 (*commit)->author_gmtoff = icommit->author_gmtoff;
601 (*commit)->committer_time = icommit->committer_time;
602 (*commit)->committer_gmtoff = icommit->committer_gmtoff;
604 if (icommit->author_len == 0) {
605 (*commit)->author = strdup("");
606 if ((*commit)->author == NULL) {
607 err = got_error_from_errno("strdup");
608 break;
610 } else {
611 (*commit)->author = malloc(icommit->author_len + 1);
612 if ((*commit)->author == NULL) {
613 err = got_error_from_errno("malloc");
614 break;
616 memcpy((*commit)->author, imsg.data + len,
617 icommit->author_len);
618 (*commit)->author[icommit->author_len] = '\0';
620 len += icommit->author_len;
622 if (icommit->committer_len == 0) {
623 (*commit)->committer = strdup("");
624 if ((*commit)->committer == NULL) {
625 err = got_error_from_errno("strdup");
626 break;
628 } else {
629 (*commit)->committer =
630 malloc(icommit->committer_len + 1);
631 if ((*commit)->committer == NULL) {
632 err = got_error_from_errno("malloc");
633 break;
635 memcpy((*commit)->committer, imsg.data + len,
636 icommit->committer_len);
637 (*commit)->committer[icommit->committer_len] = '\0';
639 len += icommit->committer_len;
641 if (icommit->logmsg_len == 0) {
642 (*commit)->logmsg = strdup("");
643 if ((*commit)->logmsg == NULL) {
644 err = got_error_from_errno("strdup");
645 break;
647 } else {
648 size_t offset = 0, remain = icommit->logmsg_len;
650 (*commit)->logmsg = malloc(icommit->logmsg_len + 1);
651 if ((*commit)->logmsg == NULL) {
652 err = got_error_from_errno("malloc");
653 break;
655 while (remain > 0) {
656 struct imsg imsg_log;
657 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
658 remain);
660 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
661 if (err)
662 return err;
664 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
665 return got_error(GOT_ERR_PRIVSEP_MSG);
667 memcpy((*commit)->logmsg + offset,
668 imsg_log.data, n);
669 imsg_free(&imsg_log);
670 offset += n;
671 remain -= n;
673 (*commit)->logmsg[icommit->logmsg_len] = '\0';
676 for (i = 0; i < icommit->nparents; i++) {
677 struct got_object_qid *qid;
679 err = got_object_qid_alloc_partial(&qid);
680 if (err)
681 break;
682 memcpy(qid->id, imsg.data + len +
683 i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
684 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
685 (*commit)->nparents++;
687 break;
688 default:
689 err = got_error(GOT_ERR_PRIVSEP_MSG);
690 break;
693 imsg_free(&imsg);
695 return err;
698 const struct got_error *
699 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_pathlist_head *entries,
700 int nentries)
702 const struct got_error *err = NULL;
703 struct got_imsg_tree_object itree;
704 struct got_pathlist_entry *pe;
705 size_t totlen;
706 int nimsg; /* number of imsg queued in ibuf */
708 itree.nentries = nentries;
709 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
710 == -1)
711 return got_error_from_errno("imsg_compose TREE");
713 totlen = sizeof(itree);
714 nimsg = 1;
715 TAILQ_FOREACH(pe, entries, entry) {
716 const char *name = pe->path;
717 struct got_parsed_tree_entry *pte = pe->data;
718 struct ibuf *wbuf;
719 size_t namelen = strlen(name);
720 size_t len = sizeof(struct got_imsg_tree_entry) + namelen;
722 if (len > MAX_IMSGSIZE)
723 return got_error(GOT_ERR_NO_SPACE);
725 nimsg++;
726 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
727 err = flush_imsg(ibuf);
728 if (err)
729 return err;
730 nimsg = 0;
733 wbuf = imsg_create(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, len);
734 if (wbuf == NULL)
735 return got_error_from_errno("imsg_create TREE_ENTRY");
737 /* Keep in sync with struct got_imsg_tree_object definition! */
738 if (imsg_add(wbuf, pte->id, SHA1_DIGEST_LENGTH) == -1) {
739 err = got_error_from_errno("imsg_add TREE_ENTRY");
740 ibuf_free(wbuf);
741 return err;
743 if (imsg_add(wbuf, &pte->mode, sizeof(pte->mode)) == -1) {
744 err = got_error_from_errno("imsg_add TREE_ENTRY");
745 ibuf_free(wbuf);
746 return err;
749 if (imsg_add(wbuf, name, namelen) == -1) {
750 err = got_error_from_errno("imsg_add TREE_ENTRY");
751 ibuf_free(wbuf);
752 return err;
755 wbuf->fd = -1;
756 imsg_close(ibuf, wbuf);
758 totlen += len;
761 return flush_imsg(ibuf);
764 const struct got_error *
765 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
767 const struct got_error *err = NULL;
768 const size_t min_datalen =
769 MIN(sizeof(struct got_imsg_error),
770 sizeof(struct got_imsg_tree_object));
771 struct got_imsg_tree_object *itree;
772 int nentries = 0;
774 *tree = NULL;
775 get_more:
776 err = read_imsg(ibuf);
777 if (err)
778 goto done;
780 for (;;) {
781 struct imsg imsg;
782 size_t n;
783 size_t datalen;
784 struct got_imsg_tree_entry *ite;
785 struct got_tree_entry *te = NULL;
787 n = imsg_get(ibuf, &imsg);
788 if (n == 0) {
789 if (*tree && (*tree)->nentries != nentries)
790 goto get_more;
791 break;
794 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
795 return got_error(GOT_ERR_PRIVSEP_LEN);
797 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
799 switch (imsg.hdr.type) {
800 case GOT_IMSG_ERROR:
801 err = recv_imsg_error(&imsg, datalen);
802 break;
803 case GOT_IMSG_TREE:
804 /* This message should only appear once. */
805 if (*tree != NULL) {
806 err = got_error(GOT_ERR_PRIVSEP_MSG);
807 break;
809 if (datalen != sizeof(*itree)) {
810 err = got_error(GOT_ERR_PRIVSEP_LEN);
811 break;
813 itree = imsg.data;
814 *tree = malloc(sizeof(**tree));
815 if (*tree == NULL) {
816 err = got_error_from_errno("malloc");
817 break;
819 (*tree)->entries = calloc(itree->nentries,
820 sizeof(struct got_tree_entry));
821 if ((*tree)->entries == NULL) {
822 err = got_error_from_errno("malloc");
823 break;
825 (*tree)->nentries = itree->nentries;
826 (*tree)->refcnt = 0;
827 break;
828 case GOT_IMSG_TREE_ENTRY:
829 /* This message should be preceeded by GOT_IMSG_TREE. */
830 if (*tree == NULL) {
831 err = got_error(GOT_ERR_PRIVSEP_MSG);
832 break;
834 if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
835 err = got_error(GOT_ERR_PRIVSEP_LEN);
836 break;
839 /* Remaining data contains the entry's name. */
840 datalen -= sizeof(*ite);
841 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
842 err = got_error(GOT_ERR_PRIVSEP_LEN);
843 break;
845 ite = imsg.data;
847 if (datalen + 1 > sizeof(te->name)) {
848 err = got_error(GOT_ERR_NO_SPACE);
849 break;
851 te = &(*tree)->entries[nentries];
852 memcpy(te->name, imsg.data + sizeof(*ite), datalen);
853 te->name[datalen] = '\0';
855 memcpy(te->id.sha1, ite->id, SHA1_DIGEST_LENGTH);
856 te->mode = ite->mode;
857 te->idx = nentries;
858 nentries++;
859 break;
860 default:
861 err = got_error(GOT_ERR_PRIVSEP_MSG);
862 break;
865 imsg_free(&imsg);
867 done:
868 if (*tree && (*tree)->nentries != nentries) {
869 if (err == NULL)
870 err = got_error(GOT_ERR_PRIVSEP_LEN);
871 got_object_tree_close(*tree);
872 *tree = NULL;
875 return err;
878 const struct got_error *
879 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
880 const uint8_t *data)
882 struct got_imsg_blob iblob;
884 iblob.size = size;
885 iblob.hdrlen = hdrlen;
887 if (data) {
888 uint8_t *buf;
890 if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
891 return got_error(GOT_ERR_NO_SPACE);
893 buf = malloc(sizeof(iblob) + size);
894 if (buf == NULL)
895 return got_error_from_errno("malloc");
897 memcpy(buf, &iblob, sizeof(iblob));
898 memcpy(buf + sizeof(iblob), data, size);
899 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
900 sizeof(iblob) + size) == -1) {
901 free(buf);
902 return got_error_from_errno("imsg_compose BLOB");
904 free(buf);
905 } else {
906 /* Data has already been written to file descriptor. */
907 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
908 sizeof(iblob)) == -1)
909 return got_error_from_errno("imsg_compose BLOB");
913 return flush_imsg(ibuf);
916 const struct got_error *
917 got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
918 struct imsgbuf *ibuf)
920 const struct got_error *err = NULL;
921 struct imsg imsg;
922 struct got_imsg_blob *iblob;
923 size_t datalen;
925 *outbuf = NULL;
927 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
928 if (err)
929 return err;
931 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
933 switch (imsg.hdr.type) {
934 case GOT_IMSG_BLOB:
935 if (datalen < sizeof(*iblob)) {
936 err = got_error(GOT_ERR_PRIVSEP_LEN);
937 break;
939 iblob = imsg.data;
940 *size = iblob->size;
941 *hdrlen = iblob->hdrlen;
943 if (datalen == sizeof(*iblob)) {
944 /* Data has been written to file descriptor. */
945 break;
948 if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
949 err = got_error(GOT_ERR_PRIVSEP_LEN);
950 break;
953 *outbuf = malloc(*size);
954 if (*outbuf == NULL) {
955 err = got_error_from_errno("malloc");
956 break;
958 memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
959 break;
960 default:
961 err = got_error(GOT_ERR_PRIVSEP_MSG);
962 break;
965 imsg_free(&imsg);
967 return err;
970 static const struct got_error *
971 send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
973 const struct got_error *err = NULL;
974 size_t offset, remain;
976 offset = 0;
977 remain = tagmsg_len;
978 while (remain > 0) {
979 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
981 if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
982 tag->tagmsg + offset, n) == -1) {
983 err = got_error_from_errno("imsg_compose TAG_TAGMSG");
984 break;
987 err = flush_imsg(ibuf);
988 if (err)
989 break;
991 offset += n;
992 remain -= n;
995 return err;
998 const struct got_error *
999 got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
1001 const struct got_error *err = NULL;
1002 struct got_imsg_tag_object *itag;
1003 uint8_t *buf;
1004 size_t len, total;
1005 size_t tag_len = strlen(tag->tag);
1006 size_t tagger_len = strlen(tag->tagger);
1007 size_t tagmsg_len = strlen(tag->tagmsg);
1009 total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
1011 buf = malloc(total);
1012 if (buf == NULL)
1013 return got_error_from_errno("malloc");
1015 itag = (struct got_imsg_tag_object *)buf;
1016 memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
1017 itag->obj_type = tag->obj_type;
1018 itag->tag_len = tag_len;
1019 itag->tagger_len = tagger_len;
1020 itag->tagger_time = tag->tagger_time;
1021 itag->tagger_gmtoff = tag->tagger_gmtoff;
1022 itag->tagmsg_len = tagmsg_len;
1024 len = sizeof(*itag);
1025 memcpy(buf + len, tag->tag, tag_len);
1026 len += tag_len;
1027 memcpy(buf + len, tag->tagger, tagger_len);
1028 len += tagger_len;
1030 if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
1031 err = got_error_from_errno("imsg_compose TAG");
1032 goto done;
1035 if (tagmsg_len == 0 ||
1036 tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
1037 err = flush_imsg(ibuf);
1038 if (err)
1039 goto done;
1041 err = send_tagmsg(ibuf, tag, tagmsg_len);
1042 done:
1043 free(buf);
1044 return err;
1047 const struct got_error *
1048 got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
1050 const struct got_error *err = NULL;
1051 struct imsg imsg;
1052 struct got_imsg_tag_object *itag;
1053 size_t len, datalen;
1054 const size_t min_datalen =
1055 MIN(sizeof(struct got_imsg_error),
1056 sizeof(struct got_imsg_tag_object));
1058 *tag = NULL;
1060 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1061 if (err)
1062 return err;
1064 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1065 len = 0;
1067 switch (imsg.hdr.type) {
1068 case GOT_IMSG_TAG:
1069 if (datalen < sizeof(*itag)) {
1070 err = got_error(GOT_ERR_PRIVSEP_LEN);
1071 break;
1073 itag = imsg.data;
1074 if (datalen != sizeof(*itag) + itag->tag_len +
1075 itag->tagger_len) {
1076 err = got_error(GOT_ERR_PRIVSEP_LEN);
1077 break;
1079 len += sizeof(*itag);
1081 *tag = calloc(1, sizeof(**tag));
1082 if (*tag == NULL) {
1083 err = got_error_from_errno("calloc");
1084 break;
1087 memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
1089 if (itag->tag_len == 0) {
1090 (*tag)->tag = strdup("");
1091 if ((*tag)->tag == NULL) {
1092 err = got_error_from_errno("strdup");
1093 break;
1095 } else {
1096 (*tag)->tag = malloc(itag->tag_len + 1);
1097 if ((*tag)->tag == NULL) {
1098 err = got_error_from_errno("malloc");
1099 break;
1101 memcpy((*tag)->tag, imsg.data + len,
1102 itag->tag_len);
1103 (*tag)->tag[itag->tag_len] = '\0';
1105 len += itag->tag_len;
1107 (*tag)->obj_type = itag->obj_type;
1108 (*tag)->tagger_time = itag->tagger_time;
1109 (*tag)->tagger_gmtoff = itag->tagger_gmtoff;
1111 if (itag->tagger_len == 0) {
1112 (*tag)->tagger = strdup("");
1113 if ((*tag)->tagger == NULL) {
1114 err = got_error_from_errno("strdup");
1115 break;
1117 } else {
1118 (*tag)->tagger = malloc(itag->tagger_len + 1);
1119 if ((*tag)->tagger == NULL) {
1120 err = got_error_from_errno("malloc");
1121 break;
1123 memcpy((*tag)->tagger, imsg.data + len,
1124 itag->tagger_len);
1125 (*tag)->tagger[itag->tagger_len] = '\0';
1127 len += itag->tagger_len;
1129 if (itag->tagmsg_len == 0) {
1130 (*tag)->tagmsg = strdup("");
1131 if ((*tag)->tagmsg == NULL) {
1132 err = got_error_from_errno("strdup");
1133 break;
1135 } else {
1136 size_t offset = 0, remain = itag->tagmsg_len;
1138 (*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
1139 if ((*tag)->tagmsg == NULL) {
1140 err = got_error_from_errno("malloc");
1141 break;
1143 while (remain > 0) {
1144 struct imsg imsg_log;
1145 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
1146 remain);
1148 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
1149 if (err)
1150 return err;
1152 if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
1153 return got_error(GOT_ERR_PRIVSEP_MSG);
1155 memcpy((*tag)->tagmsg + offset, imsg_log.data,
1156 n);
1157 imsg_free(&imsg_log);
1158 offset += n;
1159 remain -= n;
1161 (*tag)->tagmsg[itag->tagmsg_len] = '\0';
1164 break;
1165 default:
1166 err = got_error(GOT_ERR_PRIVSEP_MSG);
1167 break;
1170 imsg_free(&imsg);
1172 return err;
1175 const struct got_error *
1176 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
1177 struct got_packidx *packidx)
1179 const struct got_error *err = NULL;
1180 struct got_imsg_packidx ipackidx;
1181 struct got_imsg_pack ipack;
1182 int fd;
1184 ipackidx.len = packidx->len;
1185 fd = dup(packidx->fd);
1186 if (fd == -1)
1187 return got_error_from_errno("dup");
1189 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
1190 sizeof(ipackidx)) == -1) {
1191 err = got_error_from_errno("imsg_compose PACKIDX");
1192 close(fd);
1193 return err;
1196 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1197 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1198 return got_error(GOT_ERR_NO_SPACE);
1199 ipack.filesize = pack->filesize;
1201 fd = dup(pack->fd);
1202 if (fd == -1)
1203 return got_error_from_errno("dup");
1205 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1206 == -1) {
1207 err = got_error_from_errno("imsg_compose PACK");
1208 close(fd);
1209 return err;
1212 return flush_imsg(ibuf);
1215 const struct got_error *
1216 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1217 struct got_object_id *id)
1219 struct got_imsg_packed_object iobj;
1221 iobj.idx = idx;
1222 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1224 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1225 &iobj, sizeof(iobj)) == -1)
1226 return got_error_from_errno("imsg_compose "
1227 "PACKED_OBJECT_REQUEST");
1229 return flush_imsg(ibuf);
1232 const struct got_error *
1233 got_privsep_send_gitconfig_parse_req(struct imsgbuf *ibuf, int fd)
1235 const struct got_error *err = NULL;
1237 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_PARSE_REQUEST, 0, 0, fd,
1238 NULL, 0) == -1) {
1239 err = got_error_from_errno("imsg_compose "
1240 "GITCONFIG_PARSE_REQUEST");
1241 close(fd);
1242 return err;
1245 return flush_imsg(ibuf);
1248 const struct got_error *
1249 got_privsep_send_gitconfig_repository_format_version_req(struct imsgbuf *ibuf)
1251 if (imsg_compose(ibuf,
1252 GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST, 0, 0, -1,
1253 NULL, 0) == -1)
1254 return got_error_from_errno("imsg_compose "
1255 "GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST");
1257 return flush_imsg(ibuf);
1260 const struct got_error *
1261 got_privsep_send_gitconfig_author_name_req(struct imsgbuf *ibuf)
1263 if (imsg_compose(ibuf,
1264 GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST, 0, 0, -1, NULL, 0) == -1)
1265 return got_error_from_errno("imsg_compose "
1266 "GITCONFIG_AUTHOR_NAME_REQUEST");
1268 return flush_imsg(ibuf);
1271 const struct got_error *
1272 got_privsep_send_gitconfig_author_email_req(struct imsgbuf *ibuf)
1274 if (imsg_compose(ibuf,
1275 GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST, 0, 0, -1, NULL, 0) == -1)
1276 return got_error_from_errno("imsg_compose "
1277 "GITCONFIG_AUTHOR_EMAIL_REQUEST");
1279 return flush_imsg(ibuf);
1282 const struct got_error *
1283 got_privsep_send_gitconfig_remotes_req(struct imsgbuf *ibuf)
1285 if (imsg_compose(ibuf,
1286 GOT_IMSG_GITCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
1287 return got_error_from_errno("imsg_compose "
1288 "GITCONFIG_REMOTE_REQUEST");
1290 return flush_imsg(ibuf);
1293 const struct got_error *
1294 got_privsep_send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
1296 size_t len = value ? strlen(value) + 1 : 0;
1298 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
1299 value, len) == -1)
1300 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
1302 return flush_imsg(ibuf);
1305 const struct got_error *
1306 got_privsep_recv_gitconfig_str(char **str, struct imsgbuf *ibuf)
1308 const struct got_error *err = NULL;
1309 struct imsg imsg;
1310 size_t datalen;
1311 const size_t min_datalen = 0;
1313 *str = NULL;
1315 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1316 if (err)
1317 return err;
1318 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1320 switch (imsg.hdr.type) {
1321 case GOT_IMSG_GITCONFIG_STR_VAL:
1322 if (datalen == 0)
1323 break;
1324 *str = malloc(datalen);
1325 if (*str == NULL) {
1326 err = got_error_from_errno("malloc");
1327 break;
1329 if (strlcpy(*str, imsg.data, datalen) >= datalen)
1330 err = got_error(GOT_ERR_NO_SPACE);
1331 break;
1332 default:
1333 err = got_error(GOT_ERR_PRIVSEP_MSG);
1334 break;
1337 imsg_free(&imsg);
1338 return err;
1341 const struct got_error *
1342 got_privsep_send_gitconfig_int(struct imsgbuf *ibuf, int value)
1344 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
1345 &value, sizeof(value)) == -1)
1346 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
1348 return flush_imsg(ibuf);
1351 const struct got_error *
1352 got_privsep_recv_gitconfig_int(int *val, struct imsgbuf *ibuf)
1354 const struct got_error *err = NULL;
1355 struct imsg imsg;
1356 size_t datalen;
1357 const size_t min_datalen =
1358 MIN(sizeof(struct got_imsg_error), sizeof(int));
1360 *val = 0;
1362 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
1363 if (err)
1364 return err;
1365 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1367 switch (imsg.hdr.type) {
1368 case GOT_IMSG_GITCONFIG_INT_VAL:
1369 if (datalen != sizeof(*val)) {
1370 err = got_error(GOT_ERR_PRIVSEP_LEN);
1371 break;
1373 memcpy(val, imsg.data, sizeof(*val));
1374 break;
1375 default:
1376 err = got_error(GOT_ERR_PRIVSEP_MSG);
1377 break;
1380 imsg_free(&imsg);
1381 return err;
1384 const struct got_error *
1385 got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
1386 struct got_remote_repo *remotes, int nremotes)
1388 const struct got_error *err = NULL;
1389 struct got_imsg_remotes iremotes;
1390 int i;
1392 iremotes.nremotes = nremotes;
1393 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
1394 &iremotes, sizeof(iremotes)) == -1)
1395 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
1397 err = flush_imsg(ibuf);
1398 imsg_clear(ibuf);
1399 if (err)
1400 return err;
1402 for (i = 0; i < nremotes; i++) {
1403 struct got_imsg_remote iremote;
1404 size_t len = sizeof(iremote);
1405 struct ibuf *wbuf;
1407 iremote.name_len = strlen(remotes[i].name);
1408 len += iremote.name_len;
1409 iremote.url_len = strlen(remotes[i].url);
1410 len += iremote.url_len;
1412 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
1413 if (wbuf == NULL)
1414 return got_error_from_errno(
1415 "imsg_create GITCONFIG_REMOTE");
1417 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
1418 err = got_error_from_errno(
1419 "imsg_add GIITCONFIG_REMOTE");
1420 ibuf_free(wbuf);
1421 return err;
1424 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
1425 err = got_error_from_errno(
1426 "imsg_add GIITCONFIG_REMOTE");
1427 ibuf_free(wbuf);
1428 return err;
1430 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
1431 err = got_error_from_errno(
1432 "imsg_add GIITCONFIG_REMOTE");
1433 ibuf_free(wbuf);
1434 return err;
1437 wbuf->fd = -1;
1438 imsg_close(ibuf, wbuf);
1439 err = flush_imsg(ibuf);
1440 if (err)
1441 return err;
1444 return NULL;
1447 const struct got_error *
1448 got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
1449 int *nremotes, struct imsgbuf *ibuf)
1451 const struct got_error *err = NULL;
1452 struct imsg imsg;
1453 size_t datalen;
1454 struct got_imsg_remotes iremotes;
1455 struct got_imsg_remote iremote;
1457 *remotes = NULL;
1458 *nremotes = 0;
1460 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremotes));
1461 if (err)
1462 return err;
1463 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1465 switch (imsg.hdr.type) {
1466 case GOT_IMSG_GITCONFIG_REMOTES:
1467 if (datalen != sizeof(iremotes)) {
1468 err = got_error(GOT_ERR_PRIVSEP_LEN);
1469 break;
1471 memcpy(&iremotes, imsg.data, sizeof(iremotes));
1472 if (iremotes.nremotes == 0) {
1473 imsg_free(&imsg);
1474 return NULL;
1476 break;
1477 default:
1478 err = got_error(GOT_ERR_PRIVSEP_MSG);
1479 break;
1482 imsg_free(&imsg);
1484 *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(iremote));
1485 if (*remotes == NULL)
1486 return got_error_from_errno("recallocarray");
1488 while (*nremotes < iremotes.nremotes) {
1489 struct got_remote_repo *remote;
1491 err = got_privsep_recv_imsg(&imsg, ibuf, sizeof(iremote));
1492 if (err)
1493 break;
1494 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1496 switch (imsg.hdr.type) {
1497 case GOT_IMSG_GITCONFIG_REMOTE:
1498 remote = &(*remotes)[*nremotes];
1499 if (datalen < sizeof(iremote)) {
1500 err = got_error(GOT_ERR_PRIVSEP_LEN);
1501 break;
1503 memcpy(&iremote, imsg.data, sizeof(iremote));
1504 if (iremote.name_len == 0 || iremote.url_len == 0 ||
1505 (sizeof(iremote) + iremote.name_len +
1506 iremote.url_len) > datalen) {
1507 err = got_error(GOT_ERR_PRIVSEP_LEN);
1508 break;
1510 remote->name = strndup(imsg.data + sizeof(iremote),
1511 iremote.name_len);
1512 if (remote->name == NULL) {
1513 err = got_error_from_errno("strndup");
1514 break;
1516 remote->url = strndup(imsg.data + sizeof(iremote) +
1517 iremote.name_len, iremote.url_len);
1518 if (remote->url == NULL) {
1519 err = got_error_from_errno("strndup");
1520 free(remote->name);
1521 break;
1523 (*nremotes)++;
1524 break;
1525 default:
1526 err = got_error(GOT_ERR_PRIVSEP_MSG);
1527 break;
1530 imsg_free(&imsg);
1531 if (err)
1532 break;
1535 if (err) {
1536 int i;
1537 for (i = 0; i < *nremotes; i++) {
1538 free((*remotes)[i].name);
1539 free((*remotes)[i].url);
1541 free(*remotes);
1542 *remotes = NULL;
1543 *nremotes = 0;
1545 return err;
1548 const struct got_error *
1549 got_privsep_unveil_exec_helpers(void)
1551 const char *helpers[] = {
1552 GOT_PATH_PROG_READ_PACK,
1553 GOT_PATH_PROG_READ_OBJECT,
1554 GOT_PATH_PROG_READ_COMMIT,
1555 GOT_PATH_PROG_READ_TREE,
1556 GOT_PATH_PROG_READ_BLOB,
1557 GOT_PATH_PROG_READ_TAG,
1558 GOT_PATH_PROG_READ_GITCONFIG,
1560 int i;
1562 for (i = 0; i < nitems(helpers); i++) {
1563 if (unveil(helpers[i], "x") == 0)
1564 continue;
1565 return got_error_from_errno2("unveil", helpers[i]);
1568 return NULL;
1571 void
1572 got_privsep_exec_child(int imsg_fds[2], const char *path, const char *repo_path)
1574 if (close(imsg_fds[0]) != 0) {
1575 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1576 _exit(1);
1579 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1580 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1581 _exit(1);
1583 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1584 fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
1585 _exit(1);
1588 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1589 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1590 strerror(errno));
1591 _exit(1);