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