pkg.c revision 257328
1/*- 2 * Copyright (c) 2012-2013 Baptiste Daroussin <bapt@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: stable/10/usr.sbin/pkg/pkg.c 257328 2013-10-29 12:25:22Z bdrewery $"); 29 30#include <sys/param.h> 31#include <sys/wait.h> 32 33#include <archive.h> 34#include <archive_entry.h> 35#include <err.h> 36#include <errno.h> 37#include <fcntl.h> 38#include <fetch.h> 39#include <paths.h> 40#include <stdbool.h> 41#include <stdlib.h> 42#include <stdio.h> 43#include <string.h> 44#include <time.h> 45#include <unistd.h> 46 47#include "dns_utils.h" 48#include "config.h" 49 50static int 51extract_pkg_static(int fd, char *p, int sz) 52{ 53 struct archive *a; 54 struct archive_entry *ae; 55 char *end; 56 int ret, r; 57 58 ret = -1; 59 a = archive_read_new(); 60 if (a == NULL) { 61 warn("archive_read_new"); 62 return (ret); 63 } 64 archive_read_support_filter_all(a); 65 archive_read_support_format_tar(a); 66 67 if (lseek(fd, 0, 0) == -1) { 68 warn("lseek"); 69 goto cleanup; 70 } 71 72 if (archive_read_open_fd(a, fd, 4096) != ARCHIVE_OK) { 73 warnx("archive_read_open_fd: %s", archive_error_string(a)); 74 goto cleanup; 75 } 76 77 ae = NULL; 78 while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) { 79 end = strrchr(archive_entry_pathname(ae), '/'); 80 if (end == NULL) 81 continue; 82 83 if (strcmp(end, "/pkg-static") == 0) { 84 r = archive_read_extract(a, ae, 85 ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM | 86 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL | 87 ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_XATTR); 88 strlcpy(p, archive_entry_pathname(ae), sz); 89 break; 90 } 91 } 92 93 if (r == ARCHIVE_OK) 94 ret = 0; 95 else 96 warnx("fail to extract pkg-static"); 97 98cleanup: 99 archive_read_free(a); 100 return (ret); 101 102} 103 104static int 105install_pkg_static(char *path, char *pkgpath) 106{ 107 int pstat; 108 pid_t pid; 109 110 switch ((pid = fork())) { 111 case -1: 112 return (-1); 113 case 0: 114 execl(path, "pkg-static", "add", pkgpath, (char *)NULL); 115 _exit(1); 116 default: 117 break; 118 } 119 120 while (waitpid(pid, &pstat, 0) == -1) 121 if (errno != EINTR) 122 return (-1); 123 124 if (WEXITSTATUS(pstat)) 125 return (WEXITSTATUS(pstat)); 126 else if (WIFSIGNALED(pstat)) 127 return (128 & (WTERMSIG(pstat))); 128 return (pstat); 129} 130 131static int 132bootstrap_pkg(void) 133{ 134 struct url *u; 135 FILE *remote; 136 FILE *config; 137 char *site; 138 struct dns_srvinfo *mirrors, *current; 139 /* To store _https._tcp. + hostname + \0 */ 140 char zone[MAXHOSTNAMELEN + 13]; 141 char url[MAXPATHLEN]; 142 char conf[MAXPATHLEN]; 143 char tmppkg[MAXPATHLEN]; 144 const char *packagesite, *mirror_type; 145 char buf[10240]; 146 char pkgstatic[MAXPATHLEN]; 147 int fd, retry, ret, max_retry; 148 struct url_stat st; 149 off_t done, r; 150 time_t now; 151 time_t last; 152 153 done = 0; 154 last = 0; 155 max_retry = 3; 156 ret = -1; 157 remote = NULL; 158 config = NULL; 159 current = mirrors = NULL; 160 161 printf("Bootstrapping pkg please wait\n"); 162 163 if (config_string(PACKAGESITE, &packagesite) != 0) { 164 warnx("No PACKAGESITE defined"); 165 return (-1); 166 } 167 if (config_string(MIRROR_TYPE, &mirror_type) != 0) { 168 warnx("No MIRROR_TYPE defined"); 169 return (-1); 170 } 171 172 /* Support pkg+http:// for PACKAGESITE which is the new format 173 in 1.2 to avoid confusion on why http://pkg.FreeBSD.org has 174 no A record. */ 175 if (strncmp(URL_SCHEME_PREFIX, packagesite, 176 strlen(URL_SCHEME_PREFIX)) == 0) 177 packagesite += strlen(URL_SCHEME_PREFIX); 178 snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite); 179 180 snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX", 181 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP); 182 183 if ((fd = mkstemp(tmppkg)) == -1) { 184 warn("mkstemp()"); 185 return (-1); 186 } 187 188 retry = max_retry; 189 190 u = fetchParseURL(url); 191 while (remote == NULL) { 192 if (retry == max_retry) { 193 if (strcmp(u->scheme, "file") != 0 && 194 strcasecmp(mirror_type, "srv") == 0) { 195 snprintf(zone, sizeof(zone), 196 "_%s._tcp.%s", u->scheme, u->host); 197 mirrors = dns_getsrvinfo(zone); 198 current = mirrors; 199 } 200 } 201 202 if (mirrors != NULL) { 203 strlcpy(u->host, current->host, sizeof(u->host)); 204 u->port = current->port; 205 } 206 207 remote = fetchXGet(u, &st, ""); 208 if (remote == NULL) { 209 --retry; 210 if (retry <= 0) 211 goto fetchfail; 212 if (mirrors == NULL) { 213 sleep(1); 214 } else { 215 current = current->next; 216 if (current == NULL) 217 current = mirrors; 218 } 219 } 220 } 221 222 if (remote == NULL) 223 goto fetchfail; 224 225 while (done < st.size) { 226 if ((r = fread(buf, 1, sizeof(buf), remote)) < 1) 227 break; 228 229 if (write(fd, buf, r) != r) { 230 warn("write()"); 231 goto cleanup; 232 } 233 234 done += r; 235 now = time(NULL); 236 if (now > last || done == st.size) 237 last = now; 238 } 239 240 if (ferror(remote)) 241 goto fetchfail; 242 243 if ((ret = extract_pkg_static(fd, pkgstatic, MAXPATHLEN)) == 0) 244 ret = install_pkg_static(pkgstatic, tmppkg); 245 246 snprintf(conf, MAXPATHLEN, "%s/etc/pkg.conf", 247 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE); 248 249 if (access(conf, R_OK) == -1) { 250 site = strrchr(url, '/'); 251 if (site == NULL) 252 goto cleanup; 253 site[0] = '\0'; 254 site = strrchr(url, '/'); 255 if (site == NULL) 256 goto cleanup; 257 site[0] = '\0'; 258 259 config = fopen(conf, "w+"); 260 if (config == NULL) 261 goto cleanup; 262 fprintf(config, "packagesite: %s\n", url); 263 fclose(config); 264 } 265 266 goto cleanup; 267 268fetchfail: 269 warnx("Error fetching %s: %s", url, fetchLastErrString); 270 fprintf(stderr, "A pre-built version of pkg could not be found for your system.\n"); 271 fprintf(stderr, "Consider changing PACKAGESITE or installing it from ports: 'ports-mgmt/pkg'.\n"); 272 273cleanup: 274 if (remote != NULL) 275 fclose(remote); 276 close(fd); 277 unlink(tmppkg); 278 279 return (ret); 280} 281 282static const char confirmation_message[] = 283"The package management tool is not yet installed on your system.\n" 284"Do you want to fetch and install it now? [y/N]: "; 285 286static int 287pkg_query_yes_no(void) 288{ 289 int ret, c; 290 291 c = getchar(); 292 293 if (c == 'y' || c == 'Y') 294 ret = 1; 295 else 296 ret = 0; 297 298 while (c != '\n' && c != EOF) 299 c = getchar(); 300 301 return (ret); 302} 303 304int 305main(__unused int argc, char *argv[]) 306{ 307 char pkgpath[MAXPATHLEN]; 308 char pkgstatic[MAXPATHLEN]; 309 bool yes = false; 310 int fd, ret; 311 312 snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", 313 getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE); 314 315 if (access(pkgpath, X_OK) == -1) { 316 /* 317 * To allow 'pkg -N' to be used as a reliable test for whether 318 * a system is configured to use pkg, don't bootstrap pkg 319 * when that argument is given as argv[1]. 320 */ 321 if (argv[1] != NULL && strcmp(argv[1], "-N") == 0) 322 errx(EXIT_FAILURE, "pkg is not installed"); 323 324 if (argc > 2 && strcmp(argv[1], "add") == 0 && 325 access(argv[2], R_OK) == 0) { 326 fd = open(argv[2], O_RDONLY); 327 if (fd == -1) 328 err(EXIT_FAILURE, "Unable to open %s", argv[2]); 329 330 if ((ret = extract_pkg_static(fd, pkgstatic, MAXPATHLEN)) == 0) 331 ret = install_pkg_static(pkgstatic, argv[2]); 332 close(fd); 333 if (ret != 0) 334 exit(EXIT_FAILURE); 335 exit(EXIT_SUCCESS); 336 } 337 /* 338 * Do not ask for confirmation if either of stdin or stdout is 339 * not tty. Check the environment to see if user has answer 340 * tucked in there already. 341 */ 342 config_init(); 343 config_bool(ASSUME_ALWAYS_YES, &yes); 344 if (!yes) { 345 printf("%s", confirmation_message); 346 if (!isatty(fileno(stdin))) 347 exit(EXIT_FAILURE); 348 349 if (pkg_query_yes_no() == 0) 350 exit(EXIT_FAILURE); 351 } 352 if (bootstrap_pkg() != 0) 353 exit(EXIT_FAILURE); 354 config_finish(); 355 } 356 357 execv(pkgpath, argv); 358 359 /* NOT REACHED */ 360 return (EXIT_FAILURE); 361} 362