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_compression_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("fail to extract pkg-static");
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 u = fetchParseURL(url);
206 while (remote == NULL) {
207 if (retry == max_retry) {
208 if (strcmp(u->scheme, "file") != 0 &&
209 strcasecmp(mirror_type, "srv") == 0) {
210 snprintf(zone, sizeof(zone),
211 "_%s._tcp.%s", u->scheme, u->host);
212 mirrors = dns_getsrvinfo(zone);
217 if (mirrors != NULL) {
218 strlcpy(u->host, current->host, sizeof(u->host));
219 u->port = current->port;
222 remote = fetchXGet(u, &st, "");
223 if (remote == NULL) {
227 if (mirrors == NULL) {
230 current = current->next;
237 while ((r = fread(buf, 1, sizeof(buf), remote)) > 0) {
238 if (write(fd, buf, r) != r) {
245 warn("An error occurred while fetching pkg(8)");
268 static struct fingerprint *
269 parse_fingerprint(ucl_object_t *obj)
272 ucl_object_iter_t it = NULL;
273 const char *function, *fp, *key;
274 struct fingerprint *f;
275 hash_t fct = HASH_UNKNOWN;
277 function = fp = NULL;
279 while ((cur = ucl_iterate_object(obj, &it, true))) {
280 key = ucl_object_key(cur);
281 if (cur->type != UCL_STRING)
283 if (strcasecmp(key, "function") == 0) {
284 function = ucl_object_tostring(cur);
287 if (strcasecmp(key, "fingerprint") == 0) {
288 fp = ucl_object_tostring(cur);
293 if (fp == NULL || function == NULL)
296 if (strcasecmp(function, "sha256") == 0)
299 if (fct == HASH_UNKNOWN) {
300 warnx("Unsupported hashing function: %s", function);
304 f = calloc(1, sizeof(struct fingerprint));
306 strlcpy(f->hash, fp, sizeof(f->hash));
312 free_fingerprint_list(struct fingerprint_list* list)
314 struct fingerprint *fingerprint, *tmp;
316 STAILQ_FOREACH_SAFE(fingerprint, list, next, tmp) {
317 free(fingerprint->name);
323 static struct fingerprint *
324 load_fingerprint(const char *dir, const char *filename)
326 ucl_object_t *obj = NULL;
327 struct ucl_parser *p = NULL;
328 struct fingerprint *f;
329 char path[MAXPATHLEN];
333 snprintf(path, MAXPATHLEN, "%s/%s", dir, filename);
335 p = ucl_parser_new(0);
336 if (!ucl_parser_add_file(p, path)) {
337 warnx("%s: %s", path, ucl_parser_get_error(p));
342 obj = ucl_parser_get_object(p);
344 if (obj->type == UCL_OBJECT)
345 f = parse_fingerprint(obj);
348 f->name = strdup(filename);
350 ucl_object_free(obj);
356 static struct fingerprint_list *
357 load_fingerprints(const char *path, int *count)
361 struct fingerprint *finger;
362 struct fingerprint_list *fingerprints;
366 fingerprints = calloc(1, sizeof(struct fingerprint_list));
367 if (fingerprints == NULL)
369 STAILQ_INIT(fingerprints);
371 if ((d = opendir(path)) == NULL)
374 while ((ent = readdir(d))) {
375 if (strcmp(ent->d_name, ".") == 0 ||
376 strcmp(ent->d_name, "..") == 0)
378 finger = load_fingerprint(path, ent->d_name);
379 if (finger != NULL) {
380 STAILQ_INSERT_TAIL(fingerprints, finger, next);
387 return (fingerprints);
391 sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH],
392 char out[SHA256_DIGEST_LENGTH * 2 + 1])
396 for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
397 sprintf(out + (i * 2), "%02x", hash[i]);
399 out[SHA256_DIGEST_LENGTH * 2] = '\0';
403 sha256_buf_bin(char *buf, size_t len, char hash[SHA256_DIGEST_LENGTH])
407 SHA256_Init(&sha256);
408 SHA256_Update(&sha256, buf, len);
409 SHA256_Final(hash, &sha256);
414 sha256_buf(char *buf, size_t len, char out[SHA256_DIGEST_LENGTH * 2 + 1])
416 unsigned char hash[SHA256_DIGEST_LENGTH];
421 SHA256_Init(&sha256);
422 SHA256_Update(&sha256, buf, len);
423 SHA256_Final(hash, &sha256);
424 sha256_hash(hash, out);
428 sha256_fd(int fd, char out[SHA256_DIGEST_LENGTH * 2 + 1])
433 unsigned char hash[SHA256_DIGEST_LENGTH];
445 /* Duplicate the fd so that fclose(3) does not close it. */
446 if ((my_fd = dup(fd)) == -1) {
451 if ((fp = fdopen(my_fd, "rb")) == NULL) {
456 SHA256_Init(&sha256);
458 while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0)
459 SHA256_Update(&sha256, buffer, r);
461 if (ferror(fp) != 0) {
466 SHA256_Final(hash, &sha256);
467 sha256_hash(hash, out);
473 else if (my_fd != -1)
475 (void)lseek(fd, 0, SEEK_SET);
481 load_rsa_public_key_file(const char *file)
487 bp = BIO_new_file(file, "r");
489 errx(EXIT_FAILURE, "Unable to read %s", file);
491 if (!PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) {
492 warn("error reading public key: %s",
493 ERR_error_string(ERR_get_error(), errbuf));
504 load_rsa_public_key_buf(unsigned char *cert, int certlen)
510 bp = BIO_new_mem_buf((void *)cert, certlen);
511 if (!PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) {
512 warn("error reading public key: %s",
513 ERR_error_string(ERR_get_error(), errbuf));
523 rsa_verify_cert(int fd, const char *sigfile, unsigned char *key,
524 int keylen, unsigned char *sig, int siglen)
526 char sha256[SHA256_DIGEST_LENGTH *2 +1];
527 char hash[SHA256_DIGEST_LENGTH];
532 lseek(fd, 0, SEEK_SET);
533 sha256_fd(fd, sha256);
535 SSL_load_error_strings();
536 OpenSSL_add_all_algorithms();
537 OpenSSL_add_all_ciphers();
539 sha256_buf_bin(sha256, strlen(sha256), hash);
541 if (sigfile != NULL) {
542 rsa = load_rsa_public_key_file(sigfile);
544 rsa = load_rsa_public_key_buf(key, keylen);
548 ret = RSA_verify(NID_sha256, hash, sizeof(hash), sig, siglen, rsa);
550 warnx("%s: %s", key, ERR_error_string(ERR_get_error(), errbuf));
560 static struct pubkey *
568 if (lseek(fd, 0, 0) == -1) {
573 sig = sbuf_new_auto();
575 while ((r = read(fd, buf, sizeof(buf))) >0) {
576 sbuf_bcat(sig, buf, r);
580 pk = calloc(1, sizeof(struct pubkey));
581 pk->siglen = sbuf_len(sig);
582 pk->sig = calloc(1, pk->siglen);
583 memcpy(pk->sig, sbuf_data(sig), pk->siglen);
589 static struct sig_cert *
594 struct sbuf *buf, *sig, *cert;
605 if (lseek(fd, 0, 0) == -1) {
610 /* Duplicate the fd so that fclose(3) does not close it. */
611 if ((my_fd = dup(fd)) == -1) {
616 if ((fp = fdopen(my_fd, "rb")) == NULL) {
622 sig = sbuf_new_auto();
623 cert = sbuf_new_auto();
625 while ((linelen = getline(&line, &linecap, fp)) > 0) {
626 if (strcmp(line, "SIGNATURE\n") == 0) {
629 } else if (strcmp(line, "CERT\n") == 0) {
632 } else if (strcmp(line, "END\n") == 0) {
636 sbuf_bcat(buf, line, linelen);
641 /* Trim out unrelated trailing newline */
642 sbuf_setpos(sig, sbuf_len(sig) - 1);
647 sc = calloc(1, sizeof(struct sig_cert));
648 sc->siglen = sbuf_len(sig);
649 sc->sig = calloc(1, sc->siglen);
650 memcpy(sc->sig, sbuf_data(sig), sc->siglen);
652 sc->certlen = sbuf_len(cert);
653 sc->cert = strdup(sbuf_data(cert));
662 verify_pubsignature(int fd_pkg, int fd_sig)
671 if (config_string(PUBKEY, &pubkey) != 0) {
672 warnx("No CONFIG_PUBKEY defined");
676 if ((pk = read_pubkey(fd_sig)) == NULL) {
677 warnx("Error reading signature");
681 /* Verify the signature. */
682 printf("Verifying signature with public key %s... ", pubkey);
683 if (rsa_verify_cert(fd_pkg, pubkey, NULL, 0, pk->sig,
684 pk->siglen) == false) {
685 fprintf(stderr, "Signature is not valid\n");
701 verify_signature(int fd_pkg, int fd_sig)
703 struct fingerprint_list *trusted, *revoked;
704 struct fingerprint *fingerprint;
707 int trusted_count, revoked_count;
708 const char *fingerprints;
709 char path[MAXPATHLEN];
710 char hash[SHA256_DIGEST_LENGTH * 2 + 1];
713 trusted = revoked = NULL;
716 /* Read and parse fingerprints. */
717 if (config_string(FINGERPRINTS, &fingerprints) != 0) {
718 warnx("No CONFIG_FINGERPRINTS defined");
722 snprintf(path, MAXPATHLEN, "%s/trusted", fingerprints);
723 if ((trusted = load_fingerprints(path, &trusted_count)) == NULL) {
724 warnx("Error loading trusted certificates");
728 if (trusted_count == 0 || trusted == NULL) {
729 fprintf(stderr, "No trusted certificates found.\n");
733 snprintf(path, MAXPATHLEN, "%s/revoked", fingerprints);
734 if ((revoked = load_fingerprints(path, &revoked_count)) == NULL) {
735 warnx("Error loading revoked certificates");
739 /* Read certificate and signature in. */
740 if ((sc = parse_cert(fd_sig)) == NULL) {
741 warnx("Error parsing certificate");
744 /* Explicitly mark as non-trusted until proven otherwise. */
747 /* Parse signature and pubkey out of the certificate */
748 sha256_buf(sc->cert, sc->certlen, hash);
750 /* Check if this hash is revoked */
751 if (revoked != NULL) {
752 STAILQ_FOREACH(fingerprint, revoked, next) {
753 if (strcasecmp(fingerprint->hash, hash) == 0) {
754 fprintf(stderr, "The package was signed with "
755 "revoked certificate %s\n",
762 STAILQ_FOREACH(fingerprint, trusted, next) {
763 if (strcasecmp(fingerprint->hash, hash) == 0) {
765 sc->name = strdup(fingerprint->name);
770 if (sc->trusted == false) {
771 fprintf(stderr, "No trusted fingerprint found matching "
772 "package's certificate\n");
776 /* Verify the signature. */
777 printf("Verifying signature with trusted certificate %s... ", sc->name);
778 if (rsa_verify_cert(fd_pkg, NULL, sc->cert, sc->certlen, sc->sig,
779 sc->siglen) == false) {
781 fprintf(stderr, "Signature is not valid\n");
790 free_fingerprint_list(trusted);
792 free_fingerprint_list(revoked);
804 bootstrap_pkg(bool force)
808 char url[MAXPATHLEN];
809 char tmppkg[MAXPATHLEN];
810 char tmpsig[MAXPATHLEN];
811 const char *packagesite;
812 const char *signature_type;
813 char pkgstatic[MAXPATHLEN];
818 if (config_string(PACKAGESITE, &packagesite) != 0) {
819 warnx("No PACKAGESITE defined");
823 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
824 warnx("Error looking up SIGNATURE_TYPE");
828 printf("Bootstrapping pkg from %s, please wait...\n", packagesite);
830 /* Support pkg+http:// for PACKAGESITE which is the new format
831 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has
833 if (strncmp(URL_SCHEME_PREFIX, packagesite,
834 strlen(URL_SCHEME_PREFIX)) == 0)
835 packagesite += strlen(URL_SCHEME_PREFIX);
836 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite);
838 snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX",
839 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
841 if ((fd_pkg = fetch_to_fd(url, tmppkg)) == -1)
844 if (signature_type != NULL &&
845 strcasecmp(signature_type, "NONE") != 0) {
846 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
848 snprintf(tmpsig, MAXPATHLEN, "%s/pkg.txz.sig.XXXXXX",
849 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
850 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.sig",
853 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
854 fprintf(stderr, "Signature for pkg not "
859 if (verify_signature(fd_pkg, fd_sig) == false)
861 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
863 snprintf(tmpsig, MAXPATHLEN,
864 "%s/pkg.txz.pubkeysig.XXXXXX",
865 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
866 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.pubkeysig",
869 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
870 fprintf(stderr, "Signature for pkg not "
875 if (verify_pubsignature(fd_pkg, fd_sig) == false)
878 warnx("Signature type %s is not supported for "
879 "bootstrapping.", signature_type);
884 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
885 ret = install_pkg_static(pkgstatic, tmppkg, force);
890 warnx("Error fetching %s: %s", url, fetchLastErrString);
891 fprintf(stderr, "A pre-built version of pkg could not be found for "
893 fprintf(stderr, "Consider changing PACKAGESITE or installing it from "
894 "ports: 'ports-mgmt/pkg'.\n");
907 static const char confirmation_message[] =
908 "The package management tool is not yet installed on your system.\n"
909 "Do you want to fetch and install it now? [y/N]: ";
911 static const char non_interactive_message[] =
912 "The package management tool is not yet installed on your system.\n"
913 "Please set ASSUME_ALWAYS_YES=yes environment variable to be able to bootstrap "
914 "in non-interactive (stdin not being a tty)\n";
917 pkg_query_yes_no(void)
923 if (c == 'y' || c == 'Y')
928 while (c != '\n' && c != EOF)
935 bootstrap_pkg_local(const char *pkgpath, bool force)
937 char path[MAXPATHLEN];
938 char pkgstatic[MAXPATHLEN];
939 const char *signature_type;
940 int fd_pkg, fd_sig, ret;
945 fd_pkg = open(pkgpath, O_RDONLY);
947 err(EXIT_FAILURE, "Unable to open %s", pkgpath);
949 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
950 warnx("Error looking up SIGNATURE_TYPE");
953 if (signature_type != NULL &&
954 strcasecmp(signature_type, "NONE") != 0) {
955 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
957 snprintf(path, sizeof(path), "%s.sig", pkgpath);
959 if ((fd_sig = open(path, O_RDONLY)) == -1) {
960 fprintf(stderr, "Signature for pkg not "
965 if (verify_signature(fd_pkg, fd_sig) == false)
968 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
970 snprintf(path, sizeof(path), "%s.pubkeysig", pkgpath);
972 if ((fd_sig = open(path, O_RDONLY)) == -1) {
973 fprintf(stderr, "Signature for pkg not "
978 if (verify_pubsignature(fd_pkg, fd_sig) == false)
982 warnx("Signature type %s is not supported for "
983 "bootstrapping.", signature_type);
988 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
989 ret = install_pkg_static(pkgstatic, pkgpath, force);
1000 main(int argc, char *argv[])
1002 char pkgpath[MAXPATHLEN];
1004 bool bootstrap_only, force, yes;
1006 bootstrap_only = false;
1011 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
1012 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
1014 if (argc > 1 && strcmp(argv[1], "bootstrap") == 0) {
1015 bootstrap_only = true;
1016 if (argc == 3 && strcmp(argv[2], "-f") == 0)
1020 if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) {
1022 * To allow 'pkg -N' to be used as a reliable test for whether
1023 * a system is configured to use pkg, don't bootstrap pkg
1024 * when that argument is given as argv[1].
1026 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
1027 errx(EXIT_FAILURE, "pkg is not installed");
1031 if (argc > 1 && strcmp(argv[1], "add") == 0) {
1032 if (argc > 2 && strcmp(argv[2], "-f") == 0) {
1037 if (pkgarg == NULL) {
1038 fprintf(stderr, "Path to pkg.txz required\n");
1041 if (access(pkgarg, R_OK) == -1) {
1042 fprintf(stderr, "No such file: %s\n", pkgarg);
1045 if (bootstrap_pkg_local(pkgarg, force) != 0)
1050 * Do not ask for confirmation if either of stdin or stdout is
1051 * not tty. Check the environment to see if user has answer
1052 * tucked in there already.
1054 config_bool(ASSUME_ALWAYS_YES, &yes);
1056 if (!isatty(fileno(stdin))) {
1057 fprintf(stderr, non_interactive_message);
1061 printf("%s", confirmation_message);
1062 if (pkg_query_yes_no() == 0)
1065 if (bootstrap_pkg(force) != 0)
1071 } else if (bootstrap_only) {
1072 printf("pkg already bootstrapped at %s\n", pkgpath);
1076 execv(pkgpath, argv);
1079 return (EXIT_FAILURE);