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