Hash :
1785f84a
Author :
Date :
2018-12-23T23:15:42
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
/*
* Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <sys/syslimits.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <poll.h>
#include <imsg.h>
#include <sha1.h>
#include <zlib.h>
#include <time.h>
#include "got_object.h"
#include "got_error.h"
#include "got_lib_sha1.h"
#include "got_lib_delta.h"
#include "got_lib_inflate.h"
#include "got_lib_object.h"
#include "got_lib_object_parse.h"
#include "got_lib_privsep.h"
#include "got_lib_pack.h"
#ifndef MIN
#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
#endif
static const struct got_error *
poll_fd(int fd, int events, int timeout)
{
struct pollfd pfd[1];
int n;
pfd[0].fd = fd;
pfd[0].events = events;
n = poll(pfd, 1, timeout);
if (n == -1)
return got_error_from_errno();
if (n == 0)
return got_error(GOT_ERR_TIMEOUT);
if (pfd[0].revents & (POLLERR | POLLNVAL))
return got_error_from_errno();
if (pfd[0].revents & (events | POLLHUP))
return NULL;
return got_error(GOT_ERR_INTERRUPT);
}
static const struct got_error *
read_imsg(struct imsgbuf *ibuf)
{
const struct got_error *err;
size_t n;
err = poll_fd(ibuf->fd, POLLIN, INFTIM);
if (err)
return err;
n = imsg_read(ibuf);
if (n == -1) {
if (errno == EAGAIN) /* Could be a file-descriptor leak. */
return got_error(GOT_ERR_PRIVSEP_NO_FD);
return got_error(GOT_ERR_PRIVSEP_READ);
}
if (n == 0)
return got_error(GOT_ERR_PRIVSEP_PIPE);
return NULL;
}
const struct got_error *
got_privsep_wait_for_child(pid_t pid)
{
int child_status;
waitpid(pid, &child_status, 0);
if (!WIFEXITED(child_status))
return got_error(GOT_ERR_PRIVSEP_DIED);
if (WEXITSTATUS(child_status) != 0)
return got_error(GOT_ERR_PRIVSEP_EXIT);
return NULL;
}
static const struct got_error *
recv_imsg_error(struct imsg *imsg, size_t datalen)
{
struct got_imsg_error *ierr;
if (datalen != sizeof(*ierr))
return got_error(GOT_ERR_PRIVSEP_LEN);
ierr = imsg->data;
if (ierr->code == GOT_ERR_ERRNO) {
static struct got_error serr;
serr.code = GOT_ERR_ERRNO;
serr.msg = strerror(ierr->errno_code);
return &serr;
}
return got_error(ierr->code);
}
const struct got_error *
got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf,
size_t min_datalen)
{
const struct got_error *err;
ssize_t n;
n = imsg_get(ibuf, imsg);
if (n == -1)
return got_error_from_errno();
while (n == 0) {
err = read_imsg(ibuf);
if (err)
return err;
n = imsg_get(ibuf, imsg);
}
if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
return got_error(GOT_ERR_PRIVSEP_LEN);
if (imsg->hdr.type == GOT_IMSG_ERROR) {
size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
return recv_imsg_error(imsg, datalen);
}
return NULL;
}
/* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
void
got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
{
const struct got_error *poll_err;
struct got_imsg_error ierr;
int ret;
ierr.code = err->code;
if (err->code == GOT_ERR_ERRNO)
ierr.errno_code = errno;
else
ierr.errno_code = 0;
ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
if (ret == -1) {
fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
getprogname(), err->code, err->msg, strerror(errno));
return;
}
poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
if (poll_err) {
fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
getprogname(), err->code, err->msg, poll_err->msg);
return;
}
ret = imsg_flush(ibuf);
if (ret == -1) {
fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
getprogname(), err->code, err->msg, strerror(errno));
return;
}
}
static const struct got_error *
flush_imsg(struct imsgbuf *ibuf)
{
const struct got_error *err;
err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
if (err)
return err;
if (imsg_flush(ibuf) == -1)
return got_error_from_errno();
return NULL;
}
const struct got_error *
got_privsep_send_stop(int fd)
{
const struct got_error *err = NULL;
struct imsgbuf ibuf;
imsg_init(&ibuf, fd);
if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
return got_error_from_errno();
err = flush_imsg(&ibuf);
imsg_clear(&ibuf);
return err;
}
const struct got_error *
got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
{
struct got_imsg_object iobj, *iobjp = NULL;
size_t iobj_size = 0;
int imsg_code = GOT_IMSG_OBJECT_REQUEST;
if (obj) {
switch (obj->type) {
case GOT_OBJ_TYPE_TREE:
imsg_code = GOT_IMSG_TREE_REQUEST;
break;
case GOT_OBJ_TYPE_COMMIT:
abort(); /* should not get here */
break;
case GOT_OBJ_TYPE_BLOB:
imsg_code = GOT_IMSG_BLOB_REQUEST;
break;
case GOT_OBJ_TYPE_TAG:
imsg_code = GOT_IMSG_TAG_REQUEST;
break;
default:
return got_error(GOT_ERR_OBJ_TYPE);
}
memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
iobj.type = obj->type;
iobj.flags = obj->flags;
iobj.hdrlen = obj->hdrlen;
iobj.size = obj->size;
if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
iobj.pack_offset = obj->pack_offset;
iobj.pack_idx = obj->pack_idx;
}
iobjp = &iobj;
iobj_size = sizeof(iobj);
}
if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
return got_error_from_errno();
return flush_imsg(ibuf);
}
const struct got_error *
got_privsep_send_commit_req(struct imsgbuf *ibuf, int fd,
struct got_object_id *id, int pack_idx)
{
struct got_imsg_packed_object iobj, *iobjp;
size_t len;
if (id) { /* commit is packed */
iobj.idx = pack_idx;
memcpy(iobj.id, id->sha1, sizeof(iobj.id));
iobjp = &iobj;
len = sizeof(iobj);
} else {
iobjp = NULL;
len = 0;
}
if (imsg_compose(ibuf, GOT_IMSG_COMMIT_REQUEST, 0, 0, fd, iobjp, len)
== -1)
return got_error_from_errno();
return flush_imsg(ibuf);
}
const struct got_error *
got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
{
if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
== -1)
return got_error_from_errno();
return flush_imsg(ibuf);
}
const struct got_error *
got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
{
if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
== -1)
return got_error_from_errno();
return flush_imsg(ibuf);
}
const struct got_error *
got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
{
if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
== -1)
return got_error_from_errno();
return flush_imsg(ibuf);
}
const struct got_error *
got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
{
struct got_imsg_object iobj;
memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
iobj.type = obj->type;
iobj.flags = obj->flags;
iobj.hdrlen = obj->hdrlen;
iobj.size = obj->size;
if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
iobj.pack_offset = obj->pack_offset;
iobj.pack_idx = obj->pack_idx;
}
if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
== -1)
return got_error_from_errno();
return flush_imsg(ibuf);
}
const struct got_error *
got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
struct got_imsg_object *iobj;
size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
if (datalen != sizeof(*iobj))
return got_error(GOT_ERR_PRIVSEP_LEN);
iobj = imsg->data;
*obj = calloc(1, sizeof(**obj));
if (*obj == NULL)
return got_error_from_errno();
memcpy((*obj)->id.sha1, iobj->id, SHA1_DIGEST_LENGTH);
(*obj)->type = iobj->type;
(*obj)->flags = iobj->flags;
(*obj)->hdrlen = iobj->hdrlen;
(*obj)->size = iobj->size;
/* path_packfile is handled by caller */
if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
(*obj)->pack_offset = iobj->pack_offset;
(*obj)->pack_idx = iobj->pack_idx;
}
return err;
}
const struct got_error *
got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
struct imsg imsg;
size_t datalen;
const size_t min_datalen =
MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
*obj = NULL;
err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
if (err)
return err;
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
switch (imsg.hdr.type) {
case GOT_IMSG_OBJECT:
err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
break;
default:
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;
}
imsg_free(&imsg);
return err;
}
static const struct got_error *
send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
size_t logmsg_len)
{
const struct got_error *err = NULL;
size_t offset, remain;
offset = 0;
remain = logmsg_len;
while (remain > 0) {
size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
commit->logmsg + offset, n) == -1) {
err = got_error_from_errno();
break;
}
err = flush_imsg(ibuf);
if (err)
break;
offset += n;
remain -= n;
}
return err;
}
const struct got_error *
got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
{
const struct got_error *err = NULL;
struct got_imsg_commit_object *icommit;
uint8_t *buf;
size_t len, total;
struct got_object_qid *qid;
size_t author_len = strlen(commit->author);
size_t committer_len = strlen(commit->committer);
size_t logmsg_len = strlen(commit->logmsg);
total = sizeof(*icommit) + author_len + committer_len +
commit->nparents * SHA1_DIGEST_LENGTH;
buf = malloc(total);
if (buf == NULL)
return got_error_from_errno();
icommit = (struct got_imsg_commit_object *)buf;
memcpy(icommit->tree_id, commit->tree_id->sha1, sizeof(icommit->tree_id));
icommit->author_len = author_len;
icommit->author_time = commit->author_time;
icommit->author_gmtoff = commit->author_gmtoff;
icommit->committer_len = committer_len;
icommit->committer_time = commit->committer_time;
icommit->committer_gmtoff = commit->committer_gmtoff;
icommit->logmsg_len = logmsg_len;
icommit->nparents = commit->nparents;
len = sizeof(*icommit);
memcpy(buf + len, commit->author, author_len);
len += author_len;
memcpy(buf + len, commit->committer, committer_len);
len += committer_len;
SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
len += SHA1_DIGEST_LENGTH;
}
if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
err = got_error_from_errno();
goto done;
}
if (logmsg_len == 0 ||
logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
err = flush_imsg(ibuf);
if (err)
goto done;
}
err = send_commit_logmsg(ibuf, commit, logmsg_len);
done:
free(buf);
return err;
}
const struct got_error *
got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
struct imsg imsg;
struct got_imsg_commit_object *icommit;
size_t len, datalen;
int i;
const size_t min_datalen =
MIN(sizeof(struct got_imsg_error),
sizeof(struct got_imsg_commit_object));
*commit = NULL;
err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
if (err)
return err;
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
len = 0;
switch (imsg.hdr.type) {
case GOT_IMSG_COMMIT:
if (datalen < sizeof(*icommit)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
icommit = imsg.data;
if (datalen != sizeof(*icommit) + icommit->author_len +
icommit->committer_len +
icommit->nparents * SHA1_DIGEST_LENGTH) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
if (icommit->nparents < 0) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
len += sizeof(*icommit);
*commit = got_object_commit_alloc_partial();
if (*commit == NULL) {
err = got_error_from_errno();
break;
}
memcpy((*commit)->tree_id->sha1, icommit->tree_id,
SHA1_DIGEST_LENGTH);
(*commit)->author_time = icommit->author_time;
(*commit)->author_gmtoff = icommit->author_gmtoff;
(*commit)->committer_time = icommit->committer_time;
(*commit)->committer_gmtoff = icommit->committer_gmtoff;
if (icommit->author_len == 0) {
(*commit)->author = strdup("");
if ((*commit)->author == NULL) {
err = got_error_from_errno();
break;
}
} else {
(*commit)->author = malloc(icommit->author_len + 1);
if ((*commit)->author == NULL) {
err = got_error_from_errno();
break;
}
memcpy((*commit)->author, imsg.data + len,
icommit->author_len);
(*commit)->author[icommit->author_len] = '\0';
}
len += icommit->author_len;
if (icommit->committer_len == 0) {
(*commit)->committer = strdup("");
if ((*commit)->committer == NULL) {
err = got_error_from_errno();
break;
}
} else {
(*commit)->committer =
malloc(icommit->committer_len + 1);
if ((*commit)->committer == NULL) {
err = got_error_from_errno();
break;
}
memcpy((*commit)->committer, imsg.data + len,
icommit->committer_len);
(*commit)->committer[icommit->committer_len] = '\0';
}
len += icommit->committer_len;
if (icommit->logmsg_len == 0) {
(*commit)->logmsg = strdup("");
if ((*commit)->logmsg == NULL) {
err = got_error_from_errno();
break;
}
} else {
size_t offset = 0, remain = icommit->logmsg_len;
(*commit)->logmsg = malloc(icommit->logmsg_len + 1);
if ((*commit)->logmsg == NULL) {
err = got_error_from_errno();
break;
}
while (remain > 0) {
struct imsg imsg_log;
size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
remain);
err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
if (err)
return err;
if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
return got_error(GOT_ERR_PRIVSEP_MSG);
memcpy((*commit)->logmsg + offset,
imsg_log.data, n);
imsg_free(&imsg_log);
offset += n;
remain -= n;
}
(*commit)->logmsg[icommit->logmsg_len] = '\0';
}
for (i = 0; i < icommit->nparents; i++) {
struct got_object_qid *qid;
err = got_object_qid_alloc_partial(&qid);
if (err)
break;
memcpy(qid->id, imsg.data + len +
i * SHA1_DIGEST_LENGTH, sizeof(*qid->id));
SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
(*commit)->nparents++;
}
break;
default:
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;
}
imsg_free(&imsg);
return err;
}
const struct got_error *
got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
{
const struct got_error *err = NULL;
struct got_imsg_tree_object itree;
struct got_tree_entry *te;
size_t totlen;
int nimsg; /* number of imsg queued in ibuf */
itree.nentries = tree->entries.nentries;
if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
== -1)
return got_error_from_errno();
totlen = sizeof(itree);
nimsg = 1;
SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
struct got_imsg_tree_entry *ite;
uint8_t *buf = NULL;
size_t len = sizeof(*ite) + strlen(te->name);
if (len > MAX_IMSGSIZE)
return got_error(GOT_ERR_NO_SPACE);
nimsg++;
if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
err = flush_imsg(ibuf);
if (err)
return err;
nimsg = 0;
}
buf = malloc(len);
if (buf == NULL)
return got_error_from_errno();
ite = (struct got_imsg_tree_entry *)buf;
memcpy(ite->id, te->id->sha1, sizeof(ite->id));
ite->mode = te->mode;
memcpy(buf + sizeof(*ite), te->name, strlen(te->name));
if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
buf, len) == -1)
err = got_error_from_errno();
free(buf);
if (err)
return err;
totlen += len;
}
return flush_imsg(ibuf);
}
const struct got_error *
got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
const size_t min_datalen =
MIN(sizeof(struct got_imsg_error),
sizeof(struct got_imsg_tree_object));
struct got_imsg_tree_object *itree;
int nentries = 0;
*tree = NULL;
get_more:
err = read_imsg(ibuf);
if (err)
goto done;
while (1) {
struct imsg imsg;
size_t n;
size_t datalen;
struct got_imsg_tree_entry *ite;
struct got_tree_entry *te = NULL;
n = imsg_get(ibuf, &imsg);
if (n == 0) {
if (*tree && (*tree)->entries.nentries != nentries)
goto get_more;
break;
}
if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
return got_error(GOT_ERR_PRIVSEP_LEN);
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
switch (imsg.hdr.type) {
case GOT_IMSG_ERROR:
err = recv_imsg_error(&imsg, datalen);
break;
case GOT_IMSG_TREE:
/* This message should only appear once. */
if (*tree != NULL) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;
}
if (datalen != sizeof(*itree)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
itree = imsg.data;
*tree = malloc(sizeof(**tree));
if (*tree == NULL) {
err = got_error_from_errno();
break;
}
(*tree)->entries.nentries = itree->nentries;
SIMPLEQ_INIT(&(*tree)->entries.head);
(*tree)->refcnt = 0;
break;
case GOT_IMSG_TREE_ENTRY:
/* This message should be preceeded by GOT_IMSG_TREE. */
if (*tree == NULL) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;
}
if (datalen < sizeof(*ite) || datalen > MAX_IMSGSIZE) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
/* Remaining data contains the entry's name. */
datalen -= sizeof(*ite);
if (datalen == 0 || datalen > MAX_IMSGSIZE) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
ite = imsg.data;
te = got_alloc_tree_entry_partial();
if (te == NULL) {
err = got_error_from_errno();
break;
}
te->name = malloc(datalen + 1);
if (te->name == NULL) {
free(te);
err = got_error_from_errno();
break;
}
memcpy(te->name, imsg.data + sizeof(*ite), datalen);
te->name[datalen] = '\0';
memcpy(te->id->sha1, ite->id, SHA1_DIGEST_LENGTH);
te->mode = ite->mode;
SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
nentries++;
break;
default:
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;
}
imsg_free(&imsg);
}
done:
if (*tree && (*tree)->entries.nentries != nentries) {
if (err == NULL)
err = got_error(GOT_ERR_PRIVSEP_LEN);
got_object_tree_close(*tree);
*tree = NULL;
}
return err;
}
const struct got_error *
got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
{
struct got_imsg_blob iblob;
iblob.size = size;
/* Data has already been written to file descriptor. */
if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
== -1)
return got_error_from_errno();
return flush_imsg(ibuf);
}
const struct got_error *
got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
struct imsg imsg;
struct got_imsg_blob *iblob;
size_t datalen;
err = got_privsep_recv_imsg(&imsg, ibuf, 0);
if (err)
return err;
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
switch (imsg.hdr.type) {
case GOT_IMSG_BLOB:
if (datalen != sizeof(*iblob)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
iblob = imsg.data;
*size = iblob->size;
/* Data has been written to file descriptor. */
break;
default:
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;
}
imsg_free(&imsg);
return err;
}
static const struct got_error *
send_tagmsg(struct imsgbuf *ibuf, struct got_tag_object *tag, size_t tagmsg_len)
{
const struct got_error *err = NULL;
size_t offset, remain;
offset = 0;
remain = tagmsg_len;
while (remain > 0) {
size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
if (imsg_compose(ibuf, GOT_IMSG_TAG_TAGMSG, 0, 0, -1,
tag->tagmsg + offset, n) == -1) {
err = got_error_from_errno();
break;
}
err = flush_imsg(ibuf);
if (err)
break;
offset += n;
remain -= n;
}
return err;
}
const struct got_error *
got_privsep_send_tag(struct imsgbuf *ibuf, struct got_tag_object *tag)
{
const struct got_error *err = NULL;
struct got_imsg_tag_object *itag;
uint8_t *buf;
size_t len, total;
size_t tag_len = strlen(tag->tag);
size_t tagger_len = strlen(tag->tagger);
size_t tagmsg_len = strlen(tag->tagmsg);
total = sizeof(*itag) + tag_len + tagger_len + tagmsg_len;
buf = malloc(total);
if (buf == NULL)
return got_error_from_errno();
itag = (struct got_imsg_tag_object *)buf;
memcpy(itag->id, tag->id.sha1, sizeof(itag->id));
itag->obj_type = tag->obj_type;
itag->tag_len = tag_len;
itag->tagger_len = tagger_len;
itag->tagger_time = tag->tagger_time;
itag->tagger_gmtoff = tag->tagger_gmtoff;
itag->tagmsg_len = tagmsg_len;
len = sizeof(*itag);
memcpy(buf + len, tag->tag, tag_len);
len += tag_len;
memcpy(buf + len, tag->tagger, tagger_len);
len += tagger_len;
if (imsg_compose(ibuf, GOT_IMSG_TAG, 0, 0, -1, buf, len) == -1) {
err = got_error_from_errno();
goto done;
}
if (tagmsg_len == 0 ||
tagmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
err = flush_imsg(ibuf);
if (err)
goto done;
}
err = send_tagmsg(ibuf, tag, tagmsg_len);
done:
free(buf);
return err;
}
const struct got_error *
got_privsep_recv_tag(struct got_tag_object **tag, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
struct imsg imsg;
struct got_imsg_tag_object *itag;
size_t len, datalen;
const size_t min_datalen =
MIN(sizeof(struct got_imsg_error),
sizeof(struct got_imsg_tag_object));
*tag = NULL;
err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
if (err)
return err;
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
len = 0;
switch (imsg.hdr.type) {
case GOT_IMSG_TAG:
if (datalen < sizeof(*itag)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
itag = imsg.data;
if (datalen != sizeof(*itag) + itag->tag_len +
itag->tagger_len) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
len += sizeof(*itag);
*tag = calloc(1, sizeof(**tag));
if (*tag == NULL) {
err = got_error_from_errno();
break;
}
memcpy((*tag)->id.sha1, itag->id, SHA1_DIGEST_LENGTH);
if (itag->tag_len == 0) {
(*tag)->tag = strdup("");
if ((*tag)->tag == NULL) {
err = got_error_from_errno();
break;
}
} else {
(*tag)->tag = malloc(itag->tag_len + 1);
if ((*tag)->tag == NULL) {
err = got_error_from_errno();
break;
}
memcpy((*tag)->tag, imsg.data + len,
itag->tag_len);
(*tag)->tag[itag->tag_len] = '\0';
}
len += itag->tag_len;
(*tag)->obj_type = itag->obj_type;
(*tag)->tagger_time = itag->tagger_time;
(*tag)->tagger_gmtoff = itag->tagger_gmtoff;
if (itag->tagger_len == 0) {
(*tag)->tagger = strdup("");
if ((*tag)->tagger == NULL) {
err = got_error_from_errno();
break;
}
} else {
(*tag)->tagger = malloc(itag->tagger_len + 1);
if ((*tag)->tagger == NULL) {
err = got_error_from_errno();
break;
}
memcpy((*tag)->tagger, imsg.data + len,
itag->tagger_len);
(*tag)->tagger[itag->tagger_len] = '\0';
}
len += itag->tagger_len;
if (itag->tagmsg_len == 0) {
(*tag)->tagmsg = strdup("");
if ((*tag)->tagmsg == NULL) {
err = got_error_from_errno();
break;
}
} else {
size_t offset = 0, remain = itag->tagmsg_len;
(*tag)->tagmsg = malloc(itag->tagmsg_len + 1);
if ((*tag)->tagmsg == NULL) {
err = got_error_from_errno();
break;
}
while (remain > 0) {
struct imsg imsg_log;
size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
remain);
err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
if (err)
return err;
if (imsg_log.hdr.type != GOT_IMSG_TAG_TAGMSG)
return got_error(GOT_ERR_PRIVSEP_MSG);
memcpy((*tag)->tagmsg + offset, imsg_log.data,
n);
imsg_free(&imsg_log);
offset += n;
remain -= n;
}
(*tag)->tagmsg[itag->tagmsg_len] = '\0';
}
break;
default:
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;
}
imsg_free(&imsg);
return err;
}
const struct got_error *
got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
struct got_packidx *packidx)
{
struct got_imsg_packidx ipackidx;
struct got_imsg_pack ipack;
int fd;
ipackidx.len = packidx->len;
fd = dup(packidx->fd);
if (fd == -1)
return got_error_from_errno();
if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
sizeof(ipackidx)) == -1)
return got_error_from_errno();
if (strlcpy(ipack.path_packfile, pack->path_packfile,
sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
return got_error(GOT_ERR_NO_SPACE);
ipack.filesize = pack->filesize;
fd = dup(pack->fd);
if (fd == -1)
return got_error_from_errno();
if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
== -1)
return got_error_from_errno();
return flush_imsg(ibuf);
}
const struct got_error *
got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
struct got_object_id *id)
{
struct got_imsg_packed_object iobj;
iobj.idx = idx;
memcpy(iobj.id, id->sha1, sizeof(iobj.id));
if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
&iobj, sizeof(iobj)) == -1)
return got_error_from_errno();
return flush_imsg(ibuf);
}