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>
54 #include <openssl/err.h>
55 #include <openssl/ssl.h>
57 #include "dns_utils.h"
83 STAILQ_ENTRY(fingerprint) next;
86 STAILQ_HEAD(fingerprint_list, fingerprint);
89 extract_pkg_static(int fd, char *p, int sz)
92 struct archive_entry *ae;
97 a = archive_read_new();
99 warn("archive_read_new");
102 archive_read_support_compression_all(a);
103 archive_read_support_format_tar(a);
105 if (lseek(fd, 0, 0) == -1) {
110 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
111 warnx("archive_read_open_fd: %s", archive_error_string(a));
116 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) {
117 end = strrchr(archive_entry_pathname(ae), '/');
121 if (strcmp(end, "/pkg-static") == 0) {
122 r = archive_read_extract(a, ae,
123 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
124 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
125 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
126 strlcpy(p, archive_entry_pathname(ae), sz);
134 warnx("failed to extract pkg-static: %s",
135 archive_error_string(a));
138 archive_read_free(a);
144 install_pkg_static(const char *path, const char *pkgpath, bool force)
149 switch ((pid = fork())) {
154 execl(path, "pkg-static", "add", "-f", pkgpath,
157 execl(path, "pkg-static", "add", pkgpath,
164 while (waitpid(pid, &pstat, 0) == -1)
168 if (WEXITSTATUS(pstat))
169 return (WEXITSTATUS(pstat));
170 else if (WIFSIGNALED(pstat))
171 return (128 & (WTERMSIG(pstat)));
176 fetch_to_fd(const char *url, char *path)
179 struct dns_srvinfo *mirrors, *current;
182 /* To store _https._tcp. + hostname + \0 */
184 int retry, max_retry;
188 char zone[MAXHOSTNAMELEN + 13];
189 static const char *mirror_type = NULL;
194 current = mirrors = NULL;
197 if (mirror_type == NULL && config_string(MIRROR_TYPE, &mirror_type)
199 warnx("No MIRROR_TYPE defined");
203 if ((fd = mkstemp(path)) == -1) {
210 u = fetchParseURL(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 (done < st.size) {
243 if ((r = fread(buf, 1, sizeof(buf), remote)) < 1)
246 if (write(fd, buf, r) != r) {
253 if (now > last || done == st.size)
276 static struct fingerprint *
277 parse_fingerprint(ucl_object_t *obj)
279 const ucl_object_t *cur;
280 ucl_object_iter_t it = NULL;
281 const char *function, *fp, *key;
282 struct fingerprint *f;
283 hash_t fct = HASH_UNKNOWN;
285 function = fp = NULL;
287 while ((cur = ucl_iterate_object(obj, &it, true))) {
288 key = ucl_object_key(cur);
289 if (cur->type != UCL_STRING)
291 if (strcasecmp(key, "function") == 0) {
292 function = ucl_object_tostring(cur);
295 if (strcasecmp(key, "fingerprint") == 0) {
296 fp = ucl_object_tostring(cur);
301 if (fp == NULL || function == NULL)
304 if (strcasecmp(function, "sha256") == 0)
307 if (fct == HASH_UNKNOWN) {
308 warnx("Unsupported hashing function: %s", function);
312 f = calloc(1, sizeof(struct fingerprint));
314 strlcpy(f->hash, fp, sizeof(f->hash));
320 free_fingerprint_list(struct fingerprint_list* list)
322 struct fingerprint *fingerprint, *tmp;
324 STAILQ_FOREACH_SAFE(fingerprint, list, next, tmp) {
325 free(fingerprint->name);
331 static struct fingerprint *
332 load_fingerprint(const char *dir, const char *filename)
334 ucl_object_t *obj = NULL;
335 struct ucl_parser *p = NULL;
336 struct fingerprint *f;
337 char path[MAXPATHLEN];
341 snprintf(path, MAXPATHLEN, "%s/%s", dir, filename);
343 p = ucl_parser_new(0);
344 if (!ucl_parser_add_file(p, path)) {
345 warnx("%s: %s", path, ucl_parser_get_error(p));
350 obj = ucl_parser_get_object(p);
352 if (obj->type == UCL_OBJECT)
353 f = parse_fingerprint(obj);
356 f->name = strdup(filename);
358 ucl_object_unref(obj);
364 static struct fingerprint_list *
365 load_fingerprints(const char *path, int *count)
369 struct fingerprint *finger;
370 struct fingerprint_list *fingerprints;
374 fingerprints = calloc(1, sizeof(struct fingerprint_list));
375 if (fingerprints == NULL)
377 STAILQ_INIT(fingerprints);
379 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_bin(char *buf, size_t len, char hash[SHA256_DIGEST_LENGTH])
415 SHA256_Init(&sha256);
416 SHA256_Update(&sha256, buf, len);
417 SHA256_Final(hash, &sha256);
422 sha256_buf(char *buf, size_t len, char out[SHA256_DIGEST_LENGTH * 2 + 1])
424 unsigned char hash[SHA256_DIGEST_LENGTH];
429 SHA256_Init(&sha256);
430 SHA256_Update(&sha256, buf, len);
431 SHA256_Final(hash, &sha256);
432 sha256_hash(hash, out);
436 sha256_fd(int fd, char out[SHA256_DIGEST_LENGTH * 2 + 1])
441 unsigned char hash[SHA256_DIGEST_LENGTH];
453 /* Duplicate the fd so that fclose(3) does not close it. */
454 if ((my_fd = dup(fd)) == -1) {
459 if ((fp = fdopen(my_fd, "rb")) == NULL) {
464 SHA256_Init(&sha256);
466 while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0)
467 SHA256_Update(&sha256, buffer, r);
469 if (ferror(fp) != 0) {
474 SHA256_Final(hash, &sha256);
475 sha256_hash(hash, out);
481 else if (my_fd != -1)
483 (void)lseek(fd, 0, SEEK_SET);
489 load_rsa_public_key_file(const char *file)
495 bp = BIO_new_file(file, "r");
497 errx(EXIT_FAILURE, "Unable to read %s", file);
499 if (!PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) {
500 warn("error reading public key: %s",
501 ERR_error_string(ERR_get_error(), errbuf));
512 load_rsa_public_key_buf(const unsigned char *cert, int certlen)
518 bp = BIO_new_mem_buf((void *)cert, certlen);
519 if (!PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) {
520 warn("error reading public key: %s",
521 ERR_error_string(ERR_get_error(), errbuf));
531 rsa_verify_cert(int fd, const char *sigfile, const unsigned char *key,
532 int keylen, unsigned char *sig, int siglen)
534 char sha256[SHA256_DIGEST_LENGTH *2 +1];
535 char hash[SHA256_DIGEST_LENGTH];
540 lseek(fd, 0, SEEK_SET);
541 sha256_fd(fd, sha256);
543 SSL_load_error_strings();
544 OpenSSL_add_all_algorithms();
545 OpenSSL_add_all_ciphers();
547 sha256_buf_bin(sha256, strlen(sha256), hash);
549 if (sigfile != NULL) {
550 rsa = load_rsa_public_key_file(sigfile);
552 rsa = load_rsa_public_key_buf(key, keylen);
556 ret = RSA_verify(NID_sha256, hash, sizeof(hash), sig, siglen, rsa);
558 warnx("%s: %s", key, ERR_error_string(ERR_get_error(), errbuf));
568 static struct pubkey *
576 if (lseek(fd, 0, 0) == -1) {
581 sig = sbuf_new_auto();
583 while ((r = read(fd, buf, sizeof(buf))) >0) {
584 sbuf_bcat(sig, buf, r);
588 pk = calloc(1, sizeof(struct pubkey));
589 pk->siglen = sbuf_len(sig);
590 pk->sig = calloc(1, pk->siglen);
591 memcpy(pk->sig, sbuf_data(sig), pk->siglen);
597 static struct sig_cert *
602 struct sbuf *buf, *sig, *cert;
613 if (lseek(fd, 0, 0) == -1) {
618 /* Duplicate the fd so that fclose(3) does not close it. */
619 if ((my_fd = dup(fd)) == -1) {
624 if ((fp = fdopen(my_fd, "rb")) == NULL) {
630 sig = sbuf_new_auto();
631 cert = sbuf_new_auto();
633 while ((linelen = getline(&line, &linecap, fp)) > 0) {
634 if (strcmp(line, "SIGNATURE\n") == 0) {
637 } else if (strcmp(line, "CERT\n") == 0) {
640 } else if (strcmp(line, "END\n") == 0) {
644 sbuf_bcat(buf, line, linelen);
649 /* Trim out unrelated trailing newline */
650 sbuf_setpos(sig, sbuf_len(sig) - 1);
655 sc = calloc(1, sizeof(struct sig_cert));
656 sc->siglen = sbuf_len(sig);
657 sc->sig = calloc(1, sc->siglen);
658 memcpy(sc->sig, sbuf_data(sig), sc->siglen);
660 sc->certlen = sbuf_len(cert);
661 sc->cert = strdup(sbuf_data(cert));
670 verify_pubsignature(int fd_pkg, int fd_sig)
679 if (config_string(PUBKEY, &pubkey) != 0) {
680 warnx("No CONFIG_PUBKEY defined");
684 if ((pk = read_pubkey(fd_sig)) == NULL) {
685 warnx("Error reading signature");
689 /* Verify the signature. */
690 printf("Verifying signature with public key %s... ", pubkey);
691 if (rsa_verify_cert(fd_pkg, pubkey, NULL, 0, pk->sig,
692 pk->siglen) == false) {
693 fprintf(stderr, "Signature is not valid\n");
709 verify_signature(int fd_pkg, int fd_sig)
711 struct fingerprint_list *trusted, *revoked;
712 struct fingerprint *fingerprint;
715 int trusted_count, revoked_count;
716 const char *fingerprints;
717 char path[MAXPATHLEN];
718 char hash[SHA256_DIGEST_LENGTH * 2 + 1];
721 trusted = revoked = NULL;
724 /* Read and parse fingerprints. */
725 if (config_string(FINGERPRINTS, &fingerprints) != 0) {
726 warnx("No CONFIG_FINGERPRINTS defined");
730 snprintf(path, MAXPATHLEN, "%s/trusted", fingerprints);
731 if ((trusted = load_fingerprints(path, &trusted_count)) == NULL) {
732 warnx("Error loading trusted certificates");
736 if (trusted_count == 0 || trusted == NULL) {
737 fprintf(stderr, "No trusted certificates found.\n");
741 snprintf(path, MAXPATHLEN, "%s/revoked", fingerprints);
742 if ((revoked = load_fingerprints(path, &revoked_count)) == NULL) {
743 warnx("Error loading revoked certificates");
747 /* Read certificate and signature in. */
748 if ((sc = parse_cert(fd_sig)) == NULL) {
749 warnx("Error parsing certificate");
752 /* Explicitly mark as non-trusted until proven otherwise. */
755 /* Parse signature and pubkey out of the certificate */
756 sha256_buf(sc->cert, sc->certlen, hash);
758 /* Check if this hash is revoked */
759 if (revoked != NULL) {
760 STAILQ_FOREACH(fingerprint, revoked, next) {
761 if (strcasecmp(fingerprint->hash, hash) == 0) {
762 fprintf(stderr, "The package was signed with "
763 "revoked certificate %s\n",
770 STAILQ_FOREACH(fingerprint, trusted, next) {
771 if (strcasecmp(fingerprint->hash, hash) == 0) {
773 sc->name = strdup(fingerprint->name);
778 if (sc->trusted == false) {
779 fprintf(stderr, "No trusted fingerprint found matching "
780 "package's certificate\n");
784 /* Verify the signature. */
785 printf("Verifying signature with trusted certificate %s... ", sc->name);
786 if (rsa_verify_cert(fd_pkg, NULL, sc->cert, sc->certlen, sc->sig,
787 sc->siglen) == false) {
789 fprintf(stderr, "Signature is not valid\n");
798 free_fingerprint_list(trusted);
800 free_fingerprint_list(revoked);
812 bootstrap_pkg(bool force)
816 char url[MAXPATHLEN];
817 char tmppkg[MAXPATHLEN];
818 char tmpsig[MAXPATHLEN];
819 const char *packagesite;
820 const char *signature_type;
821 char pkgstatic[MAXPATHLEN];
826 if (config_string(PACKAGESITE, &packagesite) != 0) {
827 warnx("No PACKAGESITE defined");
831 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
832 warnx("Error looking up SIGNATURE_TYPE");
836 printf("Bootstrapping pkg from %s, please wait...\n", packagesite);
838 /* Support pkg+http:// for PACKAGESITE which is the new format
839 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has
841 if (strncmp(URL_SCHEME_PREFIX, packagesite,
842 strlen(URL_SCHEME_PREFIX)) == 0)
843 packagesite += strlen(URL_SCHEME_PREFIX);
844 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite);
846 snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX",
847 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
849 if ((fd_pkg = fetch_to_fd(url, tmppkg)) == -1)
852 if (signature_type != NULL &&
853 strcasecmp(signature_type, "NONE") != 0) {
854 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
856 snprintf(tmpsig, MAXPATHLEN, "%s/pkg.txz.sig.XXXXXX",
857 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
858 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.sig",
861 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
862 fprintf(stderr, "Signature for pkg not "
867 if (verify_signature(fd_pkg, fd_sig) == false)
869 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
871 snprintf(tmpsig, MAXPATHLEN,
872 "%s/pkg.txz.pubkeysig.XXXXXX",
873 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP);
874 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz.pubkeysig",
877 if ((fd_sig = fetch_to_fd(url, tmpsig)) == -1) {
878 fprintf(stderr, "Signature for pkg not "
883 if (verify_pubsignature(fd_pkg, fd_sig) == false)
886 warnx("Signature type %s is not supported for "
887 "bootstrapping.", signature_type);
892 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
893 ret = install_pkg_static(pkgstatic, tmppkg, force);
898 warnx("Error fetching %s: %s", url, fetchLastErrString);
899 fprintf(stderr, "A pre-built version of pkg could not be found for "
901 fprintf(stderr, "Consider changing PACKAGESITE or installing it from "
902 "ports: 'ports-mgmt/pkg'.\n");
915 static const char confirmation_message[] =
916 "The package management tool is not yet installed on your system.\n"
917 "Do you want to fetch and install it now? [y/N]: ";
919 static const char non_interactive_message[] =
920 "The package management tool is not yet installed on your system.\n"
921 "Please set ASSUME_ALWAYS_YES=yes environment variable to be able to bootstrap "
922 "in non-interactive (stdin not being a tty)\n";
925 pkg_query_yes_no(void)
931 if (c == 'y' || c == 'Y')
936 while (c != '\n' && c != EOF)
943 bootstrap_pkg_local(const char *pkgpath, bool force)
945 char path[MAXPATHLEN];
946 char pkgstatic[MAXPATHLEN];
947 const char *signature_type;
948 int fd_pkg, fd_sig, ret;
953 fd_pkg = open(pkgpath, O_RDONLY);
955 err(EXIT_FAILURE, "Unable to open %s", pkgpath);
957 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
958 warnx("Error looking up SIGNATURE_TYPE");
961 if (signature_type != NULL &&
962 strcasecmp(signature_type, "NONE") != 0) {
963 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
965 snprintf(path, sizeof(path), "%s.sig", pkgpath);
967 if ((fd_sig = open(path, O_RDONLY)) == -1) {
968 fprintf(stderr, "Signature for pkg not "
973 if (verify_signature(fd_pkg, fd_sig) == false)
976 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
978 snprintf(path, sizeof(path), "%s.pubkeysig", pkgpath);
980 if ((fd_sig = open(path, O_RDONLY)) == -1) {
981 fprintf(stderr, "Signature for pkg not "
986 if (verify_pubsignature(fd_pkg, fd_sig) == false)
990 warnx("Signature type %s is not supported for "
991 "bootstrapping.", signature_type);
996 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
997 ret = install_pkg_static(pkgstatic, pkgpath, force);
1008 main(int argc, char *argv[])
1010 char pkgpath[MAXPATHLEN];
1012 bool bootstrap_only, force, yes;
1014 bootstrap_only = false;
1019 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
1020 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
1022 if (argc > 1 && strcmp(argv[1], "bootstrap") == 0) {
1023 bootstrap_only = true;
1024 if (argc == 3 && strcmp(argv[2], "-f") == 0)
1028 if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) {
1030 * To allow 'pkg -N' to be used as a reliable test for whether
1031 * a system is configured to use pkg, don't bootstrap pkg
1032 * when that argument is given as argv[1].
1034 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0)
1035 errx(EXIT_FAILURE, "pkg is not installed");
1039 if (argc > 1 && strcmp(argv[1], "add") == 0) {
1040 if (argc > 2 && strcmp(argv[2], "-f") == 0) {
1045 if (pkgarg == NULL) {
1046 fprintf(stderr, "Path to pkg.txz required\n");
1049 if (access(pkgarg, R_OK) == -1) {
1050 fprintf(stderr, "No such file: %s\n", pkgarg);
1053 if (bootstrap_pkg_local(pkgarg, force) != 0)
1058 * Do not ask for confirmation if either of stdin or stdout is
1059 * not tty. Check the environment to see if user has answer
1060 * tucked in there already.
1062 config_bool(ASSUME_ALWAYS_YES, &yes);
1064 if (!isatty(fileno(stdin))) {
1065 fprintf(stderr, non_interactive_message);
1069 printf("%s", confirmation_message);
1070 if (pkg_query_yes_no() == 0)
1073 if (bootstrap_pkg(force) != 0)
1079 } else if (bootstrap_only) {
1080 printf("pkg already bootstrapped at %s\n", pkgpath);
1084 execv(pkgpath, argv);
1087 return (EXIT_FAILURE);