pkg.c revision 268896
1/*- 2 * Copyright (c) 2012-2014 Baptiste Daroussin <bapt@FreeBSD.org> 3 * Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: stable/10/usr.sbin/pkg/pkg.c 268896 2014-07-19 23:44:57Z bapt $"); 30 31#include <sys/param.h> 32#include <sys/queue.h> 33#include <sys/types.h> 34#include <sys/sbuf.h> 35#include <sys/wait.h> 36 37#define _WITH_GETLINE 38#include <archive.h> 39#include <archive_entry.h> 40#include <dirent.h> 41#include <err.h> 42#include <errno.h> 43#include <fcntl.h> 44#include <fetch.h> 45#include <paths.h> 46#include <stdbool.h> 47#include <stdlib.h> 48#include <stdio.h> 49#include <string.h> 50#include <time.h> 51#include <unistd.h> 52#include <ucl.h> 53 54#include <openssl/err.h> 55#include <openssl/ssl.h> 56 57#include "dns_utils.h" 58#include "config.h" 59 60struct sig_cert { 61 char *name; 62 unsigned char *sig; 63 int siglen; 64 unsigned char *cert; 65 int certlen; 66 bool trusted; 67}; 68 69typedef enum { 70 HASH_UNKNOWN, 71 HASH_SHA256, 72} hash_t; 73 74struct fingerprint { 75 hash_t type; 76 char *name; 77 char hash[BUFSIZ]; 78 STAILQ_ENTRY(fingerprint) next; 79}; 80 81STAILQ_HEAD(fingerprint_list, fingerprint); 82 83static int 84extract_pkg_static(int fd, char *p, int sz) 85{ 86 struct archive *a; 87 struct archive_entry *ae; 88 char *end; 89 int ret, r; 90 91 ret = -1; 92 a = archive_read_new(); 93 if (a == NULL) { 94 warn("archive_read_new"); 95 return (ret); 96 } 97 archive_read_support_filter_all(a); 98 archive_read_support_format_tar(a); 99 100 if (lseek(fd, 0, 0) == -1) { 101 warn("lseek"); 102 goto cleanup; 103 } 104 105 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) { 106 warnx("archive_read_open_fd: %s", archive_error_string(a)); 107 goto cleanup; 108 } 109 110 ae = NULL; 111 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) { 112 end = strrchr(archive_entry_pathname(ae), '/'); 113 if (end == NULL) 114 continue; 115 116 if (strcmp(end, "/pkg-static") == 0) { 117 r = archive_read_extract(a, ae, 118 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM | 119 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL | 120 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR); 121 strlcpy(p, archive_entry_pathname(ae), sz); 122 break; 123 } 124 } 125 126 if (r == ARCHIVE_OK) 127 ret = 0; 128 else 129 warnx("fail to extract pkg-static"); 130 131cleanup: 132 archive_read_free(a); 133 return (ret); 134 135} 136 137static int 138install_pkg_static(const char *path, const char *pkgpath, bool force) 139{ 140 int pstat; 141 pid_t pid; 142 143 switch ((pid = fork())) { 144 case -1: 145 return (-1); 146 case 0: 147 if (force) 148 execl(path, "pkg-static", "add", "-f", pkgpath, 149 (char *)NULL); 150 else 151 execl(path, "pkg-static", "add", pkgpath, 152 (char *)NULL); 153 _exit(1); 154 default: 155 break; 156 } 157 158 while (waitpid(pid, &pstat, 0) == -1) 159 if (errno != EINTR) 160 return (-1); 161 162 if (WEXITSTATUS(pstat)) 163 return (WEXITSTATUS(pstat)); 164 else if (WIFSIGNALED(pstat)) 165 return (128 & (WTERMSIG(pstat))); 166 return (pstat); 167} 168 169static int 170fetch_to_fd(const char *url, char *path) 171{ 172 struct url *u; 173 struct dns_srvinfo *mirrors, *current; 174 struct url_stat st; 175 FILE *remote; 176 /* To store _https._tcp. + hostname + \0 */ 177 int fd; 178 int retry, max_retry; 179 off_t done, r; 180 time_t now, last; 181 char buf[10240]; 182 char zone[MAXHOSTNAMELEN + 13]; 183 static const char *mirror_type = NULL; 184 185 done = 0; 186 last = 0; 187 max_retry = 3; 188 current = mirrors = NULL; 189 remote = NULL; 190 191 if (mirror_type == NULL && config_string(MIRROR_TYPE, &mirror_type) 192 != 0) { 193 warnx("No MIRROR_TYPE defined"); 194 return (-1); 195 } 196 197 if ((fd = mkstemp(path)) == -1) { 198 warn("mkstemp()"); 199 return (-1); 200 } 201 202 retry = max_retry; 203 204 u = fetchParseURL(url); 205 while (remote == NULL) { 206 if (retry == max_retry) { 207 if (strcmp(u->scheme, "file") != 0 && 208 strcasecmp(mirror_type, "srv") == 0) { 209 snprintf(zone, sizeof(zone), 210 "_%s._tcp.%s", u->scheme, u->host); 211 mirrors = dns_getsrvinfo(zone); 212 current = mirrors; 213 } 214 } 215 216 if (mirrors != NULL) { 217 strlcpy(u->host, current->host, sizeof(u->host)); 218 u->port = current->port; 219 } 220 221 remote = fetchXGet(u, &st, ""); 222 if (remote == NULL) { 223 --retry; 224 if (retry <= 0) 225 goto fetchfail; 226 if (mirrors == NULL) { 227 sleep(1); 228 } else { 229 current = current->next; 230 if (current == NULL) 231 current = mirrors; 232 } 233 } 234 } 235 236 if (remote == NULL) 237 goto fetchfail; 238 239 while (done < st.size) { 240 if ((r = fread(buf, 1, sizeof(buf), remote)) < 1) 241 break; 242 243 if (write(fd, buf, r) != r) { 244 warn("write()"); 245 goto fetchfail; 246 } 247 248 done += r; 249 now = time(NULL); 250 if (now > last || done == st.size) 251 last = now; 252 } 253 254 if (ferror(remote)) 255 goto fetchfail; 256 257 goto cleanup; 258 259fetchfail: 260 if (fd != -1) { 261 close(fd); 262 fd = -1; 263 unlink(path); 264 } 265 266cleanup: 267 if (remote != NULL) 268 fclose(remote); 269 270 return fd; 271} 272 273static struct fingerprint * 274parse_fingerprint(ucl_object_t *obj) 275{ 276 const ucl_object_t *cur; 277 ucl_object_iter_t it = NULL; 278 const char *function, *fp, *key; 279 struct fingerprint *f; 280 hash_t fct = HASH_UNKNOWN; 281 282 function = fp = NULL; 283 284 while ((cur = ucl_iterate_object(obj, &it, true))) { 285 key = ucl_object_key(cur); 286 if (cur->type != UCL_STRING) 287 continue; 288 if (strcasecmp(key, "function") == 0) { 289 function = ucl_object_tostring(cur); 290 continue; 291 } 292 if (strcasecmp(key, "fingerprint") == 0) { 293 fp = ucl_object_tostring(cur); 294 continue; 295 } 296 } 297 298 if (fp == NULL || function == NULL) 299 return (NULL); 300 301 if (strcasecmp(function, "sha256") == 0) 302 fct = HASH_SHA256; 303 304 if (fct == HASH_UNKNOWN) { 305 warnx("Unsupported hashing function: %s", function); 306 return (NULL); 307 } 308 309 f = calloc(1, sizeof(struct fingerprint)); 310 f->type = fct; 311 strlcpy(f->hash, fp, sizeof(f->hash)); 312 313 return (f); 314} 315 316static void 317free_fingerprint_list(struct fingerprint_list* list) 318{ 319 struct fingerprint *fingerprint, *tmp; 320 321 STAILQ_FOREACH_SAFE(fingerprint, list, next, tmp) { 322 if (fingerprint->name) 323 free(fingerprint->name); 324 free(fingerprint); 325 } 326 free(list); 327} 328 329static struct fingerprint * 330load_fingerprint(const char *dir, const char *filename) 331{ 332 ucl_object_t *obj = NULL; 333 struct ucl_parser *p = NULL; 334 struct fingerprint *f; 335 char path[MAXPATHLEN]; 336 337 f = NULL; 338 339 snprintf(path, MAXPATHLEN, "%s/%s", dir, filename); 340 341 p = ucl_parser_new(0); 342 if (!ucl_parser_add_file(p, path)) { 343 warnx("%s: %s", path, ucl_parser_get_error(p)); 344 ucl_parser_free(p); 345 return (NULL); 346 } 347 348 obj = ucl_parser_get_object(p); 349 350 if (obj->type == UCL_OBJECT) 351 f = parse_fingerprint(obj); 352 353 if (f != NULL) 354 f->name = strdup(filename); 355 356 ucl_object_unref(obj); 357 ucl_parser_free(p); 358 359 return (f); 360} 361 362static struct fingerprint_list * 363load_fingerprints(const char *path, int *count) 364{ 365 DIR *d; 366 struct dirent *ent; 367 struct fingerprint *finger; 368 struct fingerprint_list *fingerprints; 369 370 *count = 0; 371 372 fingerprints = calloc(1, sizeof(struct fingerprint_list)); 373 if (fingerprints == NULL) 374 return (NULL); 375 STAILQ_INIT(fingerprints); 376 377 if ((d = opendir(path)) == NULL) 378 return (NULL); 379 380 while ((ent = readdir(d))) { 381 if (strcmp(ent->d_name, ".") == 0 || 382 strcmp(ent->d_name, "..") == 0) 383 continue; 384 finger = load_fingerprint(path, ent->d_name); 385 if (finger != NULL) { 386 STAILQ_INSERT_TAIL(fingerprints, finger, next); 387 ++(*count); 388 } 389 } 390 391 closedir(d); 392 393 return (fingerprints); 394} 395 396static void 397sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH], 398 char out[SHA256_DIGEST_LENGTH * 2 + 1]) 399{ 400 int i; 401 402 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) 403 sprintf(out + (i * 2), "%02x", hash[i]); 404 405 out[SHA256_DIGEST_LENGTH * 2] = '\0'; 406} 407 408static void 409sha256_buf(char *buf, size_t len, char out[SHA256_DIGEST_LENGTH * 2 + 1]) 410{ 411 unsigned char hash[SHA256_DIGEST_LENGTH]; 412 SHA256_CTX sha256; 413 414 out[0] = '\0'; 415 416 SHA256_Init(&sha256); 417 SHA256_Update(&sha256, buf, len); 418 SHA256_Final(hash, &sha256); 419 sha256_hash(hash, out); 420} 421 422static int 423sha256_fd(int fd, char out[SHA256_DIGEST_LENGTH * 2 + 1]) 424{ 425 int my_fd; 426 FILE *fp; 427 char buffer[BUFSIZ]; 428 unsigned char hash[SHA256_DIGEST_LENGTH]; 429 size_t r; 430 int ret; 431 SHA256_CTX sha256; 432 433 my_fd = -1; 434 fp = NULL; 435 r = 0; 436 ret = 1; 437 438 out[0] = '\0'; 439 440 /* Duplicate the fd so that fclose(3) does not close it. */ 441 if ((my_fd = dup(fd)) == -1) { 442 warnx("dup"); 443 goto cleanup; 444 } 445 446 if ((fp = fdopen(my_fd, "rb")) == NULL) { 447 warnx("fdopen"); 448 goto cleanup; 449 } 450 451 SHA256_Init(&sha256); 452 453 while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0) 454 SHA256_Update(&sha256, buffer, r); 455 456 if (ferror(fp) != 0) { 457 warnx("fread"); 458 goto cleanup; 459 } 460 461 SHA256_Final(hash, &sha256); 462 sha256_hash(hash, out); 463 ret = 0; 464 465cleanup: 466 if (fp != NULL) 467 fclose(fp); 468 else if (my_fd != -1) 469 close(my_fd); 470 (void)lseek(fd, 0, SEEK_SET); 471 472 return (ret); 473} 474 475static EVP_PKEY * 476load_public_key_buf(const unsigned char *cert, int certlen) 477{ 478 EVP_PKEY *pkey; 479 BIO *bp; 480 char errbuf[1024]; 481 482 bp = BIO_new_mem_buf(__DECONST(void *, cert), certlen); 483 484 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL) 485 warnx("%s", ERR_error_string(ERR_get_error(), errbuf)); 486 487 BIO_free(bp); 488 489 return (pkey); 490} 491 492static bool 493rsa_verify_cert(int fd, const unsigned char *key, int keylen, 494 unsigned char *sig, int siglen) 495{ 496 EVP_MD_CTX *mdctx; 497 EVP_PKEY *pkey; 498 char sha256[(SHA256_DIGEST_LENGTH * 2) + 2]; 499 char errbuf[1024]; 500 bool ret; 501 502 pkey = NULL; 503 mdctx = NULL; 504 ret = false; 505 506 /* Compute SHA256 of the package. */ 507 if (lseek(fd, 0, 0) == -1) { 508 warn("lseek"); 509 goto cleanup; 510 } 511 if ((sha256_fd(fd, sha256)) == -1) { 512 warnx("Error creating SHA256 hash for package"); 513 goto cleanup; 514 } 515 516 if ((pkey = load_public_key_buf(key, keylen)) == NULL) { 517 warnx("Error reading public key"); 518 goto cleanup; 519 } 520 521 /* Verify signature of the SHA256(pkg) is valid. */ 522 if ((mdctx = EVP_MD_CTX_create()) == NULL) { 523 warnx("%s", ERR_error_string(ERR_get_error(), errbuf)); 524 goto error; 525 } 526 527 if (EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey) != 1) { 528 warnx("%s", ERR_error_string(ERR_get_error(), errbuf)); 529 goto error; 530 } 531 if (EVP_DigestVerifyUpdate(mdctx, sha256, strlen(sha256)) != 1) { 532 warnx("%s", ERR_error_string(ERR_get_error(), errbuf)); 533 goto error; 534 } 535 536 if (EVP_DigestVerifyFinal(mdctx, sig, siglen) != 1) { 537 warnx("%s", ERR_error_string(ERR_get_error(), errbuf)); 538 goto error; 539 } 540 541 ret = true; 542 printf("done\n"); 543 goto cleanup; 544 545error: 546 printf("failed\n"); 547 548cleanup: 549 if (pkey) 550 EVP_PKEY_free(pkey); 551 if (mdctx) 552 EVP_MD_CTX_destroy(mdctx); 553 ERR_free_strings(); 554 555 return (ret); 556} 557 558static struct sig_cert * 559parse_cert(int fd) { 560 int my_fd; 561 struct sig_cert *sc; 562 FILE *fp; 563 struct sbuf *buf, *sig, *cert; 564 char *line; 565 size_t linecap; 566 ssize_t linelen; 567 568 buf = NULL; 569 my_fd = -1; 570 sc = NULL; 571 line = NULL; 572 linecap = 0; 573 574 if (lseek(fd, 0, 0) == -1) { 575 warn("lseek"); 576 return (NULL); 577 } 578 579 /* Duplicate the fd so that fclose(3) does not close it. */ 580 if ((my_fd = dup(fd)) == -1) { 581 warnx("dup"); 582 return (NULL); 583 } 584 585 if ((fp = fdopen(my_fd, "rb")) == NULL) { 586 warn("fdopen"); 587 close(my_fd); 588 return (NULL); 589 } 590 591 sig = sbuf_new_auto(); 592 cert = sbuf_new_auto(); 593 594 while ((linelen = getline(&line, &linecap, fp)) > 0) { 595 if (strcmp(line, "SIGNATURE\n") == 0) { 596 buf = sig; 597 continue; 598 } else if (strcmp(line, "CERT\n") == 0) { 599 buf = cert; 600 continue; 601 } else if (strcmp(line, "END\n") == 0) { 602 break; 603 } 604 if (buf != NULL) 605 sbuf_bcat(buf, line, linelen); 606 } 607 608 fclose(fp); 609 610 /* Trim out unrelated trailing newline */ 611 sbuf_setpos(sig, sbuf_len(sig) - 1); 612 613 sbuf_finish(sig); 614 sbuf_finish(cert); 615 616 sc = calloc(1, sizeof(struct sig_cert)); 617 sc->siglen = sbuf_len(sig); 618 sc->sig = calloc(1, sc->siglen); 619 memcpy(sc->sig, sbuf_data(sig), sc->siglen); 620 621 sc->certlen = sbuf_len(cert); 622 sc->cert = strdup(sbuf_data(cert)); 623 624 sbuf_delete(sig); 625 sbuf_delete(cert); 626 627 return (sc); 628} 629 630static bool 631verify_signature(int fd_pkg, int fd_sig) 632{ 633 struct fingerprint_list *trusted, *revoked; 634 struct fingerprint *fingerprint; 635 struct sig_cert *sc; 636 bool ret; 637 int trusted_count, revoked_count; 638 const char *fingerprints; 639 char path[MAXPATHLEN]; 640 char hash[SHA256_DIGEST_LENGTH * 2 + 1]; 641 642 sc = NULL; 643 trusted = revoked = NULL; 644 ret = false; 645 646 /* Read and parse fingerprints. */ 647 if (config_string(FINGERPRINTS, &fingerprints) != 0) { 648 warnx("No CONFIG_FINGERPRINTS defined"); 649 goto cleanup; 650 } 651 652 snprintf(path, MAXPATHLEN, "%s/trusted", fingerprints); 653 if ((trusted = load_fingerprints(path, &trusted_count)) == NULL) { 654 warnx("Error loading trusted certificates"); 655 goto cleanup; 656 } 657 658 if (trusted_count == 0 || trusted == NULL) { 659 fprintf(stderr, "No trusted certificates found.\n"); 660 goto cleanup; 661 } 662 663 snprintf(path, MAXPATHLEN, "%s/revoked", fingerprints); 664 if ((revoked = load_fingerprints(path, &revoked_count)) == NULL) { 665 warnx("Error loading revoked certificates"); 666 goto cleanup; 667 } 668 669 /* Read certificate and signature in. */ 670 if ((sc = parse_cert(fd_sig)) == NULL) { 671 warnx("Error parsing certificate"); 672 goto cleanup; 673 } 674 /* Explicitly mark as non-trusted until proven otherwise. */ 675 sc->trusted = false; 676 677 /* Parse signature and pubkey out of the certificate */ 678 sha256_buf(sc->cert, sc->certlen, hash); 679 680 /* Check if this hash is revoked */ 681 if (revoked != NULL) { 682 STAILQ_FOREACH(fingerprint, revoked, next) { 683 if (strcasecmp(fingerprint->hash, hash) == 0) { 684 fprintf(stderr, "The package was signed with " 685 "revoked certificate %s\n", 686 fingerprint->name); 687 goto cleanup; 688 } 689 } 690 } 691 692 STAILQ_FOREACH(fingerprint, trusted, next) { 693 if (strcasecmp(fingerprint->hash, hash) == 0) { 694 sc->trusted = true; 695 sc->name = strdup(fingerprint->name); 696 break; 697 } 698 } 699 700 if (sc->trusted == false) { 701 fprintf(stderr, "No trusted fingerprint found matching " 702 "package's certificate\n"); 703 goto cleanup; 704 } 705 706 /* Verify the signature. */ 707 printf("Verifying signature with trusted certificate %s... ", sc->name); 708 if (rsa_verify_cert(fd_pkg, sc->cert, sc->certlen, sc->sig, 709 sc->siglen) == false) { 710 fprintf(stderr, "Signature is not valid\n"); 711 goto cleanup; 712 } 713 714 ret = true; 715 716cleanup: 717 if (trusted) 718 free_fingerprint_list(trusted); 719 if (revoked) 720 free_fingerprint_list(revoked); 721 if (sc) { 722 if (sc->cert) 723 free(sc->cert); 724 if (sc->sig) 725 free(sc->sig); 726 if (sc->name) 727 free(sc->name); 728 free(sc); 729 } 730 731 return (ret); 732} 733 734static int 735bootstrap_pkg(bool force) 736{ 737 int fd_pkg, fd_sig; 738 int ret; 739 char url[MAXPATHLEN]; 740 char tmppkg[MAXPATHLEN]; 741 char tmpsig[MAXPATHLEN]; 742 const char *packagesite; 743 const char *signature_type; 744 char pkgstatic[MAXPATHLEN]; 745 746 fd_sig = -1; 747 ret = -1; 748 749 if (config_string(PACKAGESITE, &packagesite) != 0) { 750 warnx("No PACKAGESITE defined"); 751 return (-1); 752 } 753 754 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) { 755 warnx("Error looking up SIGNATURE_TYPE"); 756 return (-1); 757 } 758 759 printf("Bootstrapping pkg from %s, please wait...\n", packagesite); 760 761 /* Support pkg+http:// for PACKAGESITE which is the new format 762 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has 763 no A record. */ 764 if (strncmp(URL_SCHEME_PREFIX, packagesite, 765 strlen(URL_SCHEME_PREFIX)) == 0) 766 packagesite += strlen(URL_SCHEME_PREFIX); 767 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite); 768 769 snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX", 770 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP); 771 772 if ((fd_pkg = fetch_to_fd(url, tmppkg)) == -1) 773 goto fetchfail; 774 775 if (signature_type != NULL && 776 strcasecmp(signature_type, "FINGERPRINTS") == 0) { 777 snprintf(tmpsig, MAXPATHLEN, "%s/pkg.txz.sig.XXXXXX", 778 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP); 779 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.sig", 780 packagesite); 781 782 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) { 783 fprintf(stderr, "Signature for pkg not available.\n"); 784 goto fetchfail; 785 } 786 787 if (verify_signature(fd_pkg, fd_sig) == false) 788 goto cleanup; 789 } 790 791 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0) 792 ret = install_pkg_static(pkgstatic, tmppkg, force); 793 794 goto cleanup; 795 796fetchfail: 797 warnx("Error fetching %s: %s", url, fetchLastErrString); 798 fprintf(stderr, "A pre-built version of pkg could not be found for " 799 "your system.\n"); 800 fprintf(stderr, "Consider changing PACKAGESITE or installing it from " 801 "ports: 'ports-mgmt/pkg'.\n"); 802 803cleanup: 804 if (fd_sig != -1) { 805 close(fd_sig); 806 unlink(tmpsig); 807 } 808 close(fd_pkg); 809 unlink(tmppkg); 810 811 return (ret); 812} 813 814static const char confirmation_message[] = 815"The package management tool is not yet installed on your system.\n" 816"Do you want to fetch and install it now? [y/N]: "; 817 818static int 819pkg_query_yes_no(void) 820{ 821 int ret, c; 822 823 c = getchar(); 824 825 if (c == 'y' || c == 'Y') 826 ret = 1; 827 else 828 ret = 0; 829 830 while (c != '\n' && c != EOF) 831 c = getchar(); 832 833 return (ret); 834} 835 836static int 837bootstrap_pkg_local(const char *pkgpath, bool force) 838{ 839 char path[MAXPATHLEN]; 840 char pkgstatic[MAXPATHLEN]; 841 const char *signature_type; 842 int fd_pkg, fd_sig, ret; 843 844 fd_sig = -1; 845 ret = -1; 846 847 fd_pkg = open(pkgpath, O_RDONLY); 848 if (fd_pkg == -1) 849 err(EXIT_FAILURE, "Unable to open %s", pkgpath); 850 851 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) { 852 warnx("Error looking up SIGNATURE_TYPE"); 853 return (-1); 854 } 855 if (signature_type != NULL && 856 strcasecmp(signature_type, "FINGERPRINTS") == 0) { 857 snprintf(path, sizeof(path), "%s.sig", pkgpath); 858 859 if ((fd_sig = open(path, O_RDONLY)) == -1) { 860 fprintf(stderr, "Signature for pkg not available.\n"); 861 goto cleanup; 862 } 863 864 if (verify_signature(fd_pkg, fd_sig) == false) 865 goto cleanup; 866 } 867 868 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0) 869 ret = install_pkg_static(pkgstatic, pkgpath, force); 870 871cleanup: 872 close(fd_pkg); 873 if (fd_sig != -1) 874 close(fd_sig); 875 876 return (ret); 877} 878 879int 880main(__unused int argc, char *argv[]) 881{ 882 char pkgpath[MAXPATHLEN]; 883 const char *pkgarg; 884 bool bootstrap_only, force, yes; 885 886 bootstrap_only = false; 887 force = false; 888 pkgarg = NULL; 889 yes = false; 890 891 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", 892 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE); 893 894 if (argc > 1 && strcmp(argv[1], "bootstrap") == 0) { 895 bootstrap_only = true; 896 if (argc == 3 && strcmp(argv[2], "-f") == 0) 897 force = true; 898 } 899 900 if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) { 901 /* 902 * To allow 'pkg -N' to be used as a reliable test for whether 903 * a system is configured to use pkg, don't bootstrap pkg 904 * when that argument is given as argv[1]. 905 */ 906 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0) 907 errx(EXIT_FAILURE, "pkg is not installed"); 908 909 config_init(); 910 911 if (argc > 1 && strcmp(argv[1], "add") == 0) { 912 if (argc > 2 && strcmp(argv[2], "-f") == 0) { 913 force = true; 914 pkgarg = argv[3]; 915 } else 916 pkgarg = argv[2]; 917 if (pkgarg == NULL) { 918 fprintf(stderr, "Path to pkg.txz required\n"); 919 exit(EXIT_FAILURE); 920 } 921 if (access(pkgarg, R_OK) == -1) { 922 fprintf(stderr, "No such file: %s\n", pkgarg); 923 exit(EXIT_FAILURE); 924 } 925 if (bootstrap_pkg_local(pkgarg, force) != 0) 926 exit(EXIT_FAILURE); 927 exit(EXIT_SUCCESS); 928 } 929 /* 930 * Do not ask for confirmation if either of stdin or stdout is 931 * not tty. Check the environment to see if user has answer 932 * tucked in there already. 933 */ 934 config_bool(ASSUME_ALWAYS_YES, &yes); 935 if (!yes) { 936 printf("%s", confirmation_message); 937 if (!isatty(fileno(stdin))) 938 exit(EXIT_FAILURE); 939 940 if (pkg_query_yes_no() == 0) 941 exit(EXIT_FAILURE); 942 } 943 if (bootstrap_pkg(force) != 0) 944 exit(EXIT_FAILURE); 945 config_finish(); 946 947 if (bootstrap_only) 948 exit(EXIT_SUCCESS); 949 } else if (bootstrap_only) { 950 printf("pkg already bootstrapped at %s\n", pkgpath); 951 exit(EXIT_SUCCESS); 952 } 953 954 execv(pkgpath, argv); 955 956 /* NOT REACHED */ 957 return (EXIT_FAILURE); 958} 959