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