main.c revision 241976
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: stable/9/usr.sbin/pkg_install/add/main.c 241976 2012-10-24 02:00:54Z kensmith $"); 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 { 300000, 399000, "/packages-3-stable" }, 94 { 400000, 499000, "/packages-4-stable" }, 95 { 502100, 502128, "/packages-5-current" }, 96 { 503100, 599000, "/packages-5-stable" }, 97 { 600100, 699000, "/packages-6-stable" }, 98 { 700100, 799000, "/packages-7-stable" }, 99 { 800500, 899000, "/packages-8-stable" }, 100 { 900500, 999000, "/packages-9-stable" }, 101 { 1000000, 1099000, "/packages-10-current" }, 102 { 0, 9999999, "/packages-current" }, 103 { 0, 0, NULL } 104}; 105 106static char *getpackagesite(void); 107int getosreldate(void); 108 109static void usage(void); 110 111static char opts[] = "hviIRfFnrp:P:SMt:C:K"; 112static struct option longopts[] = { 113 { "chroot", required_argument, NULL, 'C' }, 114 { "dry-run", no_argument, NULL, 'n' }, 115 { "force", no_argument, NULL, 'f' }, 116 { "help", no_argument, NULL, 'h' }, 117 { "keep", no_argument, NULL, 'K' }, 118 { "master", no_argument, NULL, 'M' }, 119 { "no-deps", no_argument, NULL, 'i' }, 120 { "no-record", no_argument, NULL, 'R' }, 121 { "no-script", no_argument, NULL, 'I' }, 122 { "prefix", required_argument, NULL, 'p' }, 123 { "remote", no_argument, NULL, 'r' }, 124 { "template", required_argument, NULL, 't' }, 125 { "slave", no_argument, NULL, 'S' }, 126 { "verbose", no_argument, NULL, 'v' }, 127 { NULL, 0, NULL, 0 } 128}; 129 130int 131main(int argc, char **argv) 132{ 133 int ch, error; 134 char **start; 135 char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr; 136 static char temppackageroot[MAXPATHLEN]; 137 static char pkgaddpath[MAXPATHLEN]; 138 139 if (*argv[0] != '/' && strchr(argv[0], '/') != NULL) 140 PkgAddCmd = realpath(argv[0], pkgaddpath); 141 else 142 PkgAddCmd = argv[0]; 143 144 start = argv; 145 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) { 146 switch(ch) { 147 case 'v': 148 Verbose++; 149 break; 150 151 case 'p': 152 Prefix = optarg; 153 PrefixRecursive = FALSE; 154 break; 155 156 case 'P': 157 Prefix = optarg; 158 PrefixRecursive = TRUE; 159 break; 160 161 case 'I': 162 NoInstall = TRUE; 163 break; 164 165 case 'R': 166 NoRecord = TRUE; 167 break; 168 169 case 'f': 170 Force = TRUE; 171 break; 172 173 case 'F': 174 FailOnAlreadyInstalled = FALSE; 175 break; 176 177 case 'K': 178 KeepPackage = TRUE; 179 break; 180 181 case 'n': 182 Fake = TRUE; 183 break; 184 185 case 'r': 186 Remote = TRUE; 187 break; 188 189 case 't': 190 if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen)) 191 errx(1, "-t Argument too long."); 192 break; 193 194 case 'S': 195 AddMode = SLAVE; 196 break; 197 198 case 'M': 199 AddMode = MASTER; 200 break; 201 202 case 'C': 203 Chroot = optarg; 204 break; 205 206 case 'i': 207 IgnoreDeps = TRUE; 208 break; 209 210 case 'h': 211 default: 212 usage(); 213 break; 214 } 215 } 216 argc -= optind; 217 argv += optind; 218 219 if (AddMode != SLAVE) { 220 pkgs = (char **)malloc((argc+1) * sizeof(char *)); 221 for (ch = 0; ch <= argc; pkgs[ch++] = NULL) ; 222 223 /* Get all the remaining package names, if any */ 224 for (ch = 0; *argv; ch++, argv++) { 225 char temp[MAXPATHLEN]; 226 if (Remote) { 227 if ((packagesite = getpackagesite()) == NULL) 228 errx(1, "package name too long"); 229 if (strlcpy(temppackageroot, packagesite, 230 sizeof(temppackageroot)) >= sizeof(temppackageroot)) 231 errx(1, "package name too long"); 232 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot)) 233 >= sizeof(temppackageroot)) 234 errx(1, "package name too long"); 235 remotepkg = temppackageroot; 236 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' && 237 (ptr[2] == 'b' || ptr[2] == 'g' || ptr[2] == 'x') && 238 ptr[3] == 'z' && !ptr[4])) { 239 if (getenv("PACKAGESUFFIX")) { 240 if (strlcat(remotepkg, getenv("PACKAGESUFFIX"), 241 sizeof(temppackageroot)) >= sizeof(temppackageroot)) 242 errx(1, "package name too long"); 243 } else { 244 if (strlcat(remotepkg, ".tbz", 245 sizeof(temppackageroot)) >= sizeof(temppackageroot)) 246 errx(1, "package name too long"); 247 } 248 } 249 } 250 if (!strcmp(*argv, "-")) /* stdin? */ 251 pkgs[ch] = (char *)"-"; 252 else if (isURL(*argv)) { /* preserve URLs */ 253 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp)) 254 errx(1, "package name too long"); 255 pkgs[ch] = strdup(temp); 256 } 257 else if ((Remote) && isURL(remotepkg)) { 258 if (strlcpy(temp, remotepkg, sizeof(temp)) >= sizeof(temp)) 259 errx(1, "package name too long"); 260 pkgs[ch] = strdup(temp); 261 } else { /* expand all pathnames to fullnames */ 262 if (fexists(*argv)) /* refers to a file directly */ 263 pkgs[ch] = strdup(realpath(*argv, temp)); 264 else { /* look for the file in the expected places */ 265 if (!(cp = fileFindByPath(NULL, *argv))) { 266 /* let pkg_do() fail later, so that error is reported */ 267 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp)) 268 errx(1, "package name too long"); 269 pkgs[ch] = strdup(temp); 270 } else { 271 if (strlcpy(temp, cp, sizeof(temp)) >= sizeof(temp)) 272 errx(1, "package name too long"); 273 pkgs[ch] = strdup(temp); 274 } 275 } 276 } 277 if (packagesite != NULL) 278 packagesite[0] = '\0'; 279 } 280 } 281 /* If no packages, yelp */ 282 if (!ch) { 283 warnx("missing package name(s)"); 284 usage(); 285 } 286 else if (ch > 1 && AddMode == MASTER) { 287 warnx("only one package name may be specified with master mode"); 288 usage(); 289 } 290 /* Perform chroot if requested */ 291 if (Chroot != NULL) { 292 if (chdir(Chroot)) 293 errx(1, "chdir to %s failed", Chroot); 294 if (chroot(".")) 295 errx(1, "chroot to %s failed", Chroot); 296 } 297 /* Make sure the sub-execs we invoke get found */ 298 setenv("PATH", 299 "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin", 300 1); 301 302 /* Set a reasonable umask */ 303 umask(022); 304 305 if ((error = pkg_perform(pkgs)) != 0) { 306 if (Verbose) 307 warnx("%d package addition(s) failed", error); 308 return error; 309 } 310 else 311 return 0; 312} 313 314static char * 315getpackagesite(void) 316{ 317 int reldate, i; 318 static char sitepath[MAXPATHLEN]; 319 int archmib[] = { CTL_HW, HW_MACHINE_ARCH }; 320 char arch[64]; 321 size_t archlen = sizeof(arch); 322 323 if (getenv("PACKAGESITE")) { 324 if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath)) 325 >= sizeof(sitepath)) 326 return NULL; 327 return sitepath; 328 } 329 330 if (getenv("PACKAGEROOT")) { 331 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath)) 332 >= sizeof(sitepath)) 333 return NULL; 334 } else { 335 if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath)) 336 >= sizeof(sitepath)) 337 return NULL; 338 } 339 340 if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath)) 341 >= sizeof(sitepath)) 342 return NULL; 343 344 if (sysctl(archmib, 2, arch, &archlen, NULL, 0) == -1) 345 return NULL; 346 arch[archlen-1] = 0; 347 if (strlcat(sitepath, arch, sizeof(sitepath)) >= sizeof(sitepath)) 348 return NULL; 349 350 reldate = getosreldate(); 351 for(i = 0; releases[i].directory != NULL; i++) { 352 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) { 353 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath)) 354 >= sizeof(sitepath)) 355 return NULL; 356 break; 357 } 358 } 359 360 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath)) 361 return NULL; 362 363 return sitepath; 364 365} 366 367static void 368usage(void) 369{ 370 fprintf(stderr, "%s\n%s\n", 371 "usage: pkg_add [-viInfFrRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]", 372 " pkg-name [pkg-name ...]"); 373 exit(1); 374} 375