main.c revision 166374
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 166374 2007-01-31 22:34:45Z ade $"); 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[] = "hvIRfFnrp: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; 40 41char *Mode = NULL; 42char *Owner = NULL; 43char *Group = NULL; 44char *PkgName = NULL; 45char *PkgAddCmd = NULL; 46char *Directory = NULL; 47char FirstPen[FILENAME_MAX]; 48add_mode_t AddMode = NORMAL; 49 50char **pkgs; 51 52struct { 53 int lowver; /* Lowest version number to match */ 54 int hiver; /* Highest version number to match */ 55 const char *directory; /* Directory it lives in */ 56} releases[] = { 57 { 410000, 410000, "/packages-4.1-release" }, 58 { 420000, 420000, "/packages-4.2-release" }, 59 { 430000, 430000, "/packages-4.3-release" }, 60 { 440000, 440000, "/packages-4.4-release" }, 61 { 450000, 450000, "/packages-4.5-release" }, 62 { 460000, 460001, "/packages-4.6-release" }, 63 { 460002, 460099, "/packages-4.6.2-release" }, 64 { 470000, 470099, "/packages-4.7-release" }, 65 { 480000, 480099, "/packages-4.8-release" }, 66 { 490000, 490099, "/packages-4.9-release" }, 67 { 491000, 491099, "/packages-4.10-release" }, 68 { 492000, 492099, "/packages-4.11-release" }, 69 { 500000, 500099, "/packages-5.0-release" }, 70 { 501000, 501099, "/packages-5.1-release" }, 71 { 502000, 502009, "/packages-5.2-release" }, 72 { 502010, 502099, "/packages-5.2.1-release" }, 73 { 503000, 503099, "/packages-5.3-release" }, 74 { 504000, 504099, "/packages-5.4-release" }, 75 { 505000, 505099, "/packages-5.5-release" }, 76 { 600000, 600099, "/packages-6.0-release" }, 77 { 601000, 601099, "/packages-6.1-release" }, 78 { 602000, 602099, "/packages-6.2-release" }, 79 { 300000, 399000, "/packages-3-stable" }, 80 { 400000, 499000, "/packages-4-stable" }, 81 { 502100, 502128, "/packages-5-current" }, 82 { 503100, 599000, "/packages-5-stable" }, 83 { 600100, 699000, "/packages-6-stable" }, 84 { 700000, 799000, "/packages-7-current" }, 85 { 0, 9999999, "/packages-current" }, 86 { 0, 0, NULL } 87}; 88 89static char *getpackagesite(void); 90int getosreldate(void); 91 92static void usage __P((void)); 93 94int 95main(int argc, char **argv) 96{ 97 int ch, error; 98 char **start; 99 char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr; 100 static char temppackageroot[MAXPATHLEN]; 101 static char pkgaddpath[MAXPATHLEN]; 102 103 if (*argv[0] != '/' && strchr(argv[0], '/') != NULL) 104 PkgAddCmd = realpath(argv[0], pkgaddpath); 105 else 106 PkgAddCmd = argv[0]; 107 108 start = argv; 109 while ((ch = getopt(argc, argv, Options)) != -1) { 110 switch(ch) { 111 case 'v': 112 Verbose++; 113 break; 114 115 case 'p': 116 Prefix = optarg; 117 PrefixRecursive = FALSE; 118 break; 119 120 case 'P': 121 Prefix = optarg; 122 PrefixRecursive = TRUE; 123 break; 124 125 case 'I': 126 NoInstall = TRUE; 127 break; 128 129 case 'R': 130 NoRecord = TRUE; 131 break; 132 133 case 'f': 134 Force = TRUE; 135 break; 136 137 case 'F': 138 FailOnAlreadyInstalled = FALSE; 139 break; 140 141 case 'K': 142 KeepPackage = TRUE; 143 break; 144 145 case 'n': 146 Fake = TRUE; 147 break; 148 149 case 'r': 150 Remote = TRUE; 151 break; 152 153 case 't': 154 if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen)) 155 errx(1, "-t Argument too long."); 156 break; 157 158 case 'S': 159 AddMode = SLAVE; 160 break; 161 162 case 'M': 163 AddMode = MASTER; 164 break; 165 166 case 'C': 167 Chroot = optarg; 168 break; 169 170 case 'h': 171 case '?': 172 default: 173 usage(); 174 break; 175 } 176 } 177 argc -= optind; 178 argv += optind; 179 180 if (AddMode != SLAVE) { 181 pkgs = (char **)malloc(argc * sizeof(char *)); 182 for (ch = 0; ch <= argc; pkgs[ch++] = NULL) ; 183 184 /* Get all the remaining package names, if any */ 185 for (ch = 0; *argv; ch++, argv++) { 186 char temp[MAXPATHLEN]; 187 if (Remote) { 188 if ((packagesite = getpackagesite()) == NULL) 189 errx(1, "package name too long"); 190 if (strlcpy(temppackageroot, packagesite, 191 sizeof(temppackageroot)) >= sizeof(temppackageroot)) 192 errx(1, "package name too long"); 193 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot)) 194 >= sizeof(temppackageroot)) 195 errx(1, "package name too long"); 196 remotepkg = temppackageroot; 197 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' && 198 (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' && 199 !ptr[4])) 200 if (strlcat(remotepkg, 201#if defined(__FreeBSD_version) && __FreeBSD_version >= 500039 202 ".tbz", 203#else 204 ".tgz", 205#endif 206 sizeof(temppackageroot)) >= sizeof(temppackageroot)) 207 errx(1, "package name too long"); 208 } 209 if (!strcmp(*argv, "-")) /* stdin? */ 210 pkgs[ch] = (char *)"-"; 211 else if (isURL(*argv)) { /* preserve URLs */ 212 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp)) 213 errx(1, "package name too long"); 214 pkgs[ch] = strdup(temp); 215 } 216 else if ((Remote) && isURL(remotepkg)) { 217 if (strlcpy(temp, remotepkg, sizeof(temp)) >= sizeof(temp)) 218 errx(1, "package name too long"); 219 pkgs[ch] = strdup(temp); 220 } else { /* expand all pathnames to fullnames */ 221 if (fexists(*argv)) /* refers to a file directly */ 222 pkgs[ch] = strdup(realpath(*argv, temp)); 223 else { /* look for the file in the expected places */ 224 if (!(cp = fileFindByPath(NULL, *argv))) { 225 /* let pkg_do() fail later, so that error is reported */ 226 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp)) 227 errx(1, "package name too long"); 228 pkgs[ch] = strdup(temp); 229 } else { 230 if (strlcpy(temp, cp, sizeof(temp)) >= sizeof(temp)) 231 errx(1, "package name too long"); 232 pkgs[ch] = strdup(temp); 233 } 234 } 235 } 236 if (packagesite != NULL) 237 packagesite[0] = '\0'; 238 } 239 } 240 /* If no packages, yelp */ 241 else if (!ch) { 242 warnx("missing package name(s)"); 243 usage(); 244 } 245 else if (ch > 1 && AddMode == MASTER) { 246 warnx("only one package name may be specified with master mode"); 247 usage(); 248 } 249 /* Perform chroot if requested */ 250 if (Chroot != NULL) { 251 if (chroot(Chroot)) 252 errx(1, "chroot to %s failed", Chroot); 253 } 254 /* Make sure the sub-execs we invoke get found */ 255 setenv("PATH", 256 "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin", 257 1); 258 259 /* Set a reasonable umask */ 260 umask(022); 261 262 if ((error = pkg_perform(pkgs)) != 0) { 263 if (Verbose) 264 warnx("%d package addition(s) failed", error); 265 return error; 266 } 267 else 268 return 0; 269} 270 271static char * 272getpackagesite(void) 273{ 274 int reldate, i; 275 static char sitepath[MAXPATHLEN]; 276 struct utsname u; 277 278 if (getenv("PACKAGESITE")) { 279 if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath)) 280 >= sizeof(sitepath)) 281 return NULL; 282 return sitepath; 283 } 284 285 if (getenv("PACKAGEROOT")) { 286 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath)) 287 >= sizeof(sitepath)) 288 return NULL; 289 } else { 290 if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath)) 291 >= sizeof(sitepath)) 292 return NULL; 293 } 294 295 if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath)) 296 >= sizeof(sitepath)) 297 return NULL; 298 299 uname(&u); 300 if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath)) 301 return NULL; 302 303 reldate = getosreldate(); 304 for(i = 0; releases[i].directory != NULL; i++) { 305 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) { 306 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath)) 307 >= sizeof(sitepath)) 308 return NULL; 309 break; 310 } 311 } 312 313 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath)) 314 return NULL; 315 316 return sitepath; 317 318} 319 320static void 321usage() 322{ 323 fprintf(stderr, "%s\n%s\n", 324 "usage: pkg_add [-vInfFrRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]", 325 " pkg-name [pkg-name ...]"); 326 exit(1); 327} 328