2 * Copyright (c) 2012-2014 Baptiste Daroussin <bapt@FreeBSD.org>
3 * Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
38 #include <archive_entry.h>
52 #include <openssl/err.h>
53 #include <openssl/ssl.h>
55 #include "dns_utils.h"
81 STAILQ_ENTRY(fingerprint) next;
84 STAILQ_HEAD(fingerprint_list, fingerprint);
87 extract_pkg_static(int fd, char *p, int sz)
90 struct archive_entry *ae;
95 a = archive_read_new();
97 warn("archive_read_new");
100 archive_read_support_filter_all(a);
101 archive_read_support_format_tar(a);
103 if (lseek(fd, 0, 0) == -1) {
108 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
109 warnx("archive_read_open_fd: %s", archive_error_string(a));
114 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) {
115 end = strrchr(archive_entry_pathname(ae), '/');
119 if (strcmp(end, "/pkg-static") == 0) {
120 r = archive_read_extract(a, ae,
121 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
122 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
123 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
124 strlcpy(p, archive_entry_pathname(ae), sz);
132 warnx("failed to extract pkg-static: %s",
133 archive_error_string(a));
136 archive_read_free(a);
142 install_pkg_static(const char *path, const char *pkgpath, bool force)
147 switch ((pid = fork())) {
152 execl(path, "pkg-static", "add", "-f", pkgpath,
155 execl(path, "pkg-static", "add", pkgpath,
162 while (waitpid(pid, &pstat, 0) == -1)
166 if (WEXITSTATUS(pstat))
167 return (WEXITSTATUS(pstat));
168 else if (WIFSIGNALED(pstat))
169 return (128 & (WTERMSIG(pstat)));
174 fetch_to_fd(const char *url, char *path)
177 struct dns_srvinfo *mirrors, *current;
180 /* To store _https._tcp. + hostname + \0 */
182 int retry, max_retry;
185 char zone[MAXHOSTNAMELEN + 13];
186 static const char *mirror_type = NULL;
189 current = mirrors = NULL;
192 if (mirror_type == NULL && config_string(MIRROR_TYPE, &mirror_type)
194 warnx("No MIRROR_TYPE defined");
198 if ((fd = mkstemp(path)) == -1) {
205 if ((u = fetchParseURL(url)) == NULL) {
206 warn("fetchParseURL('%s')", url);
210 while (remote == NULL) {
211 if (retry == max_retry) {
212 if (strcmp(u->scheme, "file") != 0 &&
213 strcasecmp(mirror_type, "srv") == 0) {
214 snprintf(zone, sizeof(zone),
215 "_%s._tcp.%s", u->scheme, u->host);
216 mirrors = dns_getsrvinfo(zone);
221 if (mirrors != NULL) {
222 strlcpy(u->host, current->host, sizeof(u->host));
223 u->port = current->port;
226 remote = fetchXGet(u, &st, "");
227 if (remote == NULL) {
231 if (mirrors == NULL) {
234 current = current->next;
241 while ((r = fread(buf, 1, sizeof(buf), remote)) > 0) {
242 if (write(fd, buf, r) != r) {
249 warn("An error occurred while fetching pkg(8)");
272 static struct fingerprint *
273 parse_fingerprint(ucl_object_t *obj)
275 const ucl_object_t *cur;
276 ucl_object_iter_t it = NULL;
277 const char *function, *fp, *key;
278 struct fingerprint *f;
279 hash_t fct = HASH_UNKNOWN;
281 function = fp = NULL;
283 while ((cur = ucl_iterate_object(obj, &it, true))) {
284 key = ucl_object_key(cur);
285 if (cur->type != UCL_STRING)
287 if (strcasecmp(key, "function") == 0) {
288 function = ucl_object_tostring(cur);
291 if (strcasecmp(key, "fingerprint") == 0) {
292 fp = ucl_object_tostring(cur);
297 if (fp == NULL || function == NULL)
300 if (strcasecmp(function, "sha256") == 0)
303 if (fct == HASH_UNKNOWN) {
304 warnx("Unsupported hashing function: %s", function);
308 f = calloc(1, sizeof(struct fingerprint));
310 strlcpy(f->hash, fp, sizeof(f->hash));
316 free_fingerprint_list(struct fingerprint_list* list)
318 struct fingerprint *fingerprint, *tmp;
320 STAILQ_FOREACH_SAFE(fingerprint, list, next, tmp) {
321 free(fingerprint->name);
327 static struct fingerprint *
328 load_fingerprint(const char *dir, const char *filename)
330 ucl_object_t *obj = NULL;
331 struct ucl_parser *p = NULL;
332 struct fingerprint *f;
333 char path[MAXPATHLEN];
337 snprintf(path, MAXPATHLEN, "%s/%s", dir, filename);
339 p = ucl_parser_new(0);
340 if (!ucl_parser_add_file(p, path)) {
341 warnx("%s: %s", path, ucl_parser_get_error(p));
346 obj = ucl_parser_get_object(p);
348 if (obj->type == UCL_OBJECT)
349 f = parse_fingerprint(obj);
352 f->name = strdup(filename);
354 ucl_object_unref(obj);
360 static struct fingerprint_list *
361 load_fingerprints(const char *path, int *count)
365 struct fingerprint *finger;
366 struct fingerprint_list *fingerprints;
370 fingerprints = calloc(1, sizeof(struct fingerprint_list));
371 if (fingerprints == NULL)
373 STAILQ_INIT(fingerprints);
375 if ((d = opendir(path)) == NULL) {
381 while ((ent = readdir(d))) {
382 if (strcmp(ent->d_name, ".") == 0 ||
383 strcmp(ent->d_name, "..") == 0)
385 finger = load_fingerprint(path, ent->d_name);
386 if (finger != NULL) {
387 STAILQ_INSERT_TAIL(fingerprints, finger, next);
394 return (fingerprints);
398 sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH],
399 char out[SHA256_DIGEST_LENGTH * 2 + 1])
403 for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
404 sprintf(out + (i * 2), "%02x", hash[i]);
406 out[SHA256_DIGEST_LENGTH * 2] = '\0';
410 sha256_buf(char *buf, size_t len, char out[SHA256_DIGEST_LENGTH * 2 + 1])
412 unsigned char hash[SHA256_DIGEST_LENGTH];
417 SHA256_Init(&sha256);
418 SHA256_Update(&sha256, buf, len);
419 SHA256_Final(hash, &sha256);
420 sha256_hash(hash, out);
424 sha256_fd(int fd, char out[SHA256_DIGEST_LENGTH * 2 + 1])
429 unsigned char hash[SHA256_DIGEST_LENGTH];
441 /* Duplicate the fd so that fclose(3) does not close it. */
442 if ((my_fd = dup(fd)) == -1) {
447 if ((fp = fdopen(my_fd, "rb")) == NULL) {
452 SHA256_Init(&sha256);
454 while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0)
455 SHA256_Update(&sha256, buffer, r);
457 if (ferror(fp) != 0) {
462 SHA256_Final(hash, &sha256);
463 sha256_hash(hash, out);
469 else if (my_fd != -1)
471 (void)lseek(fd, 0, SEEK_SET);
477 load_public_key_file(const char *file)
483 bp = BIO_new_file(file, "r");
485 errx(EXIT_FAILURE, "Unable to read %s", file);
487 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
488 warnx("ici: %s", ERR_error_string(ERR_get_error(), errbuf));
496 load_public_key_buf(const unsigned char *cert, int certlen)
502 bp = BIO_new_mem_buf(__DECONST(void *, cert), certlen);
504 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
505 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
513 rsa_verify_cert(int fd, const char *sigfile, const unsigned char *key,
514 int keylen, unsigned char *sig, int siglen)
518 char sha256[(SHA256_DIGEST_LENGTH * 2) + 2];
526 SSL_load_error_strings();
528 /* Compute SHA256 of the package. */
529 if (lseek(fd, 0, 0) == -1) {
533 if ((sha256_fd(fd, sha256)) == -1) {
534 warnx("Error creating SHA256 hash for package");
538 if (sigfile != NULL) {
539 if ((pkey = load_public_key_file(sigfile)) == NULL) {
540 warnx("Error reading public key");
544 if ((pkey = load_public_key_buf(key, keylen)) == NULL) {
545 warnx("Error reading public key");
550 /* Verify signature of the SHA256(pkg) is valid. */
551 if ((mdctx = EVP_MD_CTX_create()) == NULL) {
552 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
556 if (EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey) != 1) {
557 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
560 if (EVP_DigestVerifyUpdate(mdctx, sha256, strlen(sha256)) != 1) {
561 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
565 if (EVP_DigestVerifyFinal(mdctx, sig, siglen) != 1) {
566 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
581 EVP_MD_CTX_destroy(mdctx);
587 static struct pubkey *
595 if (lseek(fd, 0, 0) == -1) {
600 sig = sbuf_new_auto();
602 while ((r = read(fd, buf, sizeof(buf))) >0) {
603 sbuf_bcat(sig, buf, r);
607 pk = calloc(1, sizeof(struct pubkey));
608 pk->siglen = sbuf_len(sig);
609 pk->sig = calloc(1, pk->siglen);
610 memcpy(pk->sig, sbuf_data(sig), pk->siglen);
616 static struct sig_cert *
621 struct sbuf *buf, *sig, *cert;
632 if (lseek(fd, 0, 0) == -1) {
637 /* Duplicate the fd so that fclose(3) does not close it. */
638 if ((my_fd = dup(fd)) == -1) {
643 if ((fp = fdopen(my_fd, "rb")) == NULL) {
649 sig = sbuf_new_auto();
650 cert = sbuf_new_auto();
652 while ((linelen = getline(&line, &linecap, fp)) > 0) {
653 if (strcmp(line, "SIGNATURE\n") == 0) {
656 } else if (strcmp(line, "CERT\n") == 0) {
659 } else if (strcmp(line, "END\n") == 0) {
663 sbuf_bcat(buf, line, linelen);
668 /* Trim out unrelated trailing newline */
669 sbuf_setpos(sig, sbuf_len(sig) - 1);
674 sc = calloc(1, sizeof(struct sig_cert));
675 sc->siglen = sbuf_len(sig);
676 sc->sig = calloc(1, sc->siglen);
677 memcpy(sc->sig, sbuf_data(sig), sc->siglen);
679 sc->certlen = sbuf_len(cert);
680 sc->cert = strdup(sbuf_data(cert));
689 verify_pubsignature(int fd_pkg, int fd_sig)
698 if (config_string(PUBKEY, &pubkey) != 0) {
699 warnx("No CONFIG_PUBKEY defined");
703 if ((pk = read_pubkey(fd_sig)) == NULL) {
704 warnx("Error reading signature");
708 /* Verify the signature. */
709 printf("Verifying signature with public key %s... ", pubkey);
710 if (rsa_verify_cert(fd_pkg, pubkey, NULL, 0, pk->sig,
711 pk->siglen) == false) {
712 fprintf(stderr, "Signature is not valid\n");
728 verify_signature(int fd_pkg, int fd_sig)
730 struct fingerprint_list *trusted, *revoked;
731 struct fingerprint *fingerprint;
734 int trusted_count, revoked_count;
735 const char *fingerprints;
736 char path[MAXPATHLEN];
737 char hash[SHA256_DIGEST_LENGTH * 2 + 1];
740 trusted = revoked = NULL;
743 /* Read and parse fingerprints. */
744 if (config_string(FINGERPRINTS, &fingerprints) != 0) {
745 warnx("No CONFIG_FINGERPRINTS defined");
749 snprintf(path, MAXPATHLEN, "%s/trusted", fingerprints);
750 if ((trusted = load_fingerprints(path, &trusted_count)) == NULL) {
751 warnx("Error loading trusted certificates");
755 if (trusted_count == 0 || trusted == NULL) {
756 fprintf(stderr, "No trusted certificates found.\n");
760 snprintf(path, MAXPATHLEN, "%s/revoked", fingerprints);
761 if ((revoked = load_fingerprints(path, &revoked_count)) == NULL) {
762 warnx("Error loading revoked certificates");
766 /* Read certificate and signature in. */
767 if ((sc = parse_cert(fd_sig)) == NULL) {
768 warnx("Error parsing certificate");
771 /* Explicitly mark as non-trusted until proven otherwise. */
774 /* Parse signature and pubkey out of the certificate */
775 sha256_buf(sc->cert, sc->certlen, hash);
777 /* Check if this hash is revoked */
778 if (revoked != NULL) {
779 STAILQ_FOREACH(fingerprint, revoked, next) {
780 if (strcasecmp(fingerprint->hash, hash) == 0) {
781 fprintf(stderr, "The package was signed with "
782 "revoked certificate %s\n",
789 STAILQ_FOREACH(fingerprint, trusted, next) {
790 if (strcasecmp(fingerprint->hash, hash) == 0) {
792 sc->name = strdup(fingerprint->name);
797 if (sc->trusted == false) {
798 fprintf(stderr, "No trusted fingerprint found matching "
799 "package's certificate\n");
803 /* Verify the signature. */
804 printf("Verifying signature with trusted certificate %s... ", sc->name);
805 if (rsa_verify_cert(fd_pkg, NULL, sc->cert, sc->certlen, sc->sig,
806 sc->siglen) == false) {
807 fprintf(stderr, "Signature is not valid\n");
815 free_fingerprint_list(trusted);
817 free_fingerprint_list(revoked);
829 bootstrap_pkg(bool force)
833 char url[MAXPATHLEN];
834 char tmppkg[MAXPATHLEN];
835 char tmpsig[MAXPATHLEN];
836 const char *packagesite;
837 const char *signature_type;
838 char pkgstatic[MAXPATHLEN];
843 if (config_string(PACKAGESITE, &packagesite) != 0) {
844 warnx("No PACKAGESITE defined");
848 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
849 warnx("Error looking up SIGNATURE_TYPE");
853 printf("Bootstrapping pkg from %s, please wait...\n", packagesite);
855 /* Support pkg+http:// for PACKAGESITE which is the new format
856 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has
858 if (strncmp(URL_SCHEME_PREFIX, packagesite,
859 strlen(URL_SCHEME_PREFIX)) == 0)
860 packagesite += strlen(URL_SCHEME_PREFIX);
861 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite);
863 snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX",
864 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
866 if ((fd_pkg = fetch_to_fd(url, tmppkg)) == -1)
869 if (signature_type != NULL &&
870 strcasecmp(signature_type, "NONE") != 0) {
871 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
873 snprintf(tmpsig, MAXPATHLEN, "%s/pkg.txz.sig.XXXXXX",
874 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
875 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.sig",
878 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
879 fprintf(stderr, "Signature for pkg not "
884 if (verify_signature(fd_pkg, fd_sig) == false)
886 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
888 snprintf(tmpsig, MAXPATHLEN,
889 "%s/pkg.txz.pubkeysig.XXXXXX",
890 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
891 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.pubkeysig",
894 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
895 fprintf(stderr, "Signature for pkg not "
900 if (verify_pubsignature(fd_pkg, fd_sig) == false)
903 warnx("Signature type %s is not supported for "
904 "bootstrapping.", signature_type);
909 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
910 ret = install_pkg_static(pkgstatic, tmppkg, force);
915 warnx("Error fetching %s: %s", url, fetchLastErrString);
916 fprintf(stderr, "A pre-built version of pkg could not be found for "
918 fprintf(stderr, "Consider changing PACKAGESITE or installing it from "
919 "ports: 'ports-mgmt/pkg'.\n");
935 static const char confirmation_message[] =
936 "The package management tool is not yet installed on your system.\n"
937 "Do you want to fetch and install it now? [y/N]: ";
939 static const char non_interactive_message[] =
940 "The package management tool is not yet installed on your system.\n"
941 "Please set ASSUME_ALWAYS_YES=yes environment variable to be able to bootstrap "
942 "in non-interactive (stdin not being a tty)\n";
945 pkg_query_yes_no(void)
951 if (c == 'y' || c == 'Y')
956 while (c != '\n' && c != EOF)
963 bootstrap_pkg_local(const char *pkgpath, bool force)
965 char path[MAXPATHLEN];
966 char pkgstatic[MAXPATHLEN];
967 const char *signature_type;
968 int fd_pkg, fd_sig, ret;
973 fd_pkg = open(pkgpath, O_RDONLY);
975 err(EXIT_FAILURE, "Unable to open %s", pkgpath);
977 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
978 warnx("Error looking up SIGNATURE_TYPE");
981 if (signature_type != NULL &&
982 strcasecmp(signature_type, "NONE") != 0) {
983 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
985 snprintf(path, sizeof(path), "%s.sig", pkgpath);
987 if ((fd_sig = open(path, O_RDONLY)) == -1) {
988 fprintf(stderr, "Signature for pkg not "
993 if (verify_signature(fd_pkg, fd_sig) == false)
996 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
998 snprintf(path, sizeof(path), "%s.pubkeysig", pkgpath);
1000 if ((fd_sig = open(path, O_RDONLY)) == -1) {
1001 fprintf(stderr, "Signature for pkg not "
1006 if (verify_pubsignature(fd_pkg, fd_sig) == false)
1010 warnx("Signature type %s is not supported for "
1011 "bootstrapping.", signature_type);
1016 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
1017 ret = install_pkg_static(pkgstatic, pkgpath, force);
1028 main(int argc, char *argv[])
1030 char pkgpath[MAXPATHLEN];
1032 bool bootstrap_only, force, yes;
1034 bootstrap_only = false;
1039 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
1040 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
1042 if (argc > 1 && strcmp(argv[1], "bootstrap") == 0) {
1043 bootstrap_only = true;
1044 if (argc == 3 && strcmp(argv[2], "-f") == 0)
1048 if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) {
1050 * To allow 'pkg -N' to be used as a reliable test for whether
1051 * a system is configured to use pkg, don't bootstrap pkg
1052 * when that argument is given as argv[1].
1054 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
1055 errx(EXIT_FAILURE, "pkg is not installed");
1059 if (argc > 1 && strcmp(argv[1], "add") == 0) {
1060 if (argc > 2 && strcmp(argv[2], "-f") == 0) {
1065 if (pkgarg == NULL) {
1066 fprintf(stderr, "Path to pkg.txz required\n");
1069 if (access(pkgarg, R_OK) == -1) {
1070 fprintf(stderr, "No such file: %s\n", pkgarg);
1073 if (bootstrap_pkg_local(pkgarg, force) != 0)
1078 * Do not ask for confirmation if either of stdin or stdout is
1079 * not tty. Check the environment to see if user has answer
1080 * tucked in there already.
1082 config_bool(ASSUME_ALWAYS_YES, &yes);
1084 if (!isatty(fileno(stdin))) {
1085 fprintf(stderr, non_interactive_message);
1089 printf("%s", confirmation_message);
1090 if (pkg_query_yes_no() == 0)
1093 if (bootstrap_pkg(force) != 0)
1099 } else if (bootstrap_only) {
1100 printf("pkg already bootstrapped at %s\n", pkgpath);
1104 execv(pkgpath, argv);
1107 return (EXIT_FAILURE);