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