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