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>
39 #include <archive_entry.h>
53 #include <openssl/err.h>
54 #include <openssl/ssl.h>
56 #include "dns_utils.h"
82 STAILQ_ENTRY(fingerprint) next;
85 STAILQ_HEAD(fingerprint_list, fingerprint);
88 extract_pkg_static(int fd, char *p, int sz)
91 struct archive_entry *ae;
96 a = archive_read_new();
98 warn("archive_read_new");
101 archive_read_support_filter_all(a);
102 archive_read_support_format_tar(a);
104 if (lseek(fd, 0, 0) == -1) {
109 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
110 warnx("archive_read_open_fd: %s", archive_error_string(a));
115 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) {
116 end = strrchr(archive_entry_pathname(ae), '/');
120 if (strcmp(end, "/pkg-static") == 0) {
121 r = archive_read_extract(a, ae,
122 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
123 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
124 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
125 strlcpy(p, archive_entry_pathname(ae), sz);
133 warnx("failed to extract pkg-static: %s",
134 archive_error_string(a));
137 archive_read_free(a);
143 install_pkg_static(const char *path, const char *pkgpath, bool force)
148 switch ((pid = fork())) {
153 execl(path, "pkg-static", "add", "-f", pkgpath,
156 execl(path, "pkg-static", "add", pkgpath,
163 while (waitpid(pid, &pstat, 0) == -1)
167 if (WEXITSTATUS(pstat))
168 return (WEXITSTATUS(pstat));
169 else if (WIFSIGNALED(pstat))
170 return (128 & (WTERMSIG(pstat)));
175 fetch_to_fd(const char *url, char *path)
178 struct dns_srvinfo *mirrors, *current;
181 /* To store _https._tcp. + hostname + \0 */
183 int retry, max_retry;
186 char zone[MAXHOSTNAMELEN + 13];
187 static const char *mirror_type = NULL;
190 current = mirrors = NULL;
193 if (mirror_type == NULL && config_string(MIRROR_TYPE, &mirror_type)
195 warnx("No MIRROR_TYPE defined");
199 if ((fd = mkstemp(path)) == -1) {
206 if ((u = fetchParseURL(url)) == NULL) {
207 warn("fetchParseURL('%s')", url);
211 while (remote == NULL) {
212 if (retry == max_retry) {
213 if (strcmp(u->scheme, "file") != 0 &&
214 strcasecmp(mirror_type, "srv") == 0) {
215 snprintf(zone, sizeof(zone),
216 "_%s._tcp.%s", u->scheme, u->host);
217 mirrors = dns_getsrvinfo(zone);
222 if (mirrors != NULL) {
223 strlcpy(u->host, current->host, sizeof(u->host));
224 u->port = current->port;
227 remote = fetchXGet(u, &st, "");
228 if (remote == NULL) {
232 if (mirrors == NULL) {
235 current = current->next;
242 while ((r = fread(buf, 1, sizeof(buf), remote)) > 0) {
243 if (write(fd, buf, r) != r) {
250 warn("An error occurred while fetching pkg(8)");
273 static struct fingerprint *
274 parse_fingerprint(ucl_object_t *obj)
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;
282 function = fp = NULL;
284 while ((cur = ucl_iterate_object(obj, &it, true))) {
285 key = ucl_object_key(cur);
286 if (cur->type != UCL_STRING)
288 if (strcasecmp(key, "function") == 0) {
289 function = ucl_object_tostring(cur);
292 if (strcasecmp(key, "fingerprint") == 0) {
293 fp = ucl_object_tostring(cur);
298 if (fp == NULL || function == NULL)
301 if (strcasecmp(function, "sha256") == 0)
304 if (fct == HASH_UNKNOWN) {
305 warnx("Unsupported hashing function: %s", function);
309 f = calloc(1, sizeof(struct fingerprint));
311 strlcpy(f->hash, fp, sizeof(f->hash));
317 free_fingerprint_list(struct fingerprint_list* list)
319 struct fingerprint *fingerprint, *tmp;
321 STAILQ_FOREACH_SAFE(fingerprint, list, next, tmp) {
322 free(fingerprint->name);
328 static struct fingerprint *
329 load_fingerprint(const char *dir, const char *filename)
331 ucl_object_t *obj = NULL;
332 struct ucl_parser *p = NULL;
333 struct fingerprint *f;
334 char path[MAXPATHLEN];
338 snprintf(path, MAXPATHLEN, "%s/%s", dir, filename);
340 p = ucl_parser_new(0);
341 if (!ucl_parser_add_file(p, path)) {
342 warnx("%s: %s", path, ucl_parser_get_error(p));
347 obj = ucl_parser_get_object(p);
349 if (obj->type == UCL_OBJECT)
350 f = parse_fingerprint(obj);
353 f->name = strdup(filename);
355 ucl_object_unref(obj);
361 static struct fingerprint_list *
362 load_fingerprints(const char *path, int *count)
366 struct fingerprint *finger;
367 struct fingerprint_list *fingerprints;
371 fingerprints = calloc(1, sizeof(struct fingerprint_list));
372 if (fingerprints == NULL)
374 STAILQ_INIT(fingerprints);
376 if ((d = opendir(path)) == NULL) {
382 while ((ent = readdir(d))) {
383 if (strcmp(ent->d_name, ".") == 0 ||
384 strcmp(ent->d_name, "..") == 0)
386 finger = load_fingerprint(path, ent->d_name);
387 if (finger != NULL) {
388 STAILQ_INSERT_TAIL(fingerprints, finger, next);
395 return (fingerprints);
399 sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH],
400 char out[SHA256_DIGEST_LENGTH * 2 + 1])
404 for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
405 sprintf(out + (i * 2), "%02x", hash[i]);
407 out[SHA256_DIGEST_LENGTH * 2] = '\0';
411 sha256_buf(char *buf, size_t len, char out[SHA256_DIGEST_LENGTH * 2 + 1])
413 unsigned char hash[SHA256_DIGEST_LENGTH];
418 SHA256_Init(&sha256);
419 SHA256_Update(&sha256, buf, len);
420 SHA256_Final(hash, &sha256);
421 sha256_hash(hash, out);
425 sha256_fd(int fd, char out[SHA256_DIGEST_LENGTH * 2 + 1])
430 unsigned char hash[SHA256_DIGEST_LENGTH];
442 /* Duplicate the fd so that fclose(3) does not close it. */
443 if ((my_fd = dup(fd)) == -1) {
448 if ((fp = fdopen(my_fd, "rb")) == NULL) {
453 SHA256_Init(&sha256);
455 while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0)
456 SHA256_Update(&sha256, buffer, r);
458 if (ferror(fp) != 0) {
463 SHA256_Final(hash, &sha256);
464 sha256_hash(hash, out);
470 else if (my_fd != -1)
472 (void)lseek(fd, 0, SEEK_SET);
478 load_public_key_file(const char *file)
484 bp = BIO_new_file(file, "r");
486 errx(EXIT_FAILURE, "Unable to read %s", file);
488 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
489 warnx("ici: %s", ERR_error_string(ERR_get_error(), errbuf));
497 load_public_key_buf(const unsigned char *cert, int certlen)
503 bp = BIO_new_mem_buf(__DECONST(void *, cert), certlen);
505 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
506 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
514 rsa_verify_cert(int fd, const char *sigfile, const unsigned char *key,
515 int keylen, unsigned char *sig, int siglen)
519 char sha256[(SHA256_DIGEST_LENGTH * 2) + 2];
527 SSL_load_error_strings();
529 /* Compute SHA256 of the package. */
530 if (lseek(fd, 0, 0) == -1) {
534 if ((sha256_fd(fd, sha256)) == -1) {
535 warnx("Error creating SHA256 hash for package");
539 if (sigfile != NULL) {
540 if ((pkey = load_public_key_file(sigfile)) == NULL) {
541 warnx("Error reading public key");
545 if ((pkey = load_public_key_buf(key, keylen)) == NULL) {
546 warnx("Error reading public key");
551 /* Verify signature of the SHA256(pkg) is valid. */
552 if ((mdctx = EVP_MD_CTX_create()) == NULL) {
553 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
557 if (EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey) != 1) {
558 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
561 if (EVP_DigestVerifyUpdate(mdctx, sha256, strlen(sha256)) != 1) {
562 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
566 if (EVP_DigestVerifyFinal(mdctx, sig, siglen) != 1) {
567 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
582 EVP_MD_CTX_destroy(mdctx);
588 static struct pubkey *
596 if (lseek(fd, 0, 0) == -1) {
601 sig = sbuf_new_auto();
603 while ((r = read(fd, buf, sizeof(buf))) >0) {
604 sbuf_bcat(sig, buf, r);
608 pk = calloc(1, sizeof(struct pubkey));
609 pk->siglen = sbuf_len(sig);
610 pk->sig = calloc(1, pk->siglen);
611 memcpy(pk->sig, sbuf_data(sig), pk->siglen);
617 static struct sig_cert *
622 struct sbuf *buf, *sig, *cert;
633 if (lseek(fd, 0, 0) == -1) {
638 /* Duplicate the fd so that fclose(3) does not close it. */
639 if ((my_fd = dup(fd)) == -1) {
644 if ((fp = fdopen(my_fd, "rb")) == NULL) {
650 sig = sbuf_new_auto();
651 cert = sbuf_new_auto();
653 while ((linelen = getline(&line, &linecap, fp)) > 0) {
654 if (strcmp(line, "SIGNATURE\n") == 0) {
657 } else if (strcmp(line, "CERT\n") == 0) {
660 } else if (strcmp(line, "END\n") == 0) {
664 sbuf_bcat(buf, line, linelen);
669 /* Trim out unrelated trailing newline */
670 sbuf_setpos(sig, sbuf_len(sig) - 1);
675 sc = calloc(1, sizeof(struct sig_cert));
676 sc->siglen = sbuf_len(sig);
677 sc->sig = calloc(1, sc->siglen);
678 memcpy(sc->sig, sbuf_data(sig), sc->siglen);
680 sc->certlen = sbuf_len(cert);
681 sc->cert = strdup(sbuf_data(cert));
690 verify_pubsignature(int fd_pkg, int fd_sig)
699 if (config_string(PUBKEY, &pubkey) != 0) {
700 warnx("No CONFIG_PUBKEY defined");
704 if ((pk = read_pubkey(fd_sig)) == NULL) {
705 warnx("Error reading signature");
709 /* Verify the signature. */
710 printf("Verifying signature with public key %s... ", pubkey);
711 if (rsa_verify_cert(fd_pkg, pubkey, NULL, 0, pk->sig,
712 pk->siglen) == false) {
713 fprintf(stderr, "Signature is not valid\n");
729 verify_signature(int fd_pkg, int fd_sig)
731 struct fingerprint_list *trusted, *revoked;
732 struct fingerprint *fingerprint;
735 int trusted_count, revoked_count;
736 const char *fingerprints;
737 char path[MAXPATHLEN];
738 char hash[SHA256_DIGEST_LENGTH * 2 + 1];
741 trusted = revoked = NULL;
744 /* Read and parse fingerprints. */
745 if (config_string(FINGERPRINTS, &fingerprints) != 0) {
746 warnx("No CONFIG_FINGERPRINTS defined");
750 snprintf(path, MAXPATHLEN, "%s/trusted", fingerprints);
751 if ((trusted = load_fingerprints(path, &trusted_count)) == NULL) {
752 warnx("Error loading trusted certificates");
756 if (trusted_count == 0 || trusted == NULL) {
757 fprintf(stderr, "No trusted certificates found.\n");
761 snprintf(path, MAXPATHLEN, "%s/revoked", fingerprints);
762 if ((revoked = load_fingerprints(path, &revoked_count)) == NULL) {
763 warnx("Error loading revoked certificates");
767 /* Read certificate and signature in. */
768 if ((sc = parse_cert(fd_sig)) == NULL) {
769 warnx("Error parsing certificate");
772 /* Explicitly mark as non-trusted until proven otherwise. */
775 /* Parse signature and pubkey out of the certificate */
776 sha256_buf(sc->cert, sc->certlen, hash);
778 /* Check if this hash is revoked */
779 if (revoked != NULL) {
780 STAILQ_FOREACH(fingerprint, revoked, next) {
781 if (strcasecmp(fingerprint->hash, hash) == 0) {
782 fprintf(stderr, "The package was signed with "
783 "revoked certificate %s\n",
790 STAILQ_FOREACH(fingerprint, trusted, next) {
791 if (strcasecmp(fingerprint->hash, hash) == 0) {
793 sc->name = strdup(fingerprint->name);
798 if (sc->trusted == false) {
799 fprintf(stderr, "No trusted fingerprint found matching "
800 "package's certificate\n");
804 /* Verify the signature. */
805 printf("Verifying signature with trusted certificate %s... ", sc->name);
806 if (rsa_verify_cert(fd_pkg, NULL, sc->cert, sc->certlen, sc->sig,
807 sc->siglen) == false) {
808 fprintf(stderr, "Signature is not valid\n");
816 free_fingerprint_list(trusted);
818 free_fingerprint_list(revoked);
830 bootstrap_pkg(bool force)
834 char url[MAXPATHLEN];
835 char tmppkg[MAXPATHLEN];
836 char tmpsig[MAXPATHLEN];
837 const char *packagesite;
838 const char *signature_type;
839 char pkgstatic[MAXPATHLEN];
844 if (config_string(PACKAGESITE, &packagesite) != 0) {
845 warnx("No PACKAGESITE defined");
849 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
850 warnx("Error looking up SIGNATURE_TYPE");
854 printf("Bootstrapping pkg from %s, please wait...\n", packagesite);
856 /* Support pkg+http:// for PACKAGESITE which is the new format
857 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has
859 if (strncmp(URL_SCHEME_PREFIX, packagesite,
860 strlen(URL_SCHEME_PREFIX)) == 0)
861 packagesite += strlen(URL_SCHEME_PREFIX);
862 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite);
864 snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX",
865 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
867 if ((fd_pkg = fetch_to_fd(url, tmppkg)) == -1)
870 if (signature_type != NULL &&
871 strcasecmp(signature_type, "NONE") != 0) {
872 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
874 snprintf(tmpsig, MAXPATHLEN, "%s/pkg.txz.sig.XXXXXX",
875 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
876 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.sig",
879 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
880 fprintf(stderr, "Signature for pkg not "
885 if (verify_signature(fd_pkg, fd_sig) == false)
887 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
889 snprintf(tmpsig, MAXPATHLEN,
890 "%s/pkg.txz.pubkeysig.XXXXXX",
891 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
892 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.pubkeysig",
895 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
896 fprintf(stderr, "Signature for pkg not "
901 if (verify_pubsignature(fd_pkg, fd_sig) == false)
904 warnx("Signature type %s is not supported for "
905 "bootstrapping.", signature_type);
910 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
911 ret = install_pkg_static(pkgstatic, tmppkg, force);
916 warnx("Error fetching %s: %s", url, fetchLastErrString);
917 fprintf(stderr, "A pre-built version of pkg could not be found for "
919 fprintf(stderr, "Consider changing PACKAGESITE or installing it from "
920 "ports: 'ports-mgmt/pkg'.\n");
936 static const char confirmation_message[] =
937 "The package management tool is not yet installed on your system.\n"
938 "Do you want to fetch and install it now? [y/N]: ";
940 static const char non_interactive_message[] =
941 "The package management tool is not yet installed on your system.\n"
942 "Please set ASSUME_ALWAYS_YES=yes environment variable to be able to bootstrap "
943 "in non-interactive (stdin not being a tty)\n";
946 pkg_query_yes_no(void)
952 if (c == 'y' || c == 'Y')
957 while (c != '\n' && c != EOF)
964 bootstrap_pkg_local(const char *pkgpath, bool force)
966 char path[MAXPATHLEN];
967 char pkgstatic[MAXPATHLEN];
968 const char *signature_type;
969 int fd_pkg, fd_sig, ret;
974 fd_pkg = open(pkgpath, O_RDONLY);
976 err(EXIT_FAILURE, "Unable to open %s", pkgpath);
978 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
979 warnx("Error looking up SIGNATURE_TYPE");
982 if (signature_type != NULL &&
983 strcasecmp(signature_type, "NONE") != 0) {
984 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
986 snprintf(path, sizeof(path), "%s.sig", pkgpath);
988 if ((fd_sig = open(path, O_RDONLY)) == -1) {
989 fprintf(stderr, "Signature for pkg not "
994 if (verify_signature(fd_pkg, fd_sig) == false)
997 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
999 snprintf(path, sizeof(path), "%s.pubkeysig", pkgpath);
1001 if ((fd_sig = open(path, O_RDONLY)) == -1) {
1002 fprintf(stderr, "Signature for pkg not "
1007 if (verify_pubsignature(fd_pkg, fd_sig) == false)
1011 warnx("Signature type %s is not supported for "
1012 "bootstrapping.", signature_type);
1017 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
1018 ret = install_pkg_static(pkgstatic, pkgpath, force);
1029 main(int argc, char *argv[])
1031 char pkgpath[MAXPATHLEN];
1033 bool bootstrap_only, force, yes;
1035 bootstrap_only = false;
1040 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
1041 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
1043 if (argc > 1 && strcmp(argv[1], "bootstrap") == 0) {
1044 bootstrap_only = true;
1045 if (argc == 3 && strcmp(argv[2], "-f") == 0)
1049 if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) {
1051 * To allow 'pkg -N' to be used as a reliable test for whether
1052 * a system is configured to use pkg, don't bootstrap pkg
1053 * when that argument is given as argv[1].
1055 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
1056 errx(EXIT_FAILURE, "pkg is not installed");
1060 if (argc > 1 && strcmp(argv[1], "add") == 0) {
1061 if (argc > 2 && strcmp(argv[2], "-f") == 0) {
1066 if (pkgarg == NULL) {
1067 fprintf(stderr, "Path to pkg.txz required\n");
1070 if (access(pkgarg, R_OK) == -1) {
1071 fprintf(stderr, "No such file: %s\n", pkgarg);
1074 if (bootstrap_pkg_local(pkgarg, force) != 0)
1079 * Do not ask for confirmation if either of stdin or stdout is
1080 * not tty. Check the environment to see if user has answer
1081 * tucked in there already.
1083 config_bool(ASSUME_ALWAYS_YES, &yes);
1085 if (!isatty(fileno(stdin))) {
1086 fprintf(stderr, non_interactive_message);
1090 printf("%s", confirmation_message);
1091 if (pkg_query_yes_no() == 0)
1094 if (bootstrap_pkg(force) != 0)
1100 } else if (bootstrap_only) {
1101 printf("pkg already bootstrapped at %s\n", pkgpath);
1105 execv(pkgpath, argv);
1108 return (EXIT_FAILURE);