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