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