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("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 u = fetchParseURL(url);
207 while (remote == NULL) {
208 if (retry == max_retry) {
209 if (strcmp(u->scheme, "file") != 0 &&
210 strcasecmp(mirror_type, "srv") == 0) {
211 snprintf(zone, sizeof(zone),
212 "_%s._tcp.%s", u->scheme, u->host);
213 mirrors = dns_getsrvinfo(zone);
218 if (mirrors != NULL) {
219 strlcpy(u->host, current->host, sizeof(u->host));
220 u->port = current->port;
223 remote = fetchXGet(u, &st, "");
224 if (remote == NULL) {
228 if (mirrors == NULL) {
231 current = current->next;
238 while ((r = fread(buf, 1, sizeof(buf), remote)) > 0) {
239 if (write(fd, buf, r) != r) {
246 warn("An error occurred while fetching pkg(8)");
269 static struct fingerprint *
270 parse_fingerprint(ucl_object_t *obj)
272 const ucl_object_t *cur;
273 ucl_object_iter_t it = NULL;
274 const char *function, *fp, *key;
275 struct fingerprint *f;
276 hash_t fct = HASH_UNKNOWN;
278 function = fp = NULL;
280 while ((cur = ucl_iterate_object(obj, &it, true))) {
281 key = ucl_object_key(cur);
282 if (cur->type != UCL_STRING)
284 if (strcasecmp(key, "function") == 0) {
285 function = ucl_object_tostring(cur);
288 if (strcasecmp(key, "fingerprint") == 0) {
289 fp = ucl_object_tostring(cur);
294 if (fp == NULL || function == NULL)
297 if (strcasecmp(function, "sha256") == 0)
300 if (fct == HASH_UNKNOWN) {
301 warnx("Unsupported hashing function: %s", function);
305 f = calloc(1, sizeof(struct fingerprint));
307 strlcpy(f->hash, fp, sizeof(f->hash));
313 free_fingerprint_list(struct fingerprint_list* list)
315 struct fingerprint *fingerprint, *tmp;
317 STAILQ_FOREACH_SAFE(fingerprint, list, next, tmp) {
318 free(fingerprint->name);
324 static struct fingerprint *
325 load_fingerprint(const char *dir, const char *filename)
327 ucl_object_t *obj = NULL;
328 struct ucl_parser *p = NULL;
329 struct fingerprint *f;
330 char path[MAXPATHLEN];
334 snprintf(path, MAXPATHLEN, "%s/%s", dir, filename);
336 p = ucl_parser_new(0);
337 if (!ucl_parser_add_file(p, path)) {
338 warnx("%s: %s", path, ucl_parser_get_error(p));
343 obj = ucl_parser_get_object(p);
345 if (obj->type == UCL_OBJECT)
346 f = parse_fingerprint(obj);
349 f->name = strdup(filename);
351 ucl_object_unref(obj);
357 static struct fingerprint_list *
358 load_fingerprints(const char *path, int *count)
362 struct fingerprint *finger;
363 struct fingerprint_list *fingerprints;
367 fingerprints = calloc(1, sizeof(struct fingerprint_list));
368 if (fingerprints == NULL)
370 STAILQ_INIT(fingerprints);
372 if ((d = opendir(path)) == NULL)
375 while ((ent = readdir(d))) {
376 if (strcmp(ent->d_name, ".") == 0 ||
377 strcmp(ent->d_name, "..") == 0)
379 finger = load_fingerprint(path, ent->d_name);
380 if (finger != NULL) {
381 STAILQ_INSERT_TAIL(fingerprints, finger, next);
388 return (fingerprints);
392 sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH],
393 char out[SHA256_DIGEST_LENGTH * 2 + 1])
397 for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
398 sprintf(out + (i * 2), "%02x", hash[i]);
400 out[SHA256_DIGEST_LENGTH * 2] = '\0';
404 sha256_buf_bin(char *buf, size_t len, char hash[SHA256_DIGEST_LENGTH])
408 SHA256_Init(&sha256);
409 SHA256_Update(&sha256, buf, len);
410 SHA256_Final(hash, &sha256);
415 sha256_buf(char *buf, size_t len, char out[SHA256_DIGEST_LENGTH * 2 + 1])
417 unsigned char hash[SHA256_DIGEST_LENGTH];
422 SHA256_Init(&sha256);
423 SHA256_Update(&sha256, buf, len);
424 SHA256_Final(hash, &sha256);
425 sha256_hash(hash, out);
429 sha256_fd(int fd, char out[SHA256_DIGEST_LENGTH * 2 + 1])
434 unsigned char hash[SHA256_DIGEST_LENGTH];
446 /* Duplicate the fd so that fclose(3) does not close it. */
447 if ((my_fd = dup(fd)) == -1) {
452 if ((fp = fdopen(my_fd, "rb")) == NULL) {
457 SHA256_Init(&sha256);
459 while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0)
460 SHA256_Update(&sha256, buffer, r);
462 if (ferror(fp) != 0) {
467 SHA256_Final(hash, &sha256);
468 sha256_hash(hash, out);
474 else if (my_fd != -1)
476 (void)lseek(fd, 0, SEEK_SET);
482 load_rsa_public_key_file(const char *file)
488 bp = BIO_new_file(file, "r");
490 errx(EXIT_FAILURE, "Unable to read %s", file);
492 if (!PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) {
493 warn("error reading public key: %s",
494 ERR_error_string(ERR_get_error(), errbuf));
505 load_rsa_public_key_buf(unsigned char *cert, int certlen)
511 bp = BIO_new_mem_buf((void *)cert, certlen);
512 if (!PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) {
513 warn("error reading public key: %s",
514 ERR_error_string(ERR_get_error(), errbuf));
524 rsa_verify_cert(int fd, const char *sigfile, unsigned char *key,
525 int keylen, unsigned char *sig, int siglen)
527 char sha256[SHA256_DIGEST_LENGTH *2 +1];
528 char hash[SHA256_DIGEST_LENGTH];
533 lseek(fd, 0, SEEK_SET);
534 sha256_fd(fd, sha256);
536 SSL_load_error_strings();
537 OpenSSL_add_all_algorithms();
538 OpenSSL_add_all_ciphers();
540 sha256_buf_bin(sha256, strlen(sha256), hash);
542 if (sigfile != NULL) {
543 rsa = load_rsa_public_key_file(sigfile);
545 rsa = load_rsa_public_key_buf(key, keylen);
549 ret = RSA_verify(NID_sha256, hash, sizeof(hash), sig, siglen, rsa);
551 warnx("%s: %s", key, ERR_error_string(ERR_get_error(), errbuf));
561 static struct pubkey *
569 if (lseek(fd, 0, 0) == -1) {
574 sig = sbuf_new_auto();
576 while ((r = read(fd, buf, sizeof(buf))) >0) {
577 sbuf_bcat(sig, buf, r);
581 pk = calloc(1, sizeof(struct pubkey));
582 pk->siglen = sbuf_len(sig);
583 pk->sig = calloc(1, pk->siglen);
584 memcpy(pk->sig, sbuf_data(sig), pk->siglen);
590 static struct sig_cert *
595 struct sbuf *buf, *sig, *cert;
606 if (lseek(fd, 0, 0) == -1) {
611 /* Duplicate the fd so that fclose(3) does not close it. */
612 if ((my_fd = dup(fd)) == -1) {
617 if ((fp = fdopen(my_fd, "rb")) == NULL) {
623 sig = sbuf_new_auto();
624 cert = sbuf_new_auto();
626 while ((linelen = getline(&line, &linecap, fp)) > 0) {
627 if (strcmp(line, "SIGNATURE\n") == 0) {
630 } else if (strcmp(line, "CERT\n") == 0) {
633 } else if (strcmp(line, "END\n") == 0) {
637 sbuf_bcat(buf, line, linelen);
642 /* Trim out unrelated trailing newline */
643 sbuf_setpos(sig, sbuf_len(sig) - 1);
648 sc = calloc(1, sizeof(struct sig_cert));
649 sc->siglen = sbuf_len(sig);
650 sc->sig = calloc(1, sc->siglen);
651 memcpy(sc->sig, sbuf_data(sig), sc->siglen);
653 sc->certlen = sbuf_len(cert);
654 sc->cert = strdup(sbuf_data(cert));
663 verify_pubsignature(int fd_pkg, int fd_sig)
672 if (config_string(PUBKEY, &pubkey) != 0) {
673 warnx("No CONFIG_PUBKEY defined");
677 if ((pk = read_pubkey(fd_sig)) == NULL) {
678 warnx("Error reading signature");
682 /* Verify the signature. */
683 printf("Verifying signature with public key %s... ", pubkey);
684 if (rsa_verify_cert(fd_pkg, pubkey, NULL, 0, pk->sig,
685 pk->siglen) == false) {
686 fprintf(stderr, "Signature is not valid\n");
702 verify_signature(int fd_pkg, int fd_sig)
704 struct fingerprint_list *trusted, *revoked;
705 struct fingerprint *fingerprint;
708 int trusted_count, revoked_count;
709 const char *fingerprints;
710 char path[MAXPATHLEN];
711 char hash[SHA256_DIGEST_LENGTH * 2 + 1];
714 trusted = revoked = NULL;
717 /* Read and parse fingerprints. */
718 if (config_string(FINGERPRINTS, &fingerprints) != 0) {
719 warnx("No CONFIG_FINGERPRINTS defined");
723 snprintf(path, MAXPATHLEN, "%s/trusted", fingerprints);
724 if ((trusted = load_fingerprints(path, &trusted_count)) == NULL) {
725 warnx("Error loading trusted certificates");
729 if (trusted_count == 0 || trusted == NULL) {
730 fprintf(stderr, "No trusted certificates found.\n");
734 snprintf(path, MAXPATHLEN, "%s/revoked", fingerprints);
735 if ((revoked = load_fingerprints(path, &revoked_count)) == NULL) {
736 warnx("Error loading revoked certificates");
740 /* Read certificate and signature in. */
741 if ((sc = parse_cert(fd_sig)) == NULL) {
742 warnx("Error parsing certificate");
745 /* Explicitly mark as non-trusted until proven otherwise. */
748 /* Parse signature and pubkey out of the certificate */
749 sha256_buf(sc->cert, sc->certlen, hash);
751 /* Check if this hash is revoked */
752 if (revoked != NULL) {
753 STAILQ_FOREACH(fingerprint, revoked, next) {
754 if (strcasecmp(fingerprint->hash, hash) == 0) {
755 fprintf(stderr, "The package was signed with "
756 "revoked certificate %s\n",
763 STAILQ_FOREACH(fingerprint, trusted, next) {
764 if (strcasecmp(fingerprint->hash, hash) == 0) {
766 sc->name = strdup(fingerprint->name);
771 if (sc->trusted == false) {
772 fprintf(stderr, "No trusted fingerprint found matching "
773 "package's certificate\n");
777 /* Verify the signature. */
778 printf("Verifying signature with trusted certificate %s... ", sc->name);
779 if (rsa_verify_cert(fd_pkg, NULL, sc->cert, sc->certlen, sc->sig,
780 sc->siglen) == false) {
782 fprintf(stderr, "Signature is not valid\n");
791 free_fingerprint_list(trusted);
793 free_fingerprint_list(revoked);
805 bootstrap_pkg(bool force)
809 char url[MAXPATHLEN];
810 char tmppkg[MAXPATHLEN];
811 char tmpsig[MAXPATHLEN];
812 const char *packagesite;
813 const char *signature_type;
814 char pkgstatic[MAXPATHLEN];
819 if (config_string(PACKAGESITE, &packagesite) != 0) {
820 warnx("No PACKAGESITE defined");
824 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
825 warnx("Error looking up SIGNATURE_TYPE");
829 printf("Bootstrapping pkg from %s, please wait...\n", packagesite);
831 /* Support pkg+http:// for PACKAGESITE which is the new format
832 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has
834 if (strncmp(URL_SCHEME_PREFIX, packagesite,
835 strlen(URL_SCHEME_PREFIX)) == 0)
836 packagesite += strlen(URL_SCHEME_PREFIX);
837 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite);
839 snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX",
840 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
842 if ((fd_pkg = fetch_to_fd(url, tmppkg)) == -1)
845 if (signature_type != NULL &&
846 strcasecmp(signature_type, "NONE") != 0) {
847 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
849 snprintf(tmpsig, MAXPATHLEN, "%s/pkg.txz.sig.XXXXXX",
850 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
851 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.sig",
854 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
855 fprintf(stderr, "Signature for pkg not "
860 if (verify_signature(fd_pkg, fd_sig) == false)
862 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
864 snprintf(tmpsig, MAXPATHLEN,
865 "%s/pkg.txz.pubkeysig.XXXXXX",
866 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
867 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.pubkeysig",
870 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
871 fprintf(stderr, "Signature for pkg not "
876 if (verify_pubsignature(fd_pkg, fd_sig) == false)
879 warnx("Signature type %s is not supported for "
880 "bootstrapping.", signature_type);
885 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
886 ret = install_pkg_static(pkgstatic, tmppkg, force);
891 warnx("Error fetching %s: %s", url, fetchLastErrString);
892 fprintf(stderr, "A pre-built version of pkg could not be found for "
894 fprintf(stderr, "Consider changing PACKAGESITE or installing it from "
895 "ports: 'ports-mgmt/pkg'.\n");
908 static const char confirmation_message[] =
909 "The package management tool is not yet installed on your system.\n"
910 "Do you want to fetch and install it now? [y/N]: ";
912 static const char non_interactive_message[] =
913 "The package management tool is not yet installed on your system.\n"
914 "Please set ASSUME_ALWAYS_YES=yes environment variable to be able to bootstrap "
915 "in non-interactive (stdin not being a tty)\n";
918 pkg_query_yes_no(void)
924 if (c == 'y' || c == 'Y')
929 while (c != '\n' && c != EOF)
936 bootstrap_pkg_local(const char *pkgpath, bool force)
938 char path[MAXPATHLEN];
939 char pkgstatic[MAXPATHLEN];
940 const char *signature_type;
941 int fd_pkg, fd_sig, ret;
946 fd_pkg = open(pkgpath, O_RDONLY);
948 err(EXIT_FAILURE, "Unable to open %s", pkgpath);
950 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
951 warnx("Error looking up SIGNATURE_TYPE");
954 if (signature_type != NULL &&
955 strcasecmp(signature_type, "NONE") != 0) {
956 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
958 snprintf(path, sizeof(path), "%s.sig", pkgpath);
960 if ((fd_sig = open(path, O_RDONLY)) == -1) {
961 fprintf(stderr, "Signature for pkg not "
966 if (verify_signature(fd_pkg, fd_sig) == false)
969 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
971 snprintf(path, sizeof(path), "%s.pubkeysig", pkgpath);
973 if ((fd_sig = open(path, O_RDONLY)) == -1) {
974 fprintf(stderr, "Signature for pkg not "
979 if (verify_pubsignature(fd_pkg, fd_sig) == false)
983 warnx("Signature type %s is not supported for "
984 "bootstrapping.", signature_type);
989 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
990 ret = install_pkg_static(pkgstatic, pkgpath, force);
1001 main(int argc, char *argv[])
1003 char pkgpath[MAXPATHLEN];
1005 bool bootstrap_only, force, yes;
1007 bootstrap_only = false;
1012 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
1013 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
1015 if (argc > 1 && strcmp(argv[1], "bootstrap") == 0) {
1016 bootstrap_only = true;
1017 if (argc == 3 && strcmp(argv[2], "-f") == 0)
1021 if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) {
1023 * To allow 'pkg -N' to be used as a reliable test for whether
1024 * a system is configured to use pkg, don't bootstrap pkg
1025 * when that argument is given as argv[1].
1027 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
1028 errx(EXIT_FAILURE, "pkg is not installed");
1032 if (argc > 1 && strcmp(argv[1], "add") == 0) {
1033 if (argc > 2 && strcmp(argv[2], "-f") == 0) {
1038 if (pkgarg == NULL) {
1039 fprintf(stderr, "Path to pkg.txz required\n");
1042 if (access(pkgarg, R_OK) == -1) {
1043 fprintf(stderr, "No such file: %s\n", pkgarg);
1046 if (bootstrap_pkg_local(pkgarg, force) != 0)
1051 * Do not ask for confirmation if either of stdin or stdout is
1052 * not tty. Check the environment to see if user has answer
1053 * tucked in there already.
1055 config_bool(ASSUME_ALWAYS_YES, &yes);
1057 if (!isatty(fileno(stdin))) {
1058 fprintf(stderr, non_interactive_message);
1062 printf("%s", confirmation_message);
1063 if (pkg_query_yes_no() == 0)
1066 if (bootstrap_pkg(force) != 0)
1072 } else if (bootstrap_only) {
1073 printf("pkg already bootstrapped at %s\n", pkgpath);
1077 execv(pkgpath, argv);
1080 return (EXIT_FAILURE);