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 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
37 #include <archive_entry.h>
52 #include <openssl/err.h>
53 #include <openssl/ssl.h>
55 #include "dns_utils.h"
82 STAILQ_ENTRY(fingerprint) next;
85 static const char *bootstrap_names [] = {
91 STAILQ_HEAD(fingerprint_list, fingerprint);
96 extract_pkg_static(int fd, char *p, int sz)
99 struct archive_entry *ae;
104 a = archive_read_new();
106 warn("archive_read_new");
109 archive_read_support_filter_all(a);
110 archive_read_support_format_tar(a);
112 if (lseek(fd, 0, 0) == -1) {
117 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
118 warnx("archive_read_open_fd: %s", archive_error_string(a));
123 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) {
124 end = strrchr(archive_entry_pathname(ae), '/');
128 if (strcmp(end, "/pkg-static") == 0) {
129 r = archive_read_extract(a, ae,
130 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
131 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
132 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
133 strlcpy(p, archive_entry_pathname(ae), sz);
141 warnx("failed to extract pkg-static: %s",
142 archive_error_string(a));
145 archive_read_free(a);
151 install_pkg_static(const char *path, const char *pkgpath, bool force)
156 switch ((pid = fork())) {
161 execl(path, "pkg-static", "add", "-f", pkgpath,
164 execl(path, "pkg-static", "add", pkgpath,
171 while (waitpid(pid, &pstat, 0) == -1)
175 if (WEXITSTATUS(pstat))
176 return (WEXITSTATUS(pstat));
177 else if (WIFSIGNALED(pstat))
178 return (128 & (WTERMSIG(pstat)));
183 fetch_to_fd(const char *url, char *path, const char *fetchOpts)
186 struct dns_srvinfo *mirrors, *current;
189 /* To store _https._tcp. + hostname + \0 */
191 int retry, max_retry;
194 char zone[MAXHOSTNAMELEN + 13];
195 static const char *mirror_type = NULL;
198 current = mirrors = NULL;
201 if (mirror_type == NULL && config_string(MIRROR_TYPE, &mirror_type)
203 warnx("No MIRROR_TYPE defined");
207 if ((fd = mkstemp(path)) == -1) {
214 if ((u = fetchParseURL(url)) == NULL) {
215 warn("fetchParseURL('%s')", url);
219 while (remote == NULL) {
220 if (retry == max_retry) {
221 if (strcmp(u->scheme, "file") != 0 &&
222 strcasecmp(mirror_type, "srv") == 0) {
223 snprintf(zone, sizeof(zone),
224 "_%s._tcp.%s", u->scheme, u->host);
225 mirrors = dns_getsrvinfo(zone);
230 if (mirrors != NULL) {
231 strlcpy(u->host, current->host, sizeof(u->host));
232 u->port = current->port;
235 remote = fetchXGet(u, &st, fetchOpts);
236 if (remote == NULL) {
240 if (mirrors != NULL) {
241 current = current->next;
248 while ((r = fread(buf, 1, sizeof(buf), remote)) > 0) {
249 if (write(fd, buf, r) != r) {
256 warn("An error occurred while fetching pkg(8)");
279 static struct fingerprint *
280 parse_fingerprint(ucl_object_t *obj)
282 const ucl_object_t *cur;
283 ucl_object_iter_t it = NULL;
284 const char *function, *fp, *key;
285 struct fingerprint *f;
286 hash_t fct = HASH_UNKNOWN;
288 function = fp = NULL;
290 while ((cur = ucl_iterate_object(obj, &it, true))) {
291 key = ucl_object_key(cur);
292 if (cur->type != UCL_STRING)
294 if (strcasecmp(key, "function") == 0) {
295 function = ucl_object_tostring(cur);
298 if (strcasecmp(key, "fingerprint") == 0) {
299 fp = ucl_object_tostring(cur);
304 if (fp == NULL || function == NULL)
307 if (strcasecmp(function, "sha256") == 0)
310 if (fct == HASH_UNKNOWN) {
311 warnx("Unsupported hashing function: %s", function);
315 f = calloc(1, sizeof(struct fingerprint));
317 strlcpy(f->hash, fp, sizeof(f->hash));
323 free_fingerprint_list(struct fingerprint_list* list)
325 struct fingerprint *fingerprint, *tmp;
327 STAILQ_FOREACH_SAFE(fingerprint, list, next, tmp) {
328 free(fingerprint->name);
334 static struct fingerprint *
335 load_fingerprint(const char *dir, const char *filename)
337 ucl_object_t *obj = NULL;
338 struct ucl_parser *p = NULL;
339 struct fingerprint *f;
340 char path[MAXPATHLEN];
344 snprintf(path, MAXPATHLEN, "%s/%s", dir, filename);
346 p = ucl_parser_new(0);
347 if (!ucl_parser_add_file(p, path)) {
348 warnx("%s: %s", path, ucl_parser_get_error(p));
353 obj = ucl_parser_get_object(p);
355 if (obj->type == UCL_OBJECT)
356 f = parse_fingerprint(obj);
359 f->name = strdup(filename);
361 ucl_object_unref(obj);
367 static struct fingerprint_list *
368 load_fingerprints(const char *path, int *count)
372 struct fingerprint *finger;
373 struct fingerprint_list *fingerprints;
377 fingerprints = calloc(1, sizeof(struct fingerprint_list));
378 if (fingerprints == NULL)
380 STAILQ_INIT(fingerprints);
382 if ((d = opendir(path)) == NULL) {
388 while ((ent = readdir(d))) {
389 if (strcmp(ent->d_name, ".") == 0 ||
390 strcmp(ent->d_name, "..") == 0)
392 finger = load_fingerprint(path, ent->d_name);
393 if (finger != NULL) {
394 STAILQ_INSERT_TAIL(fingerprints, finger, next);
401 return (fingerprints);
405 load_public_key_file(const char *file)
411 bp = BIO_new_file(file, "r");
413 errx(EXIT_FAILURE, "Unable to read %s", file);
415 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
416 warnx("ici: %s", ERR_error_string(ERR_get_error(), errbuf));
424 load_public_key_buf(const unsigned char *cert, int certlen)
430 bp = BIO_new_mem_buf(__DECONST(void *, cert), certlen);
432 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
433 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
441 rsa_verify_cert(int fd, const char *sigfile, const unsigned char *key,
442 int keylen, unsigned char *sig, int siglen)
455 SSL_load_error_strings();
457 /* Compute SHA256 of the package. */
458 if (lseek(fd, 0, 0) == -1) {
462 if ((sha256 = sha256_fd(fd)) == NULL) {
463 warnx("Error creating SHA256 hash for package");
467 if (sigfile != NULL) {
468 if ((pkey = load_public_key_file(sigfile)) == NULL) {
469 warnx("Error reading public key");
473 if ((pkey = load_public_key_buf(key, keylen)) == NULL) {
474 warnx("Error reading public key");
479 /* Verify signature of the SHA256(pkg) is valid. */
480 if ((mdctx = EVP_MD_CTX_create()) == NULL) {
481 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
485 if (EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey) != 1) {
486 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
489 if (EVP_DigestVerifyUpdate(mdctx, sha256, strlen(sha256)) != 1) {
490 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
494 if (EVP_DigestVerifyFinal(mdctx, sig, siglen) != 1) {
495 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
511 EVP_MD_CTX_destroy(mdctx);
517 static struct pubkey *
527 if (lseek(fd, 0, 0) == -1) {
534 sig = open_memstream(&sigb, &sigsz);
536 err(EXIT_FAILURE, "open_memstream()");
538 while ((r = read(fd, buf, sizeof(buf))) >0) {
539 fwrite(buf, 1, r, sig);
543 pk = calloc(1, sizeof(struct pubkey));
545 pk->sig = calloc(1, pk->siglen);
546 memcpy(pk->sig, sigb, pk->siglen);
552 static struct sig_cert *
556 FILE *fp, *sigfp, *certfp, *tmpfp;
559 size_t linecap, sigsz, certsz;
566 sigfp = certfp = tmpfp = NULL;
568 if (lseek(fd, 0, 0) == -1) {
573 /* Duplicate the fd so that fclose(3) does not close it. */
574 if ((my_fd = dup(fd)) == -1) {
579 if ((fp = fdopen(my_fd, "rb")) == NULL) {
586 sigfp = open_memstream(&sig, &sigsz);
588 err(EXIT_FAILURE, "open_memstream()");
589 certfp = open_memstream(&cert, &certsz);
591 err(EXIT_FAILURE, "open_memstream()");
593 while ((linelen = getline(&line, &linecap, fp)) > 0) {
594 if (strcmp(line, "SIGNATURE\n") == 0) {
597 } else if (strcmp(line, "CERT\n") == 0) {
600 } else if (strcmp(line, "END\n") == 0) {
604 fwrite(line, 1, linelen, tmpfp);
611 sc = calloc(1, sizeof(struct sig_cert));
612 sc->siglen = sigsz -1; /* Trim out unrelated trailing newline */
615 sc->certlen = certsz;
622 verify_pubsignature(int fd_pkg, int fd_sig)
631 if (config_string(PUBKEY, &pubkey) != 0) {
632 warnx("No CONFIG_PUBKEY defined");
636 if ((pk = read_pubkey(fd_sig)) == NULL) {
637 warnx("Error reading signature");
641 /* Verify the signature. */
642 printf("Verifying signature with public key %s... ", pubkey);
643 if (rsa_verify_cert(fd_pkg, pubkey, NULL, 0, pk->sig,
644 pk->siglen) == false) {
645 fprintf(stderr, "Signature is not valid\n");
661 verify_signature(int fd_pkg, int fd_sig)
663 struct fingerprint_list *trusted, *revoked;
664 struct fingerprint *fingerprint;
667 int trusted_count, revoked_count;
668 const char *fingerprints;
669 char path[MAXPATHLEN];
674 trusted = revoked = NULL;
677 /* Read and parse fingerprints. */
678 if (config_string(FINGERPRINTS, &fingerprints) != 0) {
679 warnx("No CONFIG_FINGERPRINTS defined");
683 snprintf(path, MAXPATHLEN, "%s/trusted", fingerprints);
684 if ((trusted = load_fingerprints(path, &trusted_count)) == NULL) {
685 warnx("Error loading trusted certificates");
689 if (trusted_count == 0 || trusted == NULL) {
690 fprintf(stderr, "No trusted certificates found.\n");
694 snprintf(path, MAXPATHLEN, "%s/revoked", fingerprints);
695 if ((revoked = load_fingerprints(path, &revoked_count)) == NULL) {
696 warnx("Error loading revoked certificates");
700 /* Read certificate and signature in. */
701 if ((sc = parse_cert(fd_sig)) == NULL) {
702 warnx("Error parsing certificate");
705 /* Explicitly mark as non-trusted until proven otherwise. */
708 /* Parse signature and pubkey out of the certificate */
709 hash = sha256_buf(sc->cert, sc->certlen);
711 /* Check if this hash is revoked */
712 if (revoked != NULL) {
713 STAILQ_FOREACH(fingerprint, revoked, next) {
714 if (strcasecmp(fingerprint->hash, hash) == 0) {
715 fprintf(stderr, "The package was signed with "
716 "revoked certificate %s\n",
723 STAILQ_FOREACH(fingerprint, trusted, next) {
724 if (strcasecmp(fingerprint->hash, hash) == 0) {
726 sc->name = strdup(fingerprint->name);
731 if (sc->trusted == false) {
732 fprintf(stderr, "No trusted fingerprint found matching "
733 "package's certificate\n");
737 /* Verify the signature. */
738 printf("Verifying signature with trusted certificate %s... ", sc->name);
739 if (rsa_verify_cert(fd_pkg, NULL, sc->cert, sc->certlen, sc->sig,
740 sc->siglen) == false) {
741 fprintf(stderr, "Signature is not valid\n");
750 free_fingerprint_list(trusted);
752 free_fingerprint_list(revoked);
764 bootstrap_pkg(bool force, const char *fetchOpts)
768 char url[MAXPATHLEN];
769 char tmppkg[MAXPATHLEN];
770 char tmpsig[MAXPATHLEN];
771 const char *packagesite;
772 const char *signature_type;
773 char pkgstatic[MAXPATHLEN];
774 const char *bootstrap_name;
779 if (config_string(PACKAGESITE, &packagesite) != 0) {
780 warnx("No PACKAGESITE defined");
784 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
785 warnx("Error looking up SIGNATURE_TYPE");
789 printf("Bootstrapping pkg from %s, please wait...\n", packagesite);
791 /* Support pkg+http:// for PACKAGESITE which is the new format
792 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has
794 if (strncmp(URL_SCHEME_PREFIX, packagesite,
795 strlen(URL_SCHEME_PREFIX)) == 0)
796 packagesite += strlen(URL_SCHEME_PREFIX);
797 for (int j = 0; bootstrap_names[j] != NULL; j++) {
798 bootstrap_name = bootstrap_names[j];
800 snprintf(url, MAXPATHLEN, "%s/Latest/%s", packagesite, bootstrap_name);
801 snprintf(tmppkg, MAXPATHLEN, "%s/%s.XXXXXX",
802 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP,
804 if ((fd_pkg = fetch_to_fd(url, tmppkg, fetchOpts)) != -1)
806 bootstrap_name = NULL;
808 if (bootstrap_name == NULL)
811 if (signature_type != NULL &&
812 strcasecmp(signature_type, "NONE") != 0) {
813 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
815 snprintf(tmpsig, MAXPATHLEN, "%s/%s.sig.XXXXXX",
816 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP,
818 snprintf(url, MAXPATHLEN, "%s/Latest/%s.sig",
819 packagesite, bootstrap_name);
821 if ((fd_sig = fetch_to_fd(url, tmpsig, fetchOpts)) == -1) {
822 fprintf(stderr, "Signature for pkg not "
827 if (verify_signature(fd_pkg, fd_sig) == false)
829 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
831 snprintf(tmpsig, MAXPATHLEN,
832 "%s/%s.pubkeysig.XXXXXX",
833 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP,
835 snprintf(url, MAXPATHLEN, "%s/Latest/%s.pubkeysig",
836 packagesite, bootstrap_name);
838 if ((fd_sig = fetch_to_fd(url, tmpsig, fetchOpts)) == -1) {
839 fprintf(stderr, "Signature for pkg not "
844 if (verify_pubsignature(fd_pkg, fd_sig) == false)
847 warnx("Signature type %s is not supported for "
848 "bootstrapping.", signature_type);
853 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
854 ret = install_pkg_static(pkgstatic, tmppkg, force);
859 warnx("Error fetching %s: %s", url, fetchLastErrString);
860 if (fetchLastErrCode == FETCH_RESOLV) {
861 fprintf(stderr, "Address resolution failed for %s.\n", packagesite);
862 fprintf(stderr, "Consider changing PACKAGESITE.\n");
864 fprintf(stderr, "A pre-built version of pkg could not be found for "
866 fprintf(stderr, "Consider changing PACKAGESITE or installing it from "
867 "ports: 'ports-mgmt/pkg'.\n");
884 static const char confirmation_message[] =
885 "The package management tool is not yet installed on your system.\n"
886 "Do you want to fetch and install it now? [y/N]: ";
888 static const char non_interactive_message[] =
889 "The package management tool is not yet installed on your system.\n"
890 "Please set ASSUME_ALWAYS_YES=yes environment variable to be able to bootstrap "
891 "in non-interactive (stdin not being a tty)\n";
893 static const char args_bootstrap_message[] =
894 "Too many arguments\n"
895 "Usage: pkg [-4|-6] bootstrap [-f] [-y]\n";
897 static const char args_add_message[] =
898 "Too many arguments\n"
899 "Usage: pkg add [-f] [-y] {pkg.txz}\n";
902 pkg_query_yes_no(void)
909 if (c == 'y' || c == 'Y')
914 while (c != '\n' && c != EOF)
921 bootstrap_pkg_local(const char *pkgpath, bool force)
923 char path[MAXPATHLEN];
924 char pkgstatic[MAXPATHLEN];
925 const char *signature_type;
926 int fd_pkg, fd_sig, ret;
931 fd_pkg = open(pkgpath, O_RDONLY);
933 err(EXIT_FAILURE, "Unable to open %s", pkgpath);
935 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
936 warnx("Error looking up SIGNATURE_TYPE");
939 if (signature_type != NULL &&
940 strcasecmp(signature_type, "NONE") != 0) {
941 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
943 snprintf(path, sizeof(path), "%s.sig", pkgpath);
945 if ((fd_sig = open(path, O_RDONLY)) == -1) {
946 fprintf(stderr, "Signature for pkg not "
951 if (verify_signature(fd_pkg, fd_sig) == false)
954 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
956 snprintf(path, sizeof(path), "%s.pubkeysig", pkgpath);
958 if ((fd_sig = open(path, O_RDONLY)) == -1) {
959 fprintf(stderr, "Signature for pkg not "
964 if (verify_pubsignature(fd_pkg, fd_sig) == false)
968 warnx("Signature type %s is not supported for "
969 "bootstrapping.", signature_type);
974 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
975 ret = install_pkg_static(pkgstatic, pkgpath, force);
985 #define PKG_NAME "pkg"
986 #define PKG_DEVEL_NAME PKG_NAME "-devel"
987 #define PKG_PKG PKG_NAME "."
990 pkg_is_pkg_pkg(const char *pkg)
992 char *vstart, *basename;
996 if ((basename = strrchr(pkg, '/')) != NULL)
1000 * Chop off the final "-" (version delimiter) and check the name that
1001 * precedes it. If we didn't have a version delimiter, it must be the
1002 * pkg.$archive short form but we'll check it anyways. pkg-devel short
1003 * form will look like a pkg archive with 'devel' version, but that's
1004 * OK. We otherwise assumed that non-pkg packages will always have a
1005 * version component.
1007 vstart = strrchr(pkg, '-');
1008 if (vstart == NULL) {
1009 return (strlen(pkg) > sizeof(PKG_PKG) - 1 &&
1010 strncmp(pkg, PKG_PKG, sizeof(PKG_PKG) - 1) == 0);
1013 namelen = vstart - pkg;
1014 if (namelen == sizeof(PKG_NAME) - 1 &&
1015 strncmp(pkg, PKG_NAME, sizeof(PKG_NAME) - 1) == 0)
1017 if (namelen == sizeof(PKG_DEVEL_NAME) - 1 &&
1018 strncmp(pkg, PKG_DEVEL_NAME, sizeof(PKG_DEVEL_NAME) - 1) == 0)
1024 main(int argc, char *argv[])
1026 char pkgpath[MAXPATHLEN];
1027 const char *pkgarg, *repo_name;
1028 bool activation_test, add_pkg, bootstrap_only, force, yes;
1030 const char *fetchOpts;
1033 activation_test = false;
1035 bootstrap_only = false;
1043 struct option longopts[] = {
1044 { "debug", no_argument, NULL, 'd' },
1045 { "force", no_argument, NULL, 'f' },
1046 { "only-ipv4", no_argument, NULL, '4' },
1047 { "only-ipv6", no_argument, NULL, '6' },
1048 { "yes", no_argument, NULL, 'y' },
1049 { NULL, 0, NULL, 0 },
1052 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", getlocalbase());
1054 while ((ch = getopt_long(argc, argv, "-:dfr::yN46", longopts, NULL)) != -1) {
1063 activation_test = true;
1076 * The repository can only be specified for an explicit
1077 * bootstrap request at this time, so that we don't
1078 * confuse the user if they're trying to use a verb that
1079 * has some other conflicting meaning but we need to
1082 * For that reason, we specify that -r has an optional
1083 * argument above and process the next index ourselves.
1084 * This is mostly significant because getopt(3) will
1085 * otherwise eat the next argument, which could be
1086 * something we need to try and make sense of.
1088 * At worst this gets us false positives that we ignore
1089 * in other contexts, and we have to do a little fudging
1090 * in order to support separating -r from the reponame
1091 * with a space since it's not actually optional in
1092 * the bootstrap/add sense.
1094 if (add_pkg || bootstrap_only) {
1095 if (optarg != NULL) {
1097 } else if (optind < argc) {
1098 repo_name = argv[optind];
1101 if (repo_name == NULL || *repo_name == '\0') {
1103 "Must specify a repository with -r!\n");
1107 if (optarg == NULL) {
1108 /* Advance past repo name. */
1115 // Non-option arguments, first one is the command
1116 if (command == NULL) {
1117 command = argv[optind-1];
1118 if (strcmp(command, "add") == 0) {
1121 else if (strcmp(command, "bootstrap") == 0) {
1122 bootstrap_only = true;
1125 // bootstrap doesn't accept other arguments
1126 else if (bootstrap_only) {
1127 fprintf(stderr, args_bootstrap_message);
1130 else if (add_pkg && pkgarg != NULL) {
1132 * Additional arguments also means it's not a
1133 * local bootstrap request.
1139 * If it's not a request for pkg or pkg-devel,
1140 * then we must assume they were trying to
1141 * install some other local package and we
1142 * should try to bootstrap from the repo.
1144 if (!pkg_is_pkg_pkg(argv[optind-1])) {
1147 pkgarg = argv[optind-1];
1158 if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) {
1160 * To allow 'pkg -N' to be used as a reliable test for whether
1161 * a system is configured to use pkg, don't bootstrap pkg
1162 * when that option is passed.
1164 if (activation_test)
1165 errx(EXIT_FAILURE, "pkg is not installed");
1167 config_init(repo_name);
1170 if (pkgarg == NULL) {
1171 fprintf(stderr, "Path to pkg.txz required\n");
1174 if (access(pkgarg, R_OK) == -1) {
1175 fprintf(stderr, "No such file: %s\n", pkgarg);
1178 if (bootstrap_pkg_local(pkgarg, force) != 0)
1183 * Do not ask for confirmation if either of stdin or stdout is
1184 * not tty. Check the environment to see if user has answer
1185 * tucked in there already.
1188 config_bool(ASSUME_ALWAYS_YES, &yes);
1190 if (!isatty(fileno(stdin))) {
1191 fprintf(stderr, non_interactive_message);
1195 printf("%s", confirmation_message);
1196 if (pkg_query_yes_no() == 0)
1199 if (bootstrap_pkg(force, fetchOpts) != 0)
1205 } else if (bootstrap_only) {
1206 printf("pkg already bootstrapped at %s\n", pkgpath);
1210 execv(pkgpath, argv);
1213 return (EXIT_FAILURE);