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