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