2 * SPDX-License-Identifier: BSD-2-Clause
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>
39 #include <archive_entry.h>
54 #include <openssl/err.h>
55 #include <openssl/ssl.h>
57 #include "dns_utils.h"
84 STAILQ_ENTRY(fingerprint) next;
87 static const char *bootstrap_names [] = {
93 STAILQ_HEAD(fingerprint_list, fingerprint);
98 extract_pkg_static(int fd, char *p, int sz)
101 struct archive_entry *ae;
106 a = archive_read_new();
108 warn("archive_read_new");
111 archive_read_support_filter_all(a);
112 archive_read_support_format_tar(a);
114 if (lseek(fd, 0, 0) == -1) {
119 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
120 warnx("archive_read_open_fd: %s", archive_error_string(a));
125 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) {
126 end = strrchr(archive_entry_pathname(ae), '/');
130 if (strcmp(end, "/pkg-static") == 0) {
131 r = archive_read_extract(a, ae,
132 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
133 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
134 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
135 strlcpy(p, archive_entry_pathname(ae), sz);
143 warnx("failed to extract pkg-static: %s",
144 archive_error_string(a));
147 archive_read_free(a);
153 install_pkg_static(const char *path, const char *pkgpath, bool force)
158 switch ((pid = fork())) {
163 execl(path, "pkg-static", "add", "-f", pkgpath,
166 execl(path, "pkg-static", "add", pkgpath,
173 while (waitpid(pid, &pstat, 0) == -1)
177 if (WEXITSTATUS(pstat))
178 return (WEXITSTATUS(pstat));
179 else if (WIFSIGNALED(pstat))
180 return (128 & (WTERMSIG(pstat)));
185 fetch_to_fd(const char *url, char *path, const char *fetchOpts)
188 struct dns_srvinfo *mirrors, *current;
191 /* To store _https._tcp. + hostname + \0 */
193 int retry, max_retry;
196 char zone[MAXHOSTNAMELEN + 13];
197 static const char *mirror_type = NULL;
200 current = mirrors = NULL;
203 if (mirror_type == NULL && config_string(MIRROR_TYPE, &mirror_type)
205 warnx("No MIRROR_TYPE defined");
209 if ((fd = mkstemp(path)) == -1) {
216 if ((u = fetchParseURL(url)) == NULL) {
217 warn("fetchParseURL('%s')", url);
221 while (remote == NULL) {
222 if (retry == max_retry) {
223 if (strcmp(u->scheme, "file") != 0 &&
224 strcasecmp(mirror_type, "srv") == 0) {
225 snprintf(zone, sizeof(zone),
226 "_%s._tcp.%s", u->scheme, u->host);
227 mirrors = dns_getsrvinfo(zone);
232 if (mirrors != NULL) {
233 strlcpy(u->host, current->host, sizeof(u->host));
234 u->port = current->port;
237 remote = fetchXGet(u, &st, fetchOpts);
238 if (remote == NULL) {
242 if (mirrors != NULL) {
243 current = current->next;
250 while ((r = fread(buf, 1, sizeof(buf), remote)) > 0) {
251 if (write(fd, buf, r) != r) {
258 warn("An error occurred while fetching pkg(8)");
281 static struct fingerprint *
282 parse_fingerprint(ucl_object_t *obj)
284 const ucl_object_t *cur;
285 ucl_object_iter_t it = NULL;
286 const char *function, *fp, *key;
287 struct fingerprint *f;
288 hash_t fct = HASH_UNKNOWN;
290 function = fp = NULL;
292 while ((cur = ucl_iterate_object(obj, &it, true))) {
293 key = ucl_object_key(cur);
294 if (cur->type != UCL_STRING)
296 if (strcasecmp(key, "function") == 0) {
297 function = ucl_object_tostring(cur);
300 if (strcasecmp(key, "fingerprint") == 0) {
301 fp = ucl_object_tostring(cur);
306 if (fp == NULL || function == NULL)
309 if (strcasecmp(function, "sha256") == 0)
312 if (fct == HASH_UNKNOWN) {
313 warnx("Unsupported hashing function: %s", function);
317 f = calloc(1, sizeof(struct fingerprint));
319 strlcpy(f->hash, fp, sizeof(f->hash));
325 free_fingerprint_list(struct fingerprint_list* list)
327 struct fingerprint *fingerprint, *tmp;
329 STAILQ_FOREACH_SAFE(fingerprint, list, next, tmp) {
330 free(fingerprint->name);
336 static struct fingerprint *
337 load_fingerprint(const char *dir, const char *filename)
339 ucl_object_t *obj = NULL;
340 struct ucl_parser *p = NULL;
341 struct fingerprint *f;
342 char path[MAXPATHLEN];
346 snprintf(path, MAXPATHLEN, "%s/%s", dir, filename);
348 p = ucl_parser_new(0);
349 if (!ucl_parser_add_file(p, path)) {
350 warnx("%s: %s", path, ucl_parser_get_error(p));
355 obj = ucl_parser_get_object(p);
357 if (obj->type == UCL_OBJECT)
358 f = parse_fingerprint(obj);
361 f->name = strdup(filename);
363 ucl_object_unref(obj);
369 static struct fingerprint_list *
370 load_fingerprints(const char *path, int *count)
374 struct fingerprint *finger;
375 struct fingerprint_list *fingerprints;
379 fingerprints = calloc(1, sizeof(struct fingerprint_list));
380 if (fingerprints == NULL)
382 STAILQ_INIT(fingerprints);
384 if ((d = opendir(path)) == NULL) {
390 while ((ent = readdir(d))) {
391 if (strcmp(ent->d_name, ".") == 0 ||
392 strcmp(ent->d_name, "..") == 0)
394 finger = load_fingerprint(path, ent->d_name);
395 if (finger != NULL) {
396 STAILQ_INSERT_TAIL(fingerprints, finger, next);
403 return (fingerprints);
407 load_public_key_file(const char *file)
413 bp = BIO_new_file(file, "r");
415 errx(EXIT_FAILURE, "Unable to read %s", file);
417 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
418 warnx("ici: %s", ERR_error_string(ERR_get_error(), errbuf));
426 load_public_key_buf(const unsigned char *cert, int certlen)
432 bp = BIO_new_mem_buf(__DECONST(void *, cert), certlen);
434 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
435 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
443 rsa_verify_cert(int fd, const char *sigfile, const unsigned char *key,
444 int keylen, unsigned char *sig, int siglen)
457 SSL_load_error_strings();
459 /* Compute SHA256 of the package. */
460 if (lseek(fd, 0, 0) == -1) {
464 if ((sha256 = sha256_fd(fd)) == NULL) {
465 warnx("Error creating SHA256 hash for package");
469 if (sigfile != NULL) {
470 if ((pkey = load_public_key_file(sigfile)) == NULL) {
471 warnx("Error reading public key");
475 if ((pkey = load_public_key_buf(key, keylen)) == NULL) {
476 warnx("Error reading public key");
481 /* Verify signature of the SHA256(pkg) is valid. */
482 if ((mdctx = EVP_MD_CTX_create()) == NULL) {
483 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
487 if (EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey) != 1) {
488 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
491 if (EVP_DigestVerifyUpdate(mdctx, sha256, strlen(sha256)) != 1) {
492 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
496 if (EVP_DigestVerifyFinal(mdctx, sig, siglen) != 1) {
497 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
513 EVP_MD_CTX_destroy(mdctx);
519 static struct pubkey *
529 if (lseek(fd, 0, 0) == -1) {
536 sig = open_memstream(&sigb, &sigsz);
538 err(EXIT_FAILURE, "open_memstream()");
540 while ((r = read(fd, buf, sizeof(buf))) >0) {
541 fwrite(buf, 1, r, sig);
545 pk = calloc(1, sizeof(struct pubkey));
547 pk->sig = calloc(1, pk->siglen);
548 memcpy(pk->sig, sigb, pk->siglen);
554 static struct sig_cert *
558 FILE *fp, *sigfp, *certfp, *tmpfp;
561 size_t linecap, sigsz, certsz;
568 sigfp = certfp = tmpfp = NULL;
570 if (lseek(fd, 0, 0) == -1) {
575 /* Duplicate the fd so that fclose(3) does not close it. */
576 if ((my_fd = dup(fd)) == -1) {
581 if ((fp = fdopen(my_fd, "rb")) == NULL) {
588 sigfp = open_memstream(&sig, &sigsz);
590 err(EXIT_FAILURE, "open_memstream()");
591 certfp = open_memstream(&cert, &certsz);
593 err(EXIT_FAILURE, "open_memstream()");
595 while ((linelen = getline(&line, &linecap, fp)) > 0) {
596 if (strcmp(line, "SIGNATURE\n") == 0) {
599 } else if (strcmp(line, "CERT\n") == 0) {
602 } else if (strcmp(line, "END\n") == 0) {
606 fwrite(line, 1, linelen, tmpfp);
613 sc = calloc(1, sizeof(struct sig_cert));
614 sc->siglen = sigsz -1; /* Trim out unrelated trailing newline */
617 sc->certlen = certsz;
624 verify_pubsignature(int fd_pkg, int fd_sig)
633 if (config_string(PUBKEY, &pubkey) != 0) {
634 warnx("No CONFIG_PUBKEY defined");
638 if ((pk = read_pubkey(fd_sig)) == NULL) {
639 warnx("Error reading signature");
643 /* Verify the signature. */
644 printf("Verifying signature with public key %s... ", pubkey);
645 if (rsa_verify_cert(fd_pkg, pubkey, NULL, 0, pk->sig,
646 pk->siglen) == false) {
647 fprintf(stderr, "Signature is not valid\n");
663 verify_signature(int fd_pkg, int fd_sig)
665 struct fingerprint_list *trusted, *revoked;
666 struct fingerprint *fingerprint;
669 int trusted_count, revoked_count;
670 const char *fingerprints;
671 char path[MAXPATHLEN];
676 trusted = revoked = NULL;
679 /* Read and parse fingerprints. */
680 if (config_string(FINGERPRINTS, &fingerprints) != 0) {
681 warnx("No CONFIG_FINGERPRINTS defined");
685 snprintf(path, MAXPATHLEN, "%s/trusted", fingerprints);
686 if ((trusted = load_fingerprints(path, &trusted_count)) == NULL) {
687 warnx("Error loading trusted certificates");
691 if (trusted_count == 0 || trusted == NULL) {
692 fprintf(stderr, "No trusted certificates found.\n");
696 snprintf(path, MAXPATHLEN, "%s/revoked", fingerprints);
697 if ((revoked = load_fingerprints(path, &revoked_count)) == NULL) {
698 warnx("Error loading revoked certificates");
702 /* Read certificate and signature in. */
703 if ((sc = parse_cert(fd_sig)) == NULL) {
704 warnx("Error parsing certificate");
707 /* Explicitly mark as non-trusted until proven otherwise. */
710 /* Parse signature and pubkey out of the certificate */
711 hash = sha256_buf(sc->cert, sc->certlen);
713 /* Check if this hash is revoked */
714 if (revoked != NULL) {
715 STAILQ_FOREACH(fingerprint, revoked, next) {
716 if (strcasecmp(fingerprint->hash, hash) == 0) {
717 fprintf(stderr, "The package was signed with "
718 "revoked certificate %s\n",
725 STAILQ_FOREACH(fingerprint, trusted, next) {
726 if (strcasecmp(fingerprint->hash, hash) == 0) {
728 sc->name = strdup(fingerprint->name);
733 if (sc->trusted == false) {
734 fprintf(stderr, "No trusted fingerprint found matching "
735 "package's certificate\n");
739 /* Verify the signature. */
740 printf("Verifying signature with trusted certificate %s... ", sc->name);
741 if (rsa_verify_cert(fd_pkg, NULL, sc->cert, sc->certlen, sc->sig,
742 sc->siglen) == false) {
743 fprintf(stderr, "Signature is not valid\n");
752 free_fingerprint_list(trusted);
754 free_fingerprint_list(revoked);
766 bootstrap_pkg(bool force, const char *fetchOpts)
770 char url[MAXPATHLEN];
771 char tmppkg[MAXPATHLEN];
772 char tmpsig[MAXPATHLEN];
773 const char *packagesite;
774 const char *signature_type;
775 char pkgstatic[MAXPATHLEN];
776 const char *bootstrap_name;
781 if (config_string(PACKAGESITE, &packagesite) != 0) {
782 warnx("No PACKAGESITE defined");
786 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
787 warnx("Error looking up SIGNATURE_TYPE");
791 printf("Bootstrapping pkg from %s, please wait...\n", packagesite);
793 /* Support pkg+http:// for PACKAGESITE which is the new format
794 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has
796 if (strncmp(URL_SCHEME_PREFIX, packagesite,
797 strlen(URL_SCHEME_PREFIX)) == 0)
798 packagesite += strlen(URL_SCHEME_PREFIX);
799 for (int j = 0; bootstrap_names[j] != NULL; j++) {
800 bootstrap_name = bootstrap_names[j];
802 snprintf(url, MAXPATHLEN, "%s/Latest/%s", packagesite, bootstrap_name);
803 snprintf(tmppkg, MAXPATHLEN, "%s/%s.XXXXXX",
804 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP,
806 if ((fd_pkg = fetch_to_fd(url, tmppkg, fetchOpts)) != -1)
808 bootstrap_name = NULL;
810 if (bootstrap_name == NULL)
813 if (signature_type != NULL &&
814 strcasecmp(signature_type, "NONE") != 0) {
815 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
817 snprintf(tmpsig, MAXPATHLEN, "%s/%s.sig.XXXXXX",
818 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP,
820 snprintf(url, MAXPATHLEN, "%s/Latest/%s.sig",
821 packagesite, bootstrap_name);
823 if ((fd_sig = fetch_to_fd(url, tmpsig, fetchOpts)) == -1) {
824 fprintf(stderr, "Signature for pkg not "
829 if (verify_signature(fd_pkg, fd_sig) == false)
831 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
833 snprintf(tmpsig, MAXPATHLEN,
834 "%s/%s.pubkeysig.XXXXXX",
835 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP,
837 snprintf(url, MAXPATHLEN, "%s/Latest/%s.pubkeysig",
838 packagesite, bootstrap_name);
840 if ((fd_sig = fetch_to_fd(url, tmpsig, fetchOpts)) == -1) {
841 fprintf(stderr, "Signature for pkg not "
846 if (verify_pubsignature(fd_pkg, fd_sig) == false)
849 warnx("Signature type %s is not supported for "
850 "bootstrapping.", signature_type);
855 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
856 ret = install_pkg_static(pkgstatic, tmppkg, force);
861 warnx("Error fetching %s: %s", url, fetchLastErrString);
862 if (fetchLastErrCode == FETCH_RESOLV) {
863 fprintf(stderr, "Address resolution failed for %s.\n", packagesite);
864 fprintf(stderr, "Consider changing PACKAGESITE.\n");
866 fprintf(stderr, "A pre-built version of pkg could not be found for "
868 fprintf(stderr, "Consider changing PACKAGESITE or installing it from "
869 "ports: 'ports-mgmt/pkg'.\n");
886 static const char confirmation_message[] =
887 "The package management tool is not yet installed on your system.\n"
888 "Do you want to fetch and install it now? [y/N]: ";
890 static const char non_interactive_message[] =
891 "The package management tool is not yet installed on your system.\n"
892 "Please set ASSUME_ALWAYS_YES=yes environment variable to be able to bootstrap "
893 "in non-interactive (stdin not being a tty)\n";
895 static const char args_bootstrap_message[] =
896 "Too many arguments\n"
897 "Usage: pkg [-4|-6] bootstrap [-f] [-y]\n";
899 static const char args_add_message[] =
900 "Too many arguments\n"
901 "Usage: pkg add [-f] [-y] {pkg.txz}\n";
904 pkg_query_yes_no(void)
911 if (c == 'y' || c == 'Y')
916 while (c != '\n' && c != EOF)
923 bootstrap_pkg_local(const char *pkgpath, bool force)
925 char path[MAXPATHLEN];
926 char pkgstatic[MAXPATHLEN];
927 const char *signature_type;
928 int fd_pkg, fd_sig, ret;
933 fd_pkg = open(pkgpath, O_RDONLY);
935 err(EXIT_FAILURE, "Unable to open %s", pkgpath);
937 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
938 warnx("Error looking up SIGNATURE_TYPE");
941 if (signature_type != NULL &&
942 strcasecmp(signature_type, "NONE") != 0) {
943 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
945 snprintf(path, sizeof(path), "%s.sig", pkgpath);
947 if ((fd_sig = open(path, O_RDONLY)) == -1) {
948 fprintf(stderr, "Signature for pkg not "
953 if (verify_signature(fd_pkg, fd_sig) == false)
956 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
958 snprintf(path, sizeof(path), "%s.pubkeysig", pkgpath);
960 if ((fd_sig = open(path, O_RDONLY)) == -1) {
961 fprintf(stderr, "Signature for pkg not "
966 if (verify_pubsignature(fd_pkg, fd_sig) == false)
970 warnx("Signature type %s is not supported for "
971 "bootstrapping.", signature_type);
976 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
977 ret = install_pkg_static(pkgstatic, pkgpath, force);
987 #define PKG_NAME "pkg"
988 #define PKG_DEVEL_NAME PKG_NAME "-devel"
989 #define PKG_PKG PKG_NAME "."
992 pkg_is_pkg_pkg(const char *pkg)
994 char *vstart, *basename;
998 if ((basename = strrchr(pkg, '/')) != NULL)
1002 * Chop off the final "-" (version delimiter) and check the name that
1003 * precedes it. If we didn't have a version delimiter, it must be the
1004 * pkg.$archive short form but we'll check it anyways. pkg-devel short
1005 * form will look like a pkg archive with 'devel' version, but that's
1006 * OK. We otherwise assumed that non-pkg packages will always have a
1007 * version component.
1009 vstart = strrchr(pkg, '-');
1010 if (vstart == NULL) {
1011 return (strlen(pkg) > sizeof(PKG_PKG) - 1 &&
1012 strncmp(pkg, PKG_PKG, sizeof(PKG_PKG) - 1) == 0);
1015 namelen = vstart - pkg;
1016 if (namelen == sizeof(PKG_NAME) - 1 &&
1017 strncmp(pkg, PKG_NAME, sizeof(PKG_NAME) - 1) == 0)
1019 if (namelen == sizeof(PKG_DEVEL_NAME) - 1 &&
1020 strncmp(pkg, PKG_DEVEL_NAME, sizeof(PKG_DEVEL_NAME) - 1) == 0)
1026 main(int argc, char *argv[])
1028 char pkgpath[MAXPATHLEN];
1029 const char *pkgarg, *repo_name;
1030 bool activation_test, add_pkg, bootstrap_only, force, yes;
1032 const char *fetchOpts;
1035 activation_test = false;
1037 bootstrap_only = false;
1045 struct option longopts[] = {
1046 { "debug", no_argument, NULL, 'd' },
1047 { "force", no_argument, NULL, 'f' },
1048 { "only-ipv4", no_argument, NULL, '4' },
1049 { "only-ipv6", no_argument, NULL, '6' },
1050 { "yes", no_argument, NULL, 'y' },
1051 { NULL, 0, NULL, 0 },
1054 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", getlocalbase());
1056 while ((ch = getopt_long(argc, argv, "-:dfr::yN46", longopts, NULL)) != -1) {
1065 activation_test = true;
1078 * The repository can only be specified for an explicit
1079 * bootstrap request at this time, so that we don't
1080 * confuse the user if they're trying to use a verb that
1081 * has some other conflicting meaning but we need to
1084 * For that reason, we specify that -r has an optional
1085 * argument above and process the next index ourselves.
1086 * This is mostly significant because getopt(3) will
1087 * otherwise eat the next argument, which could be
1088 * something we need to try and make sense of.
1090 * At worst this gets us false positives that we ignore
1091 * in other contexts, and we have to do a little fudging
1092 * in order to support separating -r from the reponame
1093 * with a space since it's not actually optional in
1094 * the bootstrap/add sense.
1096 if (add_pkg || bootstrap_only) {
1097 if (optarg != NULL) {
1099 } else if (optind < argc) {
1100 repo_name = argv[optind];
1103 if (repo_name == NULL || *repo_name == '\0') {
1105 "Must specify a repository with -r!\n");
1109 if (optarg == NULL) {
1110 /* Advance past repo name. */
1117 // Non-option arguments, first one is the command
1118 if (command == NULL) {
1119 command = argv[optind-1];
1120 if (strcmp(command, "add") == 0) {
1123 else if (strcmp(command, "bootstrap") == 0) {
1124 bootstrap_only = true;
1127 // bootstrap doesn't accept other arguments
1128 else if (bootstrap_only) {
1129 fprintf(stderr, args_bootstrap_message);
1132 else if (add_pkg && pkgarg != NULL) {
1134 * Additional arguments also means it's not a
1135 * local bootstrap request.
1141 * If it's not a request for pkg or pkg-devel,
1142 * then we must assume they were trying to
1143 * install some other local package and we
1144 * should try to bootstrap from the repo.
1146 if (!pkg_is_pkg_pkg(argv[optind-1])) {
1149 pkgarg = argv[optind-1];
1160 if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) {
1162 * To allow 'pkg -N' to be used as a reliable test for whether
1163 * a system is configured to use pkg, don't bootstrap pkg
1164 * when that option is passed.
1166 if (activation_test)
1167 errx(EXIT_FAILURE, "pkg is not installed");
1169 config_init(repo_name);
1172 if (pkgarg == NULL) {
1173 fprintf(stderr, "Path to pkg.txz required\n");
1176 if (access(pkgarg, R_OK) == -1) {
1177 fprintf(stderr, "No such file: %s\n", pkgarg);
1180 if (bootstrap_pkg_local(pkgarg, force) != 0)
1185 * Do not ask for confirmation if either of stdin or stdout is
1186 * not tty. Check the environment to see if user has answer
1187 * tucked in there already.
1190 config_bool(ASSUME_ALWAYS_YES, &yes);
1192 if (!isatty(fileno(stdin))) {
1193 fprintf(stderr, non_interactive_message);
1197 printf("%s", confirmation_message);
1198 if (pkg_query_yes_no() == 0)
1201 if (bootstrap_pkg(force, fetchOpts) != 0)
1207 } else if (bootstrap_only) {
1208 printf("pkg already bootstrapped at %s\n", pkgpath);
1212 execv(pkgpath, argv);
1215 return (EXIT_FAILURE);