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