2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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"
83 STAILQ_ENTRY(fingerprint) next;
86 static const char *bootstrap_names [] = {
92 STAILQ_HEAD(fingerprint_list, fingerprint);
97 extract_pkg_static(int fd, char *p, int sz)
100 struct archive_entry *ae;
105 a = archive_read_new();
107 warn("archive_read_new");
110 archive_read_support_filter_all(a);
111 archive_read_support_format_tar(a);
113 if (lseek(fd, 0, 0) == -1) {
118 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) {
119 warnx("archive_read_open_fd: %s", archive_error_string(a));
124 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) {
125 end = strrchr(archive_entry_pathname(ae), '/');
129 if (strcmp(end, "/pkg-static") == 0) {
130 r = archive_read_extract(a, ae,
131 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM |
132 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL |
133 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR);
134 strlcpy(p, archive_entry_pathname(ae), sz);
142 warnx("failed to extract pkg-static: %s",
143 archive_error_string(a));
146 archive_read_free(a);
152 install_pkg_static(const char *path, const char *pkgpath, bool force)
157 switch ((pid = fork())) {
162 execl(path, "pkg-static", "add", "-f", pkgpath,
165 execl(path, "pkg-static", "add", pkgpath,
172 while (waitpid(pid, &pstat, 0) == -1)
176 if (WEXITSTATUS(pstat))
177 return (WEXITSTATUS(pstat));
178 else if (WIFSIGNALED(pstat))
179 return (128 & (WTERMSIG(pstat)));
184 fetch_to_fd(const char *url, char *path, const char *fetchOpts)
187 struct dns_srvinfo *mirrors, *current;
190 /* To store _https._tcp. + hostname + \0 */
192 int retry, max_retry;
195 char zone[MAXHOSTNAMELEN + 13];
196 static const char *mirror_type = NULL;
199 current = mirrors = NULL;
202 if (mirror_type == NULL && config_string(MIRROR_TYPE, &mirror_type)
204 warnx("No MIRROR_TYPE defined");
208 if ((fd = mkstemp(path)) == -1) {
215 if ((u = fetchParseURL(url)) == NULL) {
216 warn("fetchParseURL('%s')", url);
220 while (remote == NULL) {
221 if (retry == max_retry) {
222 if (strcmp(u->scheme, "file") != 0 &&
223 strcasecmp(mirror_type, "srv") == 0) {
224 snprintf(zone, sizeof(zone),
225 "_%s._tcp.%s", u->scheme, u->host);
226 mirrors = dns_getsrvinfo(zone);
231 if (mirrors != NULL) {
232 strlcpy(u->host, current->host, sizeof(u->host));
233 u->port = current->port;
236 remote = fetchXGet(u, &st, fetchOpts);
237 if (remote == NULL) {
241 if (mirrors != NULL) {
242 current = current->next;
249 while ((r = fread(buf, 1, sizeof(buf), remote)) > 0) {
250 if (write(fd, buf, r) != r) {
257 warn("An error occurred while fetching pkg(8)");
280 static struct fingerprint *
281 parse_fingerprint(ucl_object_t *obj)
283 const ucl_object_t *cur;
284 ucl_object_iter_t it = NULL;
285 const char *function, *fp, *key;
286 struct fingerprint *f;
287 hash_t fct = HASH_UNKNOWN;
289 function = fp = NULL;
291 while ((cur = ucl_iterate_object(obj, &it, true))) {
292 key = ucl_object_key(cur);
293 if (cur->type != UCL_STRING)
295 if (strcasecmp(key, "function") == 0) {
296 function = ucl_object_tostring(cur);
299 if (strcasecmp(key, "fingerprint") == 0) {
300 fp = ucl_object_tostring(cur);
305 if (fp == NULL || function == NULL)
308 if (strcasecmp(function, "sha256") == 0)
311 if (fct == HASH_UNKNOWN) {
312 warnx("Unsupported hashing function: %s", function);
316 f = calloc(1, sizeof(struct fingerprint));
318 strlcpy(f->hash, fp, sizeof(f->hash));
324 free_fingerprint_list(struct fingerprint_list* list)
326 struct fingerprint *fingerprint, *tmp;
328 STAILQ_FOREACH_SAFE(fingerprint, list, next, tmp) {
329 free(fingerprint->name);
335 static struct fingerprint *
336 load_fingerprint(const char *dir, const char *filename)
338 ucl_object_t *obj = NULL;
339 struct ucl_parser *p = NULL;
340 struct fingerprint *f;
341 char path[MAXPATHLEN];
345 snprintf(path, MAXPATHLEN, "%s/%s", dir, filename);
347 p = ucl_parser_new(0);
348 if (!ucl_parser_add_file(p, path)) {
349 warnx("%s: %s", path, ucl_parser_get_error(p));
354 obj = ucl_parser_get_object(p);
356 if (obj->type == UCL_OBJECT)
357 f = parse_fingerprint(obj);
360 f->name = strdup(filename);
362 ucl_object_unref(obj);
368 static struct fingerprint_list *
369 load_fingerprints(const char *path, int *count)
373 struct fingerprint *finger;
374 struct fingerprint_list *fingerprints;
378 fingerprints = calloc(1, sizeof(struct fingerprint_list));
379 if (fingerprints == NULL)
381 STAILQ_INIT(fingerprints);
383 if ((d = opendir(path)) == NULL) {
389 while ((ent = readdir(d))) {
390 if (strcmp(ent->d_name, ".") == 0 ||
391 strcmp(ent->d_name, "..") == 0)
393 finger = load_fingerprint(path, ent->d_name);
394 if (finger != NULL) {
395 STAILQ_INSERT_TAIL(fingerprints, finger, next);
402 return (fingerprints);
406 sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH],
407 char out[SHA256_DIGEST_LENGTH * 2 + 1])
411 for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
412 sprintf(out + (i * 2), "%02x", hash[i]);
414 out[SHA256_DIGEST_LENGTH * 2] = '\0';
418 sha256_buf(char *buf, size_t len, char out[SHA256_DIGEST_LENGTH * 2 + 1])
420 unsigned char hash[SHA256_DIGEST_LENGTH];
425 SHA256_Init(&sha256);
426 SHA256_Update(&sha256, buf, len);
427 SHA256_Final(hash, &sha256);
428 sha256_hash(hash, out);
432 sha256_fd(int fd, char out[SHA256_DIGEST_LENGTH * 2 + 1])
437 unsigned char hash[SHA256_DIGEST_LENGTH];
447 /* Duplicate the fd so that fclose(3) does not close it. */
448 if ((my_fd = dup(fd)) == -1) {
453 if ((fp = fdopen(my_fd, "rb")) == NULL) {
458 SHA256_Init(&sha256);
460 while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0)
461 SHA256_Update(&sha256, buffer, r);
463 if (ferror(fp) != 0) {
468 SHA256_Final(hash, &sha256);
469 sha256_hash(hash, out);
475 else if (my_fd != -1)
477 (void)lseek(fd, 0, SEEK_SET);
483 load_public_key_file(const char *file)
489 bp = BIO_new_file(file, "r");
491 errx(EXIT_FAILURE, "Unable to read %s", file);
493 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
494 warnx("ici: %s", ERR_error_string(ERR_get_error(), errbuf));
502 load_public_key_buf(const unsigned char *cert, int certlen)
508 bp = BIO_new_mem_buf(__DECONST(void *, cert), certlen);
510 if ((pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL)) == NULL)
511 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
519 rsa_verify_cert(int fd, const char *sigfile, const unsigned char *key,
520 int keylen, unsigned char *sig, int siglen)
524 char sha256[(SHA256_DIGEST_LENGTH * 2) + 2];
532 SSL_load_error_strings();
534 /* Compute SHA256 of the package. */
535 if (lseek(fd, 0, 0) == -1) {
539 if ((sha256_fd(fd, sha256)) == -1) {
540 warnx("Error creating SHA256 hash for package");
544 if (sigfile != NULL) {
545 if ((pkey = load_public_key_file(sigfile)) == NULL) {
546 warnx("Error reading public key");
550 if ((pkey = load_public_key_buf(key, keylen)) == NULL) {
551 warnx("Error reading public key");
556 /* Verify signature of the SHA256(pkg) is valid. */
557 if ((mdctx = EVP_MD_CTX_create()) == NULL) {
558 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
562 if (EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey) != 1) {
563 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
566 if (EVP_DigestVerifyUpdate(mdctx, sha256, strlen(sha256)) != 1) {
567 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
571 if (EVP_DigestVerifyFinal(mdctx, sig, siglen) != 1) {
572 warnx("%s", ERR_error_string(ERR_get_error(), errbuf));
587 EVP_MD_CTX_destroy(mdctx);
593 static struct pubkey *
603 if (lseek(fd, 0, 0) == -1) {
610 sig = open_memstream(&sigb, &sigsz);
612 err(EXIT_FAILURE, "open_memstream()");
614 while ((r = read(fd, buf, sizeof(buf))) >0) {
615 fwrite(buf, 1, r, sig);
619 pk = calloc(1, sizeof(struct pubkey));
621 pk->sig = calloc(1, pk->siglen);
622 memcpy(pk->sig, sigb, pk->siglen);
628 static struct sig_cert *
632 FILE *fp, *sigfp, *certfp, *tmpfp;
635 size_t linecap, sigsz, certsz;
642 sigfp = certfp = tmpfp = NULL;
644 if (lseek(fd, 0, 0) == -1) {
649 /* Duplicate the fd so that fclose(3) does not close it. */
650 if ((my_fd = dup(fd)) == -1) {
655 if ((fp = fdopen(my_fd, "rb")) == NULL) {
662 sigfp = open_memstream(&sig, &sigsz);
664 err(EXIT_FAILURE, "open_memstream()");
665 certfp = open_memstream(&cert, &certsz);
667 err(EXIT_FAILURE, "open_memstream()");
669 while ((linelen = getline(&line, &linecap, fp)) > 0) {
670 if (strcmp(line, "SIGNATURE\n") == 0) {
673 } else if (strcmp(line, "CERT\n") == 0) {
676 } else if (strcmp(line, "END\n") == 0) {
680 fwrite(line, 1, linelen, tmpfp);
687 sc = calloc(1, sizeof(struct sig_cert));
688 sc->siglen = sigsz -1; /* Trim out unrelated trailing newline */
691 sc->certlen = certsz;
698 verify_pubsignature(int fd_pkg, int fd_sig)
707 if (config_string(PUBKEY, &pubkey) != 0) {
708 warnx("No CONFIG_PUBKEY defined");
712 if ((pk = read_pubkey(fd_sig)) == NULL) {
713 warnx("Error reading signature");
717 /* Verify the signature. */
718 printf("Verifying signature with public key %s... ", pubkey);
719 if (rsa_verify_cert(fd_pkg, pubkey, NULL, 0, pk->sig,
720 pk->siglen) == false) {
721 fprintf(stderr, "Signature is not valid\n");
737 verify_signature(int fd_pkg, int fd_sig)
739 struct fingerprint_list *trusted, *revoked;
740 struct fingerprint *fingerprint;
743 int trusted_count, revoked_count;
744 const char *fingerprints;
745 char path[MAXPATHLEN];
746 char hash[SHA256_DIGEST_LENGTH * 2 + 1];
749 trusted = revoked = NULL;
752 /* Read and parse fingerprints. */
753 if (config_string(FINGERPRINTS, &fingerprints) != 0) {
754 warnx("No CONFIG_FINGERPRINTS defined");
758 snprintf(path, MAXPATHLEN, "%s/trusted", fingerprints);
759 if ((trusted = load_fingerprints(path, &trusted_count)) == NULL) {
760 warnx("Error loading trusted certificates");
764 if (trusted_count == 0 || trusted == NULL) {
765 fprintf(stderr, "No trusted certificates found.\n");
769 snprintf(path, MAXPATHLEN, "%s/revoked", fingerprints);
770 if ((revoked = load_fingerprints(path, &revoked_count)) == NULL) {
771 warnx("Error loading revoked certificates");
775 /* Read certificate and signature in. */
776 if ((sc = parse_cert(fd_sig)) == NULL) {
777 warnx("Error parsing certificate");
780 /* Explicitly mark as non-trusted until proven otherwise. */
783 /* Parse signature and pubkey out of the certificate */
784 sha256_buf(sc->cert, sc->certlen, hash);
786 /* Check if this hash is revoked */
787 if (revoked != NULL) {
788 STAILQ_FOREACH(fingerprint, revoked, next) {
789 if (strcasecmp(fingerprint->hash, hash) == 0) {
790 fprintf(stderr, "The package was signed with "
791 "revoked certificate %s\n",
798 STAILQ_FOREACH(fingerprint, trusted, next) {
799 if (strcasecmp(fingerprint->hash, hash) == 0) {
801 sc->name = strdup(fingerprint->name);
806 if (sc->trusted == false) {
807 fprintf(stderr, "No trusted fingerprint found matching "
808 "package's certificate\n");
812 /* Verify the signature. */
813 printf("Verifying signature with trusted certificate %s... ", sc->name);
814 if (rsa_verify_cert(fd_pkg, NULL, sc->cert, sc->certlen, sc->sig,
815 sc->siglen) == false) {
816 fprintf(stderr, "Signature is not valid\n");
824 free_fingerprint_list(trusted);
826 free_fingerprint_list(revoked);
838 bootstrap_pkg(bool force, const char *fetchOpts)
842 char url[MAXPATHLEN];
843 char tmppkg[MAXPATHLEN];
844 char tmpsig[MAXPATHLEN];
845 const char *packagesite;
846 const char *signature_type;
847 char pkgstatic[MAXPATHLEN];
848 const char *bootstrap_name;
853 if (config_string(PACKAGESITE, &packagesite) != 0) {
854 warnx("No PACKAGESITE defined");
858 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
859 warnx("Error looking up SIGNATURE_TYPE");
863 printf("Bootstrapping pkg from %s, please wait...\n", packagesite);
865 /* Support pkg+http:// for PACKAGESITE which is the new format
866 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has
868 if (strncmp(URL_SCHEME_PREFIX, packagesite,
869 strlen(URL_SCHEME_PREFIX)) == 0)
870 packagesite += strlen(URL_SCHEME_PREFIX);
871 for (int j = 0; bootstrap_names[j] != NULL; j++) {
872 bootstrap_name = bootstrap_names[j];
874 snprintf(url, MAXPATHLEN, "%s/Latest/%s", packagesite, bootstrap_name);
875 snprintf(tmppkg, MAXPATHLEN, "%s/%s.XXXXXX",
876 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP,
878 if ((fd_pkg = fetch_to_fd(url, tmppkg, fetchOpts)) != -1)
880 bootstrap_name = NULL;
882 if (bootstrap_name == NULL)
885 if (signature_type != NULL &&
886 strcasecmp(signature_type, "NONE") != 0) {
887 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
889 snprintf(tmpsig, MAXPATHLEN, "%s/%s.sig.XXXXXX",
890 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP,
892 snprintf(url, MAXPATHLEN, "%s/Latest/%s.sig",
893 packagesite, bootstrap_name);
895 if ((fd_sig = fetch_to_fd(url, tmpsig, fetchOpts)) == -1) {
896 fprintf(stderr, "Signature for pkg not "
901 if (verify_signature(fd_pkg, fd_sig) == false)
903 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
905 snprintf(tmpsig, MAXPATHLEN,
906 "%s/%s.pubkeysig.XXXXXX",
907 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP,
909 snprintf(url, MAXPATHLEN, "%s/Latest/%s.pubkeysig",
910 packagesite, bootstrap_name);
912 if ((fd_sig = fetch_to_fd(url, tmpsig, fetchOpts)) == -1) {
913 fprintf(stderr, "Signature for pkg not "
918 if (verify_pubsignature(fd_pkg, fd_sig) == false)
921 warnx("Signature type %s is not supported for "
922 "bootstrapping.", signature_type);
927 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
928 ret = install_pkg_static(pkgstatic, tmppkg, force);
933 warnx("Error fetching %s: %s", url, fetchLastErrString);
934 if (fetchLastErrCode == FETCH_RESOLV) {
935 fprintf(stderr, "Address resolution failed for %s.\n", packagesite);
936 fprintf(stderr, "Consider changing PACKAGESITE.\n");
938 fprintf(stderr, "A pre-built version of pkg could not be found for "
940 fprintf(stderr, "Consider changing PACKAGESITE or installing it from "
941 "ports: 'ports-mgmt/pkg'.\n");
958 static const char confirmation_message[] =
959 "The package management tool is not yet installed on your system.\n"
960 "Do you want to fetch and install it now? [y/N]: ";
962 static const char non_interactive_message[] =
963 "The package management tool is not yet installed on your system.\n"
964 "Please set ASSUME_ALWAYS_YES=yes environment variable to be able to bootstrap "
965 "in non-interactive (stdin not being a tty)\n";
967 static const char args_bootstrap_message[] =
968 "Too many arguments\n"
969 "Usage: pkg [-4|-6] bootstrap [-f] [-y]\n";
971 static const char args_add_message[] =
972 "Too many arguments\n"
973 "Usage: pkg add [-f] [-y] {pkg.txz}\n";
976 pkg_query_yes_no(void)
983 if (c == 'y' || c == 'Y')
988 while (c != '\n' && c != EOF)
995 bootstrap_pkg_local(const char *pkgpath, bool force)
997 char path[MAXPATHLEN];
998 char pkgstatic[MAXPATHLEN];
999 const char *signature_type;
1000 int fd_pkg, fd_sig, ret;
1005 fd_pkg = open(pkgpath, O_RDONLY);
1007 err(EXIT_FAILURE, "Unable to open %s", pkgpath);
1009 if (config_string(SIGNATURE_TYPE, &signature_type) != 0) {
1010 warnx("Error looking up SIGNATURE_TYPE");
1013 if (signature_type != NULL &&
1014 strcasecmp(signature_type, "NONE") != 0) {
1015 if (strcasecmp(signature_type, "FINGERPRINTS") == 0) {
1017 snprintf(path, sizeof(path), "%s.sig", pkgpath);
1019 if ((fd_sig = open(path, O_RDONLY)) == -1) {
1020 fprintf(stderr, "Signature for pkg not "
1025 if (verify_signature(fd_pkg, fd_sig) == false)
1028 } else if (strcasecmp(signature_type, "PUBKEY") == 0) {
1030 snprintf(path, sizeof(path), "%s.pubkeysig", pkgpath);
1032 if ((fd_sig = open(path, O_RDONLY)) == -1) {
1033 fprintf(stderr, "Signature for pkg not "
1038 if (verify_pubsignature(fd_pkg, fd_sig) == false)
1042 warnx("Signature type %s is not supported for "
1043 "bootstrapping.", signature_type);
1048 if ((ret = extract_pkg_static(fd_pkg, pkgstatic, MAXPATHLEN)) == 0)
1049 ret = install_pkg_static(pkgstatic, pkgpath, force);
1059 #define PKG_NAME "pkg"
1060 #define PKG_DEVEL_NAME PKG_NAME "-devel"
1061 #define PKG_PKG PKG_NAME "."
1064 pkg_is_pkg_pkg(const char *pkg)
1066 char *vstart, *basename;
1070 if ((basename = strrchr(pkg, '/')) != NULL)
1074 * Chop off the final "-" (version delimiter) and check the name that
1075 * precedes it. If we didn't have a version delimiter, it must be the
1076 * pkg.$archive short form but we'll check it anyways. pkg-devel short
1077 * form will look like a pkg archive with 'devel' version, but that's
1078 * OK. We otherwise assumed that non-pkg packages will always have a
1079 * version component.
1081 vstart = strrchr(pkg, '-');
1082 if (vstart == NULL) {
1083 return (strlen(pkg) > sizeof(PKG_PKG) - 1 &&
1084 strncmp(pkg, PKG_PKG, sizeof(PKG_PKG) - 1) == 0);
1087 namelen = vstart - pkg;
1088 if (namelen == sizeof(PKG_NAME) - 1 &&
1089 strncmp(pkg, PKG_NAME, sizeof(PKG_NAME) - 1) == 0)
1091 if (namelen == sizeof(PKG_DEVEL_NAME) - 1 &&
1092 strncmp(pkg, PKG_DEVEL_NAME, sizeof(PKG_DEVEL_NAME) - 1) == 0)
1098 main(int argc, char *argv[])
1100 char pkgpath[MAXPATHLEN];
1101 const char *pkgarg, *repo_name;
1102 bool activation_test, add_pkg, bootstrap_only, force, yes;
1104 const char *fetchOpts;
1107 activation_test = false;
1109 bootstrap_only = false;
1117 struct option longopts[] = {
1118 { "debug", no_argument, NULL, 'd' },
1119 { "force", no_argument, NULL, 'f' },
1120 { "only-ipv4", no_argument, NULL, '4' },
1121 { "only-ipv6", no_argument, NULL, '6' },
1122 { "yes", no_argument, NULL, 'y' },
1123 { NULL, 0, NULL, 0 },
1126 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", getlocalbase());
1128 while ((ch = getopt_long(argc, argv, "-:dfr::yN46", longopts, NULL)) != -1) {
1137 activation_test = true;
1150 * The repository can only be specified for an explicit
1151 * bootstrap request at this time, so that we don't
1152 * confuse the user if they're trying to use a verb that
1153 * has some other conflicting meaning but we need to
1156 * For that reason, we specify that -r has an optional
1157 * argument above and process the next index ourselves.
1158 * This is mostly significant because getopt(3) will
1159 * otherwise eat the next argument, which could be
1160 * something we need to try and make sense of.
1162 * At worst this gets us false positives that we ignore
1163 * in other contexts, and we have to do a little fudging
1164 * in order to support separating -r from the reponame
1165 * with a space since it's not actually optional in
1166 * the bootstrap/add sense.
1168 if (add_pkg || bootstrap_only) {
1169 if (optarg != NULL) {
1171 } else if (optind < argc) {
1172 repo_name = argv[optind];
1175 if (repo_name == NULL || *repo_name == '\0') {
1177 "Must specify a repository with -r!\n");
1181 if (optarg == NULL) {
1182 /* Advance past repo name. */
1189 // Non-option arguments, first one is the command
1190 if (command == NULL) {
1191 command = argv[optind-1];
1192 if (strcmp(command, "add") == 0) {
1195 else if (strcmp(command, "bootstrap") == 0) {
1196 bootstrap_only = true;
1199 // bootstrap doesn't accept other arguments
1200 else if (bootstrap_only) {
1201 fprintf(stderr, args_bootstrap_message);
1204 else if (add_pkg && pkgarg != NULL) {
1206 * Additional arguments also means it's not a
1207 * local bootstrap request.
1213 * If it's not a request for pkg or pkg-devel,
1214 * then we must assume they were trying to
1215 * install some other local package and we
1216 * should try to bootstrap from the repo.
1218 if (!pkg_is_pkg_pkg(argv[optind-1])) {
1221 pkgarg = argv[optind-1];
1232 if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) {
1234 * To allow 'pkg -N' to be used as a reliable test for whether
1235 * a system is configured to use pkg, don't bootstrap pkg
1236 * when that option is passed.
1238 if (activation_test)
1239 errx(EXIT_FAILURE, "pkg is not installed");
1241 config_init(repo_name);
1244 if (pkgarg == NULL) {
1245 fprintf(stderr, "Path to pkg.txz required\n");
1248 if (access(pkgarg, R_OK) == -1) {
1249 fprintf(stderr, "No such file: %s\n", pkgarg);
1252 if (bootstrap_pkg_local(pkgarg, force) != 0)
1257 * Do not ask for confirmation if either of stdin or stdout is
1258 * not tty. Check the environment to see if user has answer
1259 * tucked in there already.
1262 config_bool(ASSUME_ALWAYS_YES, &yes);
1264 if (!isatty(fileno(stdin))) {
1265 fprintf(stderr, non_interactive_message);
1269 printf("%s", confirmation_message);
1270 if (pkg_query_yes_no() == 0)
1273 if (bootstrap_pkg(force, fetchOpts) != 0)
1279 } else if (bootstrap_only) {
1280 printf("pkg already bootstrapped at %s\n", pkgpath);
1284 execv(pkgpath, argv);
1287 return (EXIT_FAILURE);