2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012-2014 Baptiste Daroussin <bapt@FreeBSD.org>
5 * Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
41 #include <archive_entry.h>
55 #include <openssl/err.h>
56 #include <openssl/ssl.h>
58 #include "dns_utils.h"
84 STAILQ_ENTRY(fingerprint) next;
87 STAILQ_HEAD(fingerprint_list, fingerprint);
90 extract_pkg_static(int fd, char *p, int sz)
93 struct archive_entry *ae;
98 a = archive_read_new();
100 warn("archive_read_new");
103 archive_read_support_filter_all(a);
104 archive_read_support_format_tar(a);
106 if (lseek(fd, 0, 0) == -1) {
111 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
112 warnx("archive_read_open_fd: %s", archive_error_string(a));
117 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) {
118 end = strrchr(archive_entry_pathname(ae), '/');
122 if (strcmp(end, "/pkg-static") == 0) {
123 r = archive_read_extract(a, ae,
124 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
125 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
126 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
127 strlcpy(p, archive_entry_pathname(ae), sz);
135 warnx("failed to extract pkg-static: %s",
136 archive_error_string(a));
139 archive_read_free(a);
145 install_pkg_static(const char *path, const char *pkgpath, bool force)
150 switch ((pid = fork())) {
155 execl(path, "pkg-static", "add", "-f", pkgpath,
158 execl(path, "pkg-static", "add", pkgpath,
165 while (waitpid(pid, &pstat, 0) == -1)
169 if (WEXITSTATUS(pstat))
170 return (WEXITSTATUS(pstat));
171 else if (WIFSIGNALED(pstat))
172 return (128 & (WTERMSIG(pstat)));
177 fetch_to_fd(const char *url, char *path)
180 struct dns_srvinfo *mirrors, *current;
183 /* To store _https._tcp. + hostname + \0 */
185 int retry, max_retry;
188 char zone[MAXHOSTNAMELEN + 13];
189 static const char *mirror_type = NULL;
192 current = mirrors = NULL;
195 if (mirror_type == NULL && config_string(MIRROR_TYPE, &mirror_type)
197 warnx("No MIRROR_TYPE defined");
201 if ((fd = mkstemp(path)) == -1) {
208 if ((u = fetchParseURL(url)) == NULL) {
209 warn("fetchParseURL('%s')", url);
213 while (remote == NULL) {
214 if (retry == max_retry) {
215 if (strcmp(u->scheme, "file") != 0 &&
216 strcasecmp(mirror_type, "srv") == 0) {
217 snprintf(zone, sizeof(zone),
218 "_%s._tcp.%s", u->scheme, u->host);
219 mirrors = dns_getsrvinfo(zone);
224 if (mirrors != NULL) {
225 strlcpy(u->host, current->host, sizeof(u->host));
226 u->port = current->port;
229 remote = fetchXGet(u, &st, "");
230 if (remote == NULL) {
234 if (mirrors == NULL) {
237 current = current->next;
244 while ((r = fread(buf, 1, sizeof(buf), remote)) > 0) {
245 if (write(fd, buf, r) != r) {
252 warn("An error occurred while fetching pkg(8)");
275 static struct fingerprint *
276 parse_fingerprint(ucl_object_t *obj)
278 const ucl_object_t *cur;
279 ucl_object_iter_t it = NULL;
280 const char *function, *fp, *key;
281 struct fingerprint *f;
282 hash_t fct = HASH_UNKNOWN;
284 function = fp = NULL;
286 while ((cur = ucl_iterate_object(obj, &it, true))) {
287 key = ucl_object_key(cur);
288 if (cur->type != UCL_STRING)
290 if (strcasecmp(key, "function") == 0) {
291 function = ucl_object_tostring(cur);
294 if (strcasecmp(key, "fingerprint") == 0) {
295 fp = ucl_object_tostring(cur);
300 if (fp == NULL || function == NULL)
303 if (strcasecmp(function, "sha256") == 0)
306 if (fct == HASH_UNKNOWN) {
307 warnx("Unsupported hashing function: %s", function);
311 f = calloc(1, sizeof(struct fingerprint));
313 strlcpy(f->hash, fp, sizeof(f->hash));
319 free_fingerprint_list(struct fingerprint_list* list)
321 struct fingerprint *fingerprint, *tmp;
323 STAILQ_FOREACH_SAFE(fingerprint, list, next, tmp) {
324 free(fingerprint->name);
330 static struct fingerprint *
331 load_fingerprint(const char *dir, const char *filename)
333 ucl_object_t *obj = NULL;
334 struct ucl_parser *p = NULL;
335 struct fingerprint *f;
336 char path[MAXPATHLEN];
340 snprintf(path, MAXPATHLEN, "%s/%s", dir, filename);
342 p = ucl_parser_new(0);
343 if (!ucl_parser_add_file(p, path)) {
344 warnx("%s: %s", path, ucl_parser_get_error(p));
349 obj = ucl_parser_get_object(p);
351 if (obj->type == UCL_OBJECT)
352 f = parse_fingerprint(obj);
355 f->name = strdup(filename);
357 ucl_object_unref(obj);
363 static struct fingerprint_list *
364 load_fingerprints(const char *path, int *count)
368 struct fingerprint *finger;
369 struct fingerprint_list *fingerprints;
373 fingerprints = calloc(1, sizeof(struct fingerprint_list));
374 if (fingerprints == NULL)
376 STAILQ_INIT(fingerprints);
378 if ((d = opendir(path)) == NULL) {
384 while ((ent = readdir(d))) {
385 if (strcmp(ent->d_name, ".") == 0 ||
386 strcmp(ent->d_name, "..") == 0)
388 finger = load_fingerprint(path, ent->d_name);
389 if (finger != NULL) {
390 STAILQ_INSERT_TAIL(fingerprints, finger, next);
397 return (fingerprints);
401 sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH],
402 char out[SHA256_DIGEST_LENGTH * 2 + 1])
406 for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
407 sprintf(out + (i * 2), "%02x", hash[i]);
409 out[SHA256_DIGEST_LENGTH * 2] = '\0';
413 sha256_buf(char *buf, size_t len, char out[SHA256_DIGEST_LENGTH * 2 + 1])
415 unsigned char hash[SHA256_DIGEST_LENGTH];
420 SHA256_Init(&sha256);
421 SHA256_Update(&sha256, buf, len);
422 SHA256_Final(hash, &sha256);
423 sha256_hash(hash, out);
427 sha256_fd(int fd, char out[SHA256_DIGEST_LENGTH * 2 + 1])
432 unsigned char hash[SHA256_DIGEST_LENGTH];
444 /* Duplicate the fd so that fclose(3) does not close it. */
445 if ((my_fd = dup(fd)) == -1) {
450 if ((fp = fdopen(my_fd, "rb")) == NULL) {
455 SHA256_Init(&sha256);
457 while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0)
458 SHA256_Update(&sha256, buffer, r);
460 if (ferror(fp) != 0) {
465 SHA256_Final(hash, &sha256);
466 sha256_hash(hash, out);
472 else if (my_fd != -1)
474 (void)lseek(fd, 0, SEEK_SET);
480 load_public_key_file(const char *file)
486 bp = BIO_new_file(file, "r");
488 errx(EXIT_FAILURE, "Unable to read %s", file);
490 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
491 warnx("ici: %s", ERR_error_string(ERR_get_error(), errbuf));
499 load_public_key_buf(const unsigned char *cert, int certlen)
505 bp = BIO_new_mem_buf(__DECONST(void *, cert), certlen);
507 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
508 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
516 rsa_verify_cert(int fd, const char *sigfile, const unsigned char *key,
517 int keylen, unsigned char *sig, int siglen)
521 char sha256[(SHA256_DIGEST_LENGTH * 2) + 2];
529 SSL_load_error_strings();
531 /* Compute SHA256 of the package. */
532 if (lseek(fd, 0, 0) == -1) {
536 if ((sha256_fd(fd, sha256)) == -1) {
537 warnx("Error creating SHA256 hash for package");
541 if (sigfile != NULL) {
542 if ((pkey = load_public_key_file(sigfile)) == NULL) {
543 warnx("Error reading public key");
547 if ((pkey = load_public_key_buf(key, keylen)) == NULL) {
548 warnx("Error reading public key");
553 /* Verify signature of the SHA256(pkg) is valid. */
554 if ((mdctx = EVP_MD_CTX_create()) == NULL) {
555 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
559 if (EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey) != 1) {
560 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
563 if (EVP_DigestVerifyUpdate(mdctx, sha256, strlen(sha256)) != 1) {
564 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
568 if (EVP_DigestVerifyFinal(mdctx, sig, siglen) != 1) {
569 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
584 EVP_MD_CTX_destroy(mdctx);
590 static struct pubkey *
598 if (lseek(fd, 0, 0) == -1) {
603 sig = sbuf_new_auto();
605 while ((r = read(fd, buf, sizeof(buf))) >0) {
606 sbuf_bcat(sig, buf, r);
610 pk = calloc(1, sizeof(struct pubkey));
611 pk->siglen = sbuf_len(sig);
612 pk->sig = calloc(1, pk->siglen);
613 memcpy(pk->sig, sbuf_data(sig), pk->siglen);
619 static struct sig_cert *
624 struct sbuf *buf, *sig, *cert;
635 if (lseek(fd, 0, 0) == -1) {
640 /* Duplicate the fd so that fclose(3) does not close it. */
641 if ((my_fd = dup(fd)) == -1) {
646 if ((fp = fdopen(my_fd, "rb")) == NULL) {
652 sig = sbuf_new_auto();
653 cert = sbuf_new_auto();
655 while ((linelen = getline(&line, &linecap, fp)) > 0) {
656 if (strcmp(line, "SIGNATURE\n") == 0) {
659 } else if (strcmp(line, "CERT\n") == 0) {
662 } else if (strcmp(line, "END\n") == 0) {
666 sbuf_bcat(buf, line, linelen);
671 /* Trim out unrelated trailing newline */
672 sbuf_setpos(sig, sbuf_len(sig) - 1);
677 sc = calloc(1, sizeof(struct sig_cert));
678 sc->siglen = sbuf_len(sig);
679 sc->sig = calloc(1, sc->siglen);
680 memcpy(sc->sig, sbuf_data(sig), sc->siglen);
682 sc->certlen = sbuf_len(cert);
683 sc->cert = strdup(sbuf_data(cert));
692 verify_pubsignature(int fd_pkg, int fd_sig)
701 if (config_string(PUBKEY, &pubkey) != 0) {
702 warnx("No CONFIG_PUBKEY defined");
706 if ((pk = read_pubkey(fd_sig)) == NULL) {
707 warnx("Error reading signature");
711 /* Verify the signature. */
712 printf("Verifying signature with public key %s... ", pubkey);
713 if (rsa_verify_cert(fd_pkg, pubkey, NULL, 0, pk->sig,
714 pk->siglen) == false) {
715 fprintf(stderr, "Signature is not valid\n");
731 verify_signature(int fd_pkg, int fd_sig)
733 struct fingerprint_list *trusted, *revoked;
734 struct fingerprint *fingerprint;
737 int trusted_count, revoked_count;
738 const char *fingerprints;
739 char path[MAXPATHLEN];
740 char hash[SHA256_DIGEST_LENGTH * 2 + 1];
743 trusted = revoked = NULL;
746 /* Read and parse fingerprints. */
747 if (config_string(FINGERPRINTS, &fingerprints) != 0) {
748 warnx("No CONFIG_FINGERPRINTS defined");
752 snprintf(path, MAXPATHLEN, "%s/trusted", fingerprints);
753 if ((trusted = load_fingerprints(path, &trusted_count)) == NULL) {
754 warnx("Error loading trusted certificates");
758 if (trusted_count == 0 || trusted == NULL) {
759 fprintf(stderr, "No trusted certificates found.\n");
763 snprintf(path, MAXPATHLEN, "%s/revoked", fingerprints);
764 if ((revoked = load_fingerprints(path, &revoked_count)) == NULL) {
765 warnx("Error loading revoked certificates");
769 /* Read certificate and signature in. */
770 if ((sc = parse_cert(fd_sig)) == NULL) {
771 warnx("Error parsing certificate");
774 /* Explicitly mark as non-trusted until proven otherwise. */
777 /* Parse signature and pubkey out of the certificate */
778 sha256_buf(sc->cert, sc->certlen, hash);
780 /* Check if this hash is revoked */
781 if (revoked != NULL) {
782 STAILQ_FOREACH(fingerprint, revoked, next) {
783 if (strcasecmp(fingerprint->hash, hash) == 0) {
784 fprintf(stderr, "The package was signed with "
785 "revoked certificate %s\n",
792 STAILQ_FOREACH(fingerprint, trusted, next) {
793 if (strcasecmp(fingerprint->hash, hash) == 0) {
795 sc->name = strdup(fingerprint->name);
800 if (sc->trusted == false) {
801 fprintf(stderr, "No trusted fingerprint found matching "
802 "package's certificate\n");
806 /* Verify the signature. */
807 printf("Verifying signature with trusted certificate %s... ", sc->name);
808 if (rsa_verify_cert(fd_pkg, NULL, sc->cert, sc->certlen, sc->sig,
809 sc->siglen) == false) {
810 fprintf(stderr, "Signature is not valid\n");
818 free_fingerprint_list(trusted);
820 free_fingerprint_list(revoked);
832 bootstrap_pkg(bool force)
836 char url[MAXPATHLEN];
837 char tmppkg[MAXPATHLEN];
838 char tmpsig[MAXPATHLEN];
839 const char *packagesite;
840 const char *signature_type;
841 char pkgstatic[MAXPATHLEN];
846 if (config_string(PACKAGESITE, &packagesite) != 0) {
847 warnx("No PACKAGESITE defined");
851 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
852 warnx("Error looking up SIGNATURE_TYPE");
856 printf("Bootstrapping pkg from %s, please wait...\n", packagesite);
858 /* Support pkg+http:// for PACKAGESITE which is the new format
859 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has
861 if (strncmp(URL_SCHEME_PREFIX, packagesite,
862 strlen(URL_SCHEME_PREFIX)) == 0)
863 packagesite += strlen(URL_SCHEME_PREFIX);
864 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite);
866 snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX",
867 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
869 if ((fd_pkg = fetch_to_fd(url, tmppkg)) == -1)
872 if (signature_type != NULL &&
873 strcasecmp(signature_type, "NONE") != 0) {
874 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
876 snprintf(tmpsig, MAXPATHLEN, "%s/pkg.txz.sig.XXXXXX",
877 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
878 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.sig",
881 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
882 fprintf(stderr, "Signature for pkg not "
887 if (verify_signature(fd_pkg, fd_sig) == false)
889 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
891 snprintf(tmpsig, MAXPATHLEN,
892 "%s/pkg.txz.pubkeysig.XXXXXX",
893 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
894 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.pubkeysig",
897 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
898 fprintf(stderr, "Signature for pkg not "
903 if (verify_pubsignature(fd_pkg, fd_sig) == false)
906 warnx("Signature type %s is not supported for "
907 "bootstrapping.", signature_type);
912 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
913 ret = install_pkg_static(pkgstatic, tmppkg, force);
918 warnx("Error fetching %s: %s", url, fetchLastErrString);
919 fprintf(stderr, "A pre-built version of pkg could not be found for "
921 fprintf(stderr, "Consider changing PACKAGESITE or installing it from "
922 "ports: 'ports-mgmt/pkg'.\n");
938 static const char confirmation_message[] =
939 "The package management tool is not yet installed on your system.\n"
940 "Do you want to fetch and install it now? [y/N]: ";
942 static const char non_interactive_message[] =
943 "The package management tool is not yet installed on your system.\n"
944 "Please set ASSUME_ALWAYS_YES=yes environment variable to be able to bootstrap "
945 "in non-interactive (stdin not being a tty)\n";
948 pkg_query_yes_no(void)
954 if (c == 'y' || c == 'Y')
959 while (c != '\n' && c != EOF)
966 bootstrap_pkg_local(const char *pkgpath, bool force)
968 char path[MAXPATHLEN];
969 char pkgstatic[MAXPATHLEN];
970 const char *signature_type;
971 int fd_pkg, fd_sig, ret;
976 fd_pkg = open(pkgpath, O_RDONLY);
978 err(EXIT_FAILURE, "Unable to open %s", pkgpath);
980 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
981 warnx("Error looking up SIGNATURE_TYPE");
984 if (signature_type != NULL &&
985 strcasecmp(signature_type, "NONE") != 0) {
986 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
988 snprintf(path, sizeof(path), "%s.sig", pkgpath);
990 if ((fd_sig = open(path, O_RDONLY)) == -1) {
991 fprintf(stderr, "Signature for pkg not "
996 if (verify_signature(fd_pkg, fd_sig) == false)
999 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
1001 snprintf(path, sizeof(path), "%s.pubkeysig", pkgpath);
1003 if ((fd_sig = open(path, O_RDONLY)) == -1) {
1004 fprintf(stderr, "Signature for pkg not "
1009 if (verify_pubsignature(fd_pkg, fd_sig) == false)
1013 warnx("Signature type %s is not supported for "
1014 "bootstrapping.", signature_type);
1019 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
1020 ret = install_pkg_static(pkgstatic, pkgpath, force);
1031 main(int argc, char *argv[])
1033 char pkgpath[MAXPATHLEN];
1035 bool bootstrap_only, force, yes;
1037 bootstrap_only = false;
1042 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
1043 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
1045 if (argc > 1 && strcmp(argv[1], "bootstrap") == 0) {
1046 bootstrap_only = true;
1047 if (argc == 3 && strcmp(argv[2], "-f") == 0)
1051 if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) {
1053 * To allow 'pkg -N' to be used as a reliable test for whether
1054 * a system is configured to use pkg, don't bootstrap pkg
1055 * when that argument is given as argv[1].
1057 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
1058 errx(EXIT_FAILURE, "pkg is not installed");
1062 if (argc > 1 && strcmp(argv[1], "add") == 0) {
1063 if (argc > 2 && strcmp(argv[2], "-f") == 0) {
1068 if (pkgarg == NULL) {
1069 fprintf(stderr, "Path to pkg.txz required\n");
1072 if (access(pkgarg, R_OK) == -1) {
1073 fprintf(stderr, "No such file: %s\n", pkgarg);
1076 if (bootstrap_pkg_local(pkgarg, force) != 0)
1081 * Do not ask for confirmation if either of stdin or stdout is
1082 * not tty. Check the environment to see if user has answer
1083 * tucked in there already.
1085 config_bool(ASSUME_ALWAYS_YES, &yes);
1087 if (!isatty(fileno(stdin))) {
1088 fprintf(stderr, non_interactive_message);
1092 printf("%s", confirmation_message);
1093 if (pkg_query_yes_no() == 0)
1096 if (bootstrap_pkg(force) != 0)
1102 } else if (bootstrap_only) {
1103 printf("pkg already bootstrapped at %s\n", pkgpath);
1107 execv(pkgpath, argv);
1110 return (EXIT_FAILURE);