main.c revision 267654
1/* 2 * 3 * FreeBSD install - a package for the installation and maintainance 4 * of non-core utilities. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 15 * Jordan K. Hubbard 16 * 18 July 1993 17 * 18 * This is the add module. 19 */ 20 21#include <sys/cdefs.h> 22__FBSDID("$FreeBSD: releng/9.3/usr.sbin/pkg_install/add/main.c 254528 2013-08-19 14:38:28Z gjb $"); 23 24#include <sys/param.h> 25#include <sys/sysctl.h> 26#include <err.h> 27#include <getopt.h> 28 29#include "lib.h" 30#include "add.h" 31 32char *Prefix = NULL; 33Boolean PrefixRecursive = FALSE; 34char *Chroot = NULL; 35Boolean NoInstall = FALSE; 36Boolean NoRecord = FALSE; 37Boolean Remote = FALSE; 38Boolean KeepPackage = FALSE; 39Boolean FailOnAlreadyInstalled = TRUE; 40Boolean IgnoreDeps = FALSE; 41 42char *Mode = NULL; 43char *Owner = NULL; 44char *Group = NULL; 45char *PkgName = NULL; 46char *PkgAddCmd = NULL; 47char *Directory = NULL; 48char FirstPen[FILENAME_MAX]; 49add_mode_t AddMode = NORMAL; 50 51char **pkgs; 52 53struct { 54 int lowver; /* Lowest version number to match */ 55 int hiver; /* Highest version number to match */ 56 const char *directory; /* Directory it lives in */ 57} releases[] = { 58 { 410000, 410000, "/packages-4.1-release" }, 59 { 420000, 420000, "/packages-4.2-release" }, 60 { 430000, 430000, "/packages-4.3-release" }, 61 { 440000, 440000, "/packages-4.4-release" }, 62 { 450000, 450000, "/packages-4.5-release" }, 63 { 460000, 460001, "/packages-4.6-release" }, 64 { 460002, 460099, "/packages-4.6.2-release" }, 65 { 470000, 470099, "/packages-4.7-release" }, 66 { 480000, 480099, "/packages-4.8-release" }, 67 { 490000, 490099, "/packages-4.9-release" }, 68 { 491000, 491099, "/packages-4.10-release" }, 69 { 492000, 492099, "/packages-4.11-release" }, 70 { 500000, 500099, "/packages-5.0-release" }, 71 { 501000, 501099, "/packages-5.1-release" }, 72 { 502000, 502009, "/packages-5.2-release" }, 73 { 502010, 502099, "/packages-5.2.1-release" }, 74 { 503000, 503099, "/packages-5.3-release" }, 75 { 504000, 504099, "/packages-5.4-release" }, 76 { 505000, 505099, "/packages-5.5-release" }, 77 { 600000, 600099, "/packages-6.0-release" }, 78 { 601000, 601099, "/packages-6.1-release" }, 79 { 602000, 602099, "/packages-6.2-release" }, 80 { 603000, 603099, "/packages-6.3-release" }, 81 { 604000, 604099, "/packages-6.4-release" }, 82 { 700000, 700099, "/packages-7.0-release" }, 83 { 701000, 701099, "/packages-7.1-release" }, 84 { 702000, 702099, "/packages-7.2-release" }, 85 { 703000, 703099, "/packages-7.3-release" }, 86 { 704000, 704099, "/packages-7.4-release" }, 87 { 800000, 800499, "/packages-8.0-release" }, 88 { 801000, 801499, "/packages-8.1-release" }, 89 { 802000, 802499, "/packages-8.2-release" }, 90 { 803000, 803499, "/packages-8.3-release" }, 91 { 900000, 900499, "/packages-9.0-release" }, 92 { 901000, 901499, "/packages-9.1-release" }, 93 { 902000, 902499, "/packages-9.2-release" }, 94 { 300000, 399000, "/packages-3-stable" }, 95 { 400000, 499000, "/packages-4-stable" }, 96 { 502100, 502128, "/packages-5-current" }, 97 { 503100, 599000, "/packages-5-stable" }, 98 { 600100, 699000, "/packages-6-stable" }, 99 { 700100, 799000, "/packages-7-stable" }, 100 { 800500, 899000, "/packages-8-stable" }, 101 { 900500, 999000, "/packages-9-stable" }, 102 { 1000000, 1099000, "/packages-10-current" }, 103 { 0, 9999999, "/packages-current" }, 104 { 0, 0, NULL } 105}; 106 107static char *getpackagesite(void); 108int getosreldate(void); 109 110static void usage(void); 111 112static char opts[] = "hviIRfFnrp:P:SMt:C:K"; 113static struct option longopts[] = { 114 { "chroot", required_argument, NULL, 'C' }, 115 { "dry-run", no_argument, NULL, 'n' }, 116 { "force", no_argument, NULL, 'f' }, 117 { "help", no_argument, NULL, 'h' }, 118 { "keep", no_argument, NULL, 'K' }, 119 { "master", no_argument, NULL, 'M' }, 120 { "no-deps", no_argument, NULL, 'i' }, 121 { "no-record", no_argument, NULL, 'R' }, 122 { "no-script", no_argument, NULL, 'I' }, 123 { "prefix", required_argument, NULL, 'p' }, 124 { "remote", no_argument, NULL, 'r' }, 125 { "template", required_argument, NULL, 't' }, 126 { "slave", no_argument, NULL, 'S' }, 127 { "verbose", no_argument, NULL, 'v' }, 128 { NULL, 0, NULL, 0 } 129}; 130 131int 132main(int argc, char **argv) 133{ 134 int ch, error; 135 char **start; 136 char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr; 137 static char temppackageroot[MAXPATHLEN]; 138 static char pkgaddpath[MAXPATHLEN]; 139 140 if (*argv[0] != '/' && strchr(argv[0], '/') != NULL) 141 PkgAddCmd = realpath(argv[0], pkgaddpath); 142 else 143 PkgAddCmd = argv[0]; 144 145 start = argv; 146 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) { 147 switch(ch) { 148 case 'v': 149 Verbose++; 150 break; 151 152 case 'p': 153 Prefix = optarg; 154 PrefixRecursive = FALSE; 155 break; 156 157 case 'P': 158 Prefix = optarg; 159 PrefixRecursive = TRUE; 160 break; 161 162 case 'I': 163 NoInstall = TRUE; 164 break; 165 166 case 'R': 167 NoRecord = TRUE; 168 break; 169 170 case 'f': 171 Force = TRUE; 172 break; 173 174 case 'F': 175 FailOnAlreadyInstalled = FALSE; 176 break; 177 178 case 'K': 179 KeepPackage = TRUE; 180 break; 181 182 case 'n': 183 Fake = TRUE; 184 break; 185 186 case 'r': 187 Remote = TRUE; 188 break; 189 190 case 't': 191 if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen)) 192 errx(1, "-t Argument too long."); 193 break; 194 195 case 'S': 196 AddMode = SLAVE; 197 break; 198 199 case 'M': 200 AddMode = MASTER; 201 break; 202 203 case 'C': 204 Chroot = optarg; 205 break; 206 207 case 'i': 208 IgnoreDeps = TRUE; 209 break; 210 211 case 'h': 212 default: 213 usage(); 214 break; 215 } 216 } 217 argc -= optind; 218 argv += optind; 219 220 if (AddMode != SLAVE) { 221 pkgs = (char **)malloc((argc+1) * sizeof(char *)); 222 for (ch = 0; ch <= argc; pkgs[ch++] = NULL) ; 223 224 /* Get all the remaining package names, if any */ 225 for (ch = 0; *argv; ch++, argv++) { 226 char temp[MAXPATHLEN]; 227 if (Remote) { 228 if ((packagesite = getpackagesite()) == NULL) 229 errx(1, "package name too long"); 230 if (strlcpy(temppackageroot, packagesite, 231 sizeof(temppackageroot)) >= sizeof(temppackageroot)) 232 errx(1, "package name too long"); 233 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot)) 234 >= sizeof(temppackageroot)) 235 errx(1, "package name too long"); 236 remotepkg = temppackageroot; 237 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' && 238 (ptr[2] == 'b' || ptr[2] == 'g' || ptr[2] == 'x') && 239 ptr[3] == 'z' && !ptr[4])) { 240 if (getenv("PACKAGESUFFIX")) { 241 if (strlcat(remotepkg, getenv("PACKAGESUFFIX"), 242 sizeof(temppackageroot)) >= sizeof(temppackageroot)) 243 errx(1, "package name too long"); 244 } else { 245 if (strlcat(remotepkg, ".tbz", 246 sizeof(temppackageroot)) >= sizeof(temppackageroot)) 247 errx(1, "package name too long"); 248 } 249 } 250 } 251 if (!strcmp(*argv, "-")) /* stdin? */ 252 pkgs[ch] = (char *)"-"; 253 else if (isURL(*argv)) { /* preserve URLs */ 254 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp)) 255 errx(1, "package name too long"); 256 pkgs[ch] = strdup(temp); 257 } 258 else if ((Remote) && isURL(remotepkg)) { 259 if (strlcpy(temp, remotepkg, sizeof(temp)) >= sizeof(temp)) 260 errx(1, "package name too long"); 261 pkgs[ch] = strdup(temp); 262 } else { /* expand all pathnames to fullnames */ 263 if (fexists(*argv)) /* refers to a file directly */ 264 pkgs[ch] = strdup(realpath(*argv, temp)); 265 else { /* look for the file in the expected places */ 266 if (!(cp = fileFindByPath(NULL, *argv))) { 267 /* let pkg_do() fail later, so that error is reported */ 268 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp)) 269 errx(1, "package name too long"); 270 pkgs[ch] = strdup(temp); 271 } else { 272 if (strlcpy(temp, cp, sizeof(temp)) >= sizeof(temp)) 273 errx(1, "package name too long"); 274 pkgs[ch] = strdup(temp); 275 } 276 } 277 } 278 if (packagesite != NULL) 279 packagesite[0] = '\0'; 280 } 281 } 282 /* If no packages, yelp */ 283 if (!ch) { 284 warnx("missing package name(s)"); 285 usage(); 286 } 287 else if (ch > 1 && AddMode == MASTER) { 288 warnx("only one package name may be specified with master mode"); 289 usage(); 290 } 291 /* Perform chroot if requested */ 292 if (Chroot != NULL) { 293 if (chdir(Chroot)) 294 errx(1, "chdir to %s failed", Chroot); 295 if (chroot(".")) 296 errx(1, "chroot to %s failed", Chroot); 297 } 298 /* Make sure the sub-execs we invoke get found */ 299 setenv("PATH", 300 "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin", 301 1); 302 303 /* Set a reasonable umask */ 304 umask(022); 305 306 if ((error = pkg_perform(pkgs)) != 0) { 307 if (Verbose) 308 warnx("%d package addition(s) failed", error); 309 return error; 310 } 311 else 312 return 0; 313} 314 315static char * 316getpackagesite(void) 317{ 318 int reldate, i; 319 static char sitepath[MAXPATHLEN]; 320 int archmib[] = { CTL_HW, HW_MACHINE_ARCH }; 321 char arch[64]; 322 size_t archlen = sizeof(arch); 323 324 if (getenv("PACKAGESITE")) { 325 if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath)) 326 >= sizeof(sitepath)) 327 return NULL; 328 return sitepath; 329 } 330 331 if (getenv("PACKAGEROOT")) { 332 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath)) 333 >= sizeof(sitepath)) 334 return NULL; 335 } else { 336 if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath)) 337 >= sizeof(sitepath)) 338 return NULL; 339 } 340 341 if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath)) 342 >= sizeof(sitepath)) 343 return NULL; 344 345 if (sysctl(archmib, 2, arch, &archlen, NULL, 0) == -1) 346 return NULL; 347 arch[archlen-1] = 0; 348 if (strlcat(sitepath, arch, sizeof(sitepath)) >= sizeof(sitepath)) 349 return NULL; 350 351 reldate = getosreldate(); 352 for(i = 0; releases[i].directory != NULL; i++) { 353 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) { 354 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath)) 355 >= sizeof(sitepath)) 356 return NULL; 357 break; 358 } 359 } 360 361 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath)) 362 return NULL; 363 364 return sitepath; 365 366} 367 368static void 369usage(void) 370{ 371 fprintf(stderr, "%s\n%s\n", 372 "usage: pkg_add [-viInfFrRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]", 373 " pkg-name [pkg-name ...]"); 374 exit(1); 375} 376